Why is my program looping infinitely? - infinite-loop

My program skips past the option for the user to enter the squareSide and keeps looping.
Code is as follows:
do
{
//Displays menu
cout << "Please select a geometric shape." << endl << endl;
cout << "s:" << setw(10) << "Square" << endl;
cout << "c:" << setw(10) << "Circle" << endl;
cout << "d:" << setw(10) << "Diamond" << endl;
cout << "t:" << setw(10) << "Triangle" << endl;
cout << "e:" << setw(10) << "Exit" << endl << endl;
cin >> letter;
cout << "You selected " << letter << endl;
if (letter != EXIT || letter == EXIT1)
{
if (letter == SQUARE || letter == SQUARE1)
{
int squareSide;
int character;
cout <<"\nPlease enter the ASCII character you would like to use to print your square" << endl;
cin >> character;
cout << "\nPlease enter the length of one side of your square"
<< endl;
cin >> squareSide;
for (int x=0; x < squareSide; x++)
{
for (int y = 0; y < squareSide; y++)
{
cout << character;
}
cout << endl;
}
}
}
} while (letter != EXIT && letter != EXIT1);
return 0;
}
All menu selections have to be char.

Related

Passing map by reference in C++ and mutating its value

I am passing map by reference in the canSum function where i am mutating its value and adding pairs but at the end when I iterate over the map I find the value of map has not been updated.
canSum function is a recursive function which takes a number (targetSum) and an array and finds if it is possible to form targetSum by any combinations of number in the array (numbers can be repeated).
#include<iostream>
#include<vector>
#include<map>
using namespace std;
bool canSum(int targetSum,vector<int> a,map<int, bool> &m){
if(!(m.find(targetSum) == m.end()))
return m[targetSum];
if (targetSum == 0)
return true;
if(targetSum<0)
return false;
for (int num : a)
{
if (canSum(targetSum - num, a,m)==true)
{
// m[targetSum] = true;
m.insert(pair<int, bool>(targetSum, true));
return m[targetSum];
}
}
m[targetSum] = false;
return m[targetSum];
}
int main(){
int targetSum, t;
vector<int> a;
map<int, bool> m;
m[0] = true;
cout << "enter target" << endl;
cin >> targetSum;
cout << "enter array, press esc to stop entering"<<endl;
while(cin>>t){
a.push_back(t);
}
for (int j = 0; j < a.size(); j++)
{
cout << a[j]<<" ";
}
cout << endl;
for (auto itr = m.begin(); itr != m.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
if(canSum(targetSum, a,m)){
cout << endl << "true" << endl;
}
else cout << endl << "false" << endl;
return 0;
}
Please help me. Thank you.
The for loop to print the map should be after the function call like.
if(canSum(targetSum, a,m)){
cout << endl << "true" << endl;
}
else cout << endl << "false" << endl;
for (auto itr = m.begin(); itr != m.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
Instead of
for (auto itr = m.begin(); itr != m.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
if(canSum(targetSum, a,m)){
cout << endl << "true" << endl;
}
else cout << endl << "false" << endl;
To see mutations in the map due to the function

Is there any explanation for this behavior?

Memory address shows two different values.
We have const variable (a) and we put the address of the variable into two pointers (b and c). After changing the value at the address in one of the pointers (c), we had a situation where the same memory address has two different values.
Is there any explanation for this behavior?
#include <iostream>
int main(void)
{
const int a = 99;
const int *b = &a;
int *c = (int *)b;
std::cout << &a << " - " << a << '\n';
std::cout << b << " - " << *b << '\n';
std::cout << c << " - " << *c << "\n\n";
*c = 61;
std::cout << &a << " - " << a << '\n';
std::cout << b << " - " << *b << '\n';
std::cout << c << " - " << *c << '\n';
return 0;
}
//here are the result(output)
003CFAA4 - 99
003CFAA4 - 99
003CFAA4 - 99
003CFAA4 - 99
003CFAA4 - 61
003CFAA4 - 61

ifstream reading multiple data types from same line BAD_ACCESS

I'm trying to read in multiple element types from a text file.
Here's an example input:
push 0
push 1
push 3
push 5
add
push 3
The code below work until the last line, then I get a BAD_ACCESS error after trying anything after it reads in a line without 'b'. Any suggestions?
ifstream infile("example.txt");
while (!infile.eof())
{
infile >> a >> b;
if (a == "push")
push(b);
if (a == "add")
{
add();
}
if (a == "subtract")
{
int y = subtract();
cout << y << endl;
}
if (a == "multiply")
{
int y = multiply();
cout << y << endl;
}
if (a == "divide")
{
int y = divide();
cout << y << endl;
}
if (a == "pop")
{
cout << b;
b = NULL;
}
cout << a << b << endl;
}

I have created a dice betting game. I cannot get the loop to run correctly could someone help me with finding the issue?

Okay so I need help with getting the bank to NOT reset to 100 after each time the loop runs. I have tried many ways but can't seem to get it to work. Could you please help me with a few explanations and examples?
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int displaystats(int gamesplayed, int wins, int losses, int bank);
int main()
{
int bank = 100;//intital bank value
int bet = 0;//desired wager
int wins = 0;//games won
int losses = 0;//games lost
int gamesplayed = 0;//how many rounds you played
int compdice1 = 0;//first rolled dice for computer
int compdice2 = 0;//second rolled dice for computer
int playdice1 = 0;//first rolled dice for player
int playdice2 = 0;//seconds rolled dice for player
int newdice = 0;//the dice to risk your wager
int comproll = 0;//the sum of the computers roll
int playroll = 0;//the sum of the players roll
do
{
if (bank < 0)
{
cout << "You have " << bank << " coins in your bank." << endl;
cout << "I am sorry you are out of money." << endl;
displaystats(gamesplayed, wins, losses, bank);
break;
}
else if (bank > 0)
{
cout << "You have " << bank << " coins in your bank." << endl;
cout << "How many coins would you like to bet? ";
cin >> bet;
compdice1 = (rand() + time(0)) % 6 + 1;//computer dice
compdice2 = (rand() + time(0)) % 6 + 1;//computer second dice
playdice1 = (rand() + time(0)) % 6 + 1;//player dice
playdice2 = (rand() + time(0)) % 6 + 1;//player second dice
comproll = compdice1 + compdice2;//computer sum
playroll = playdice1 + playdice2;//player sume
cout << "Your roll was " << playdice1 << " and " << playdice2 << " with a sume of " << playroll << endl;
if (playroll < comproll)
{
char option;//option to roll another dice
cout << "You win!" << endl;
cout << "Would you like to roll a third dice to earn 1.5 times your bet, yes or no? ";
cin >> option;
if (option == 'yes')
{
int newroll;//the new sum of the three dice
int newdice;//the extra roll
newdice = (rand() + time(0)) % 6 + 1;
newroll = playroll + newdice;//the value of players roll
if (newroll > comproll)
{
cout << "The computer rolled " << comproll << endl;
cout << "You now rolled higher than the computer therefore, I am sorry you lose this round." << endl;
cout << "Your bank now equals " << bank - bet << endl;
losses++;
gamesplayed++;
}
else if (newroll < comproll)
{
cout << "You win!" << endl;
cout << "Your bank now equals " << bank + (1.5 * bet) << endl;
wins++;
gamesplayed++;
}
}
else if (option == 'no')
{
cout << "Your bank now equals " << bank + bet << endl;
wins++;
gamesplayed++;
}
}
else if (playroll > comproll)
{
cout << "The computer rolled " << comproll << endl;
cout << "You rolled higher than the computer therefore, I am sorry you lose this round." << endl;
cout << "Your bank now equals " << bank - bet << endl;
losses++;
gamesplayed++;
}
else if (playroll = comproll)
{
cout << "The computer also rolled " << comproll << endl;
cout << "I am sorry you now lose double your bet!" << endl;
cout << "Your bank now equals " << bank - (2 * bet) << endl;
losses++;
gamesplayed++;
}
}
} while (bank > 0);
int stats = displaystats(gamesplayed, wins, losses, bank);
cout << "Your stats are " << stats << endl;
return 0;
}
int displaystats(int gamesplayed, int wins, int losses, int bank)
{
cout << "Games Played: " << gamesplayed << endl;
cout << "Wins: " << wins << endl;
cout << "Losses: " << losses << endl;
cout << "Bank Total: " << bank << endl;
return (gamesplayed, wins, losses, bank);
}
Your problem with the bank "resetting" constantly was you never actually subtracted the bet from the bank. See the following code, I hope this helps.
cout << "You have " << bank << " coins in your bank." << endl;
cout << "How many coins would you like to bet? ";
cin >> bet;
//This is the line that you forgot.
bank = bank - bet
If you then win, then you may want to later on add some money back into the bank. (But this is up to you.) I hope this helps.
EDIT:
Here is a paste-bin for the full code as requested: http://pastebin.com/HzvRxjXL
Also, if this solves your problem, I would appreciate it if you would mark it as the answer so others don't spend time answering a problem that has been solved.
EDIT 2:
This has a (As far as I can tell) fixed and commentated version of the code. I hope this helps: http://pastebin.com/miQjy4B5

Runtime Check Failure #3 - Variable is not initiliazed

I really need some help on this problem. My project is to create a math tutor program with a menu that look like this:
cout << "Select one of the following options" << endl;
cout << "-----------------------------------------" << endl;
cout << "1. [A]dd [+]" << endl;
cout << "2. [S]ubtract [-]" << endl;
cout << "3. [M]ultiply [x]" << endl;
cout << "4. [D]ivide [/]" << endl;
cout << "5. [Q]uit " << endl;
when the user chose add or subtract, the program will generate 2 random numbers and come up with the result. When the user chose multiply or divide, the program will generate the second number again and do the math.
Here is my code
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{ //Introduction to user
cout << "Welcome to Math Tutor. This program is designed to help student" << endl;
cout << "to learn how to do basic math such as addition, subtraction,..." << endl;
cout << endl;
//Declare variables
char choice;
int num1, num2, answer, useranswer, remainder, userremainder;
//Seed 2 random numbers
srand(static_cast<unsigned int>(time(0)));
num1 = rand() % 200 + 800;
num2 = 1 + rand() % 200;
do
{
//Display the menu to user and get user input
cout << "Select one of the following options" << endl;
cout << "-----------------------------------------" << endl;
cout << "1. [A]dd [+]" << endl;
cout << "2. [S]ubtract [-]" << endl;
cout << "3. [M]ultiply [x]" << endl;
cout << "4. [D]ivide [/]" << endl;
cout << "5. [Q]uit " << endl;
cin >> choice;
cout << "Your choice is: " << choice << endl;
//Respond to user's selection
switch (choice)
{
case 'A':
case 'a':
case '1':
case '+':
answer = num1 + num2;
cout << "" << num1 << endl;
cout << "+" << num2 << endl;
cout << "-----" << endl;
cout << "Enter your answer: " << useranswer;
if (answer == useranswer)
{
cout << "Congratulation, Your answer is correct" << endl;
cout << num1;
cout << "+" << num2;
cout << "------" << endl;
cout << answer;
}
else
{
cout << "You answer is not correct" << endl;
cout << "The answer is: " << answer;
}
break;
case 'S':
case 's':
case '2':
case '-':
answer = num1 - num2;
cout << num1;
cout << "-" << num2;
cout << "------" << endl;
cout << "Enter your answer: " << useranswer;
if (answer == useranswer)
{
cout << "Congratulation, Your answer is correct" << endl;
cout << num1;
cout << "-" << num2;
cout << "------" << endl;
cout << answer;
}
else
{
cout << "You answer is not correct" << endl;
cout << "The answer is: " << answer;
}
break;
//Seed the second number again
num2 = 1 + rand() % 10;
case 'M':
case 'm':
case '3':
case 'x':
answer = num1 * num2;
cout << num1;
cout << "x" << num2;
cout << "------" << endl;
cout << "Enter your answer: " << useranswer;
if (answer == useranswer)
{
cout << "Congratulation, Your answer is correct" << endl;
cout << num1;
cout << "x" << num2;
cout << "------" << endl;
cout << answer;
}
else
{
cout << "You answer is not correct" << endl;
cout << "The answer is: " << answer;
}
break;
case 'D':
case 'd':
case '4':
case '/':
answer = (num1 / num2);
cout << " __________" << endl;
cout << num2 << ")" << num1 << endl;
cout << "Enter your answer, both the quotient";
cout << "and the remainder separated by a space: " << endl;
cin >> useranswer >> remainder;
if ((useranswer == answer) && (userremainder = remainder))
{
cout << "Congratulation, Your answer is correct" << endl;
cout << " __________" << endl;
cout << num2 << ")" << num1 << endl;
cout << "The answer is: " << answer << "and the remainder is: " << remainder << endl;
}
else
{
cout << "Sorry. The correct answer is: " << answer << " and the remainder is: " << remainder << endl;
}
case 'Q':
case 'q':
case '5':
cout << "Exiting the program....." << endl;
break;
//Validate user's chocie
default:
{
cout << "Please enter a valid choice" << endl;
cout << "You can enter a number from 1 to 5 or" << endl;
cout << "A letter corresponding to the calculation that you want to do" << endl;
cin >> choice;
cout << "Your choice is: " << choice;
}
}
//Pause the screen
if (choice != 5)
{
cout << endl;
cin.ignore(80, '\n');
cout << "Hit the enter key to continue....\n";
cin.get();
cout << endl;
}
} while (choice != 5);
return 0;
}
your problem is the lines
cout << "Enter your answer: " << useranswer
useranswer is what you wanted the user to input, so change these lines to
cout << "Enter your answer: ";
if(cin >> useranswer) { ... } else { ... } // handle non-integer input too

Resources