need help with loop error

Write your question here.
My issue here is that after an user enters '0' for their second operand, the program outputs "division by zero is not possible" and asks to re-enter the second operand. What it does it, it calculates FINE but the program ends. I dont know how to get the program to repeat the menu again.

I only want the program to terminate if the user says "n" when it asks if you want to try again. Otherwise, I want it to keep looping and brining up the menu.



Also, I am getting an "being used without initlized" if i do the division normally. Example '/' and then 5 for the first operand and 1 for the second operand, it crashes with error even though the correct answer is outputted. The menu does not loop!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  #include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
 
int firstOperand;
int secondOperand;
char choice;
char TryAgain;
bool keepgoing = true;
 
// Display the menu choices
do {
cout << "            MENU\n\n";
cout << "=========================\n";
cout << "+) Add\n";
cout << "-) Subtract\n";
cout << "*) Multiply\n";
cout << "/) Divide\n";
cout << "X) Exit\n";
cout << "=========================\n";
cout << "Enter your choice: ";
cin  >> choice;
 
 
if(choice !='+' && choice !='-' && choice !='*' && choice != '/' && choice!='X' && choice!='x')
{
cout <<"Invalid Choice.\n";
cout << "\n";
cout << "Valid choices are +, -. *, /, or x.\n";
cout << "Please try again. \n";
//keepgoing = false;
}
else if (keepgoing)
{
if (choice =='+' || choice == '-' || choice == '*' || choice == '/') 
{
cout << "Enter the first operand: ";
cin >> firstOperand;
cout << "Enter the second operand: ";
cin >>secondOperand;
 
switch (choice)
{
case '+':
//case '+':
cout << firstOperand << " + " << secondOperand << " is " << firstOperand + secondOperand<<endl;
break;
case '-':
//case '-':
cout << firstOperand << " - " << secondOperand << " is " << firstOperand - secondOperand<<endl;
break;
case '*':
//case '*':
cout << firstOperand << " * " << secondOperand << " is" << firstOperand * secondOperand<<endl;
break;
case '/':
if (secondOperand == 0)
{
cout << "Division by zero is not possible"<<endl;
 
cout << "Would you like to try again (y/n): ";
cin >> TryAgain;
char tolower(TryAgain);
 
if(TryAgain == 'y')
{
 
cout << "Re-Enter the second operand: ";
cin >>secondOperand;
cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;
 
}
}
else 
//case '/':
cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;
break;
 
 
}
}
}
 
}while (choice != 'X' && choice !='x'&& TryAgain== 'n' && TryAgain == 'N'); //while ( !(choice == 'x' || choice == 'X') )
cout << "You have chose to Exit. \n";
 
 
return 0;
}
 
closed account (jyU4izwU)
Why

"Write your question here."

What??
Last edited on
now it works!!

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{

int firstOperand;
int secondOperand;
char choice = 'y';/////
char TryAgain = 'NULL';/////
bool keepgoing = true;

while ((choice == 'y') || (choice == 'Y')) ///<----- I changed your code here
{
cout << " MENU\n\n";
cout << "=========================\n";
cout << "+) Add\n";
cout << "-) Subtract\n";
cout << "*) Multiply\n";
cout << "/) Divide\n";
cout << "X) Exit\n";
cout << "=========================\n";
cout << "Enter your choice: ";
cin >> choice;


if(choice !='+' && choice !='-' && choice !='*' && choice != '/' && choice!='X' && choice!='x')
{
cout <<"Invalid Choice.\n";
cout << "\n";
cout << "Valid choices are +, -. *, /, or x.\n";
cout << "Please try again. \n";
}
else if (keepgoing)
{
if (choice =='+' || choice == '-' || choice == '*' || choice == '/')
{
cout << "Enter the first operand: ";
cin >> firstOperand;
cout << "Enter the second operand: ";
cin >>secondOperand;

switch (choice)
{
case '+':
//case '+':
cout << firstOperand << " + " << secondOperand << " is " << firstOperand + secondOperand<<endl;
break;
case '-':
//case '-':
cout << firstOperand << " - " << secondOperand << " is " << firstOperand - secondOperand<<endl;
break;
case '*':
//case '*':
cout << firstOperand << " * " << secondOperand << " is" << firstOperand * secondOperand<<endl;
break;
case '/':
if (secondOperand == 0)
{
cout << "Division by zero is not possible"<<endl;

cout << "Would you like to try again (y/n): ";
cin >> TryAgain;
char tolower(TryAgain);

if(TryAgain == 'y')
{

cout << "Re-Enter the second operand: ";
cin >>secondOperand;
cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;

}
}
else
//case '/':
cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;
break;


}
}
}
cout<<"would you like to calculate once more? ";/// <------ here
cin>>choice; //// <---here

}
cout << "You have chose to Exit. \n"; //// <-- here


return 0;
}
Last edited on
closed account (jyU4izwU)
Please dont spam you can serch online
Yes You Can
Edward,

Thanks. However in your code, does the whole menu repeats?

I changed my code to get it to work and now the only thing that does not work is the program does not exit once the user inputs "no" to try again. Would you be able to keep my format as I want to use 'do' loop in the beginning.

It says "exiting" but the menu comes back up instead of exiting like it does when an user inputs "X" for exit.

Thanks for the help!



#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{

int firstOperand;
int secondOperand;
char choice;
char TryAgain;
bool keepgoing = true;

// Display the menu choices
do {
cout << " MENU\n\n";
cout << "=========================\n";
cout << "+) Add\n";
cout << "-) Subtract\n";
cout << "*) Multiply\n";
cout << "/) Divide\n";
cout << "X) Exit\n";
cout << "=========================\n";
cout << "Enter your choice: ";
cin >> choice;


if(choice !='+' && choice !='-' && choice !='*' && choice != '/' && choice!='X' && choice!='x')
{
cout <<"Invalid Choice.\n";
cout << "\n";
cout << "Valid choices are +, -. *, /, or x.\n";
cout << "Please try again. \n";
keepgoing = true;
}
else if (keepgoing)
{
if (choice =='+' || choice == '-' || choice == '*' || choice == '/')
{
cout << "Enter the first operand: ";
cin >> firstOperand;
cout << "Enter the second operand: ";
cin >>secondOperand;

switch (choice)
{
case '+':
//case '+':
cout << firstOperand << " + " << secondOperand << " is " << firstOperand + secondOperand<<endl;
break;
case '-':
//case '-':
cout << firstOperand << " - " << secondOperand << " is " << firstOperand - secondOperand<<endl;
break;
case '*':
//case '*':
cout << firstOperand << " * " << secondOperand << " is" << firstOperand * secondOperand<<endl;
break;
case '/':
if (secondOperand >0)
{
cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;
}
else if (secondOperand == 0)
{
cout << "Division by zero is not possible"<<endl;
cout << "Would you like to try again? (y/n): ";
cin >> TryAgain;
tolower (TryAgain);
}
if (secondOperand == 0 && TryAgain == 'y')
{
cout << "Re-Enter second operand: ";
cin >> secondOperand;
cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;
}
if (TryAgain == 'n')
//case '/':
cout << "exiting"<<endl;
//break;
//case 'X' || 'x':
////case 'x':
//cout << "You have chose to Exit.\n";
//break;

//}
//cout << "grade has been validated so we can move on ...\n";
}
}
}

}while (choice != 'X' && choice !='x'); //while ( !(choice == 'x' || choice == 'X') )
cout << "You have chose to Exit. \n";
return 0;
}










ibranext (33)
Please dont spam you can serch online


????

Ibranext, you can search online for what? I need help, how am I spamming?
add this: cout<<"continue?";
cin>>choice;

before: }while (choice != 'X' && choice !='x');


as long as you're not to going to enter X ot x, it will go on ..
Last edited on
Edward,

if I add cout << "continue?";
cin >>choice;

then what happens is that even when it comes to mult, add,sub, everytime after I am done entering the 2 operands, it will keep asking "continue"?. I dont want it to ask to continue or not.

Right now the program is behaving as I want it but im trying to find a way to exit the whole program if the user enters a 0 for secondoperand in division only and if "do you want to try again?" equals "no", i want it to just quit the whole program but the menu keeps repeating.

Is this something that is impossible to do within a switch statement?
Nevermind, I get what your saying. Thanks, it worked!!!! I been on this for few hours now. THANK YOU!
well it def works when it comes to division, but if i do addition or something, it crashes after it does its first calculation! d'oh!!! ah this is frustrating.
it works fine for me: (copy-paste it, and see if it's still crushes)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{

	int firstOperand;
	int secondOperand;
	char choice;
	char TryAgain;
	bool keepgoing = true;

	// Display the menu choices
	do {
		cout << " MENU\n\n";
		cout << "=========================\n";
		cout << "+) Add\n";
		cout << "-) Subtract\n";
		cout << "*) Multiply\n";
		cout << "/) Divide\n";
		cout << "X) Exit\n";
		cout << "=========================\n";
		cout << "Enter your choice: ";
		cin >> choice;


		if(choice !='+' && choice !='-' && choice !='*' && choice != '/' && choice!='X' && choice!='x')
		{
			cout <<"Invalid Choice.\n";
			cout << "\n";
			cout << "Valid choices are +, -. *, /, or x.\n";
			cout << "Please try again. \n";
			keepgoing = true;
		}
		else if (keepgoing)
		{
			if (choice =='+' || choice == '-' || choice == '*' || choice == '/')
			{
				cout << "Enter the first operand: ";
				cin >> firstOperand;
				cout << "Enter the second operand: ";
				cin >>secondOperand;

				switch (choice)
				{
				case '+':
					//case '+':
					cout << firstOperand << " + " << secondOperand << " is " << firstOperand + secondOperand<<endl;
					break;
				case '-':
					//case '-':
					cout << firstOperand << " - " << secondOperand << " is " << firstOperand - secondOperand<<endl;
					break;
				case '*':
					//case '*':
					cout << firstOperand << " * " << secondOperand << " is" << firstOperand * secondOperand<<endl;
					break;
				case '/':
					if (secondOperand >0)
					{
						cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;
					}
					else if (secondOperand == 0)
					{
						cout << "Division by zero is not possible"<<endl;
						cout << "Would you like to try again? (y/n): ";
						cin >> TryAgain;
						tolower (TryAgain);
					}
					if (secondOperand == 0 && TryAgain == 'y')
					{
						cout << "Re-Enter second operand: ";
						cin >> secondOperand;
						cout << firstOperand << "/ " << secondOperand << " is" << firstOperand / secondOperand<<endl;
					}
					if (TryAgain == 'n')
						//case '/':
							cout << "exiting"<<endl;
					//break;
					//case 'X' || 'x':
					////case 'x':
					//cout << "You have chose to Exit.\n";
					//break;

					//}
					//cout << "grade has been validated so we can move on ...\n";
				}
			}
		}
		cout<<"continue?";
		cin>>choice;
	}while (choice != 'X' && choice !='x'); //while ( !(choice == 'x' || choice == 'X') )
	cout << "You have chose to Exit. \n";
	return 0;
}
Last edited on
The problem is that, it keeps asking to "continue" everytime you are done with a calculation.

The assignment is suppose to be where:
an user does the calculation and it keeps looping until the user presses "x" to quit.

If an user tries to divide by zero, it gives an error and asks if they want to try again.

If the user enters "y" then it only asks what the 2nd operand was and does its division and the menu loops without stopping.

If the user enters "no" they dont want to try again, the program is suppose to fully terminate (which in this case it does not).

ok, tell me if i understand you, only if the user tried to devide by zero, only then he can press x and exit? or the exit function can be operated at any time?
the issue with his program was that he had it set to (while ==n )that means it will repeat if it equals n since he typed y it closed down. This change is all he needs to make it work
1
2
3
}while (choice != 'X' && choice !='x' && TryAgain!= 'n' && TryAgain != 'N'); 
cout << "You have chose to Exit. \n";
 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

char selection();
void getData(int &firstOperand, int &secondOperand);
void switchFunction(char &choice, int &firstOperand, int &secondOperand);
void division(int firstOperand, int secondOperand);

int main()
{

	char choice;
	int firstOperand = 0;
	int secondOperand = 0;

	choice = selection();

	while (choice != 'X')
	{
		getData(firstOperand, secondOperand);
		switchFunction(choice, firstOperand, secondOperand);
		cout << endl << endl;
		choice = selection();
	}

	cout << "You have chose to Exit. \n";

	cin.ignore();
	return 0;
}


// Display the menu choices
char selection()
{
	char choice = ' ';
	cout << "            MENU\n\n";
	cout << "=========================\n";
	cout << "+) Add\n";
	cout << "-) Subtract\n";
	cout << "*) Multiply\n";
	cout << "/) Divide\n";
	cout << "X) Exit\n";
	cout << "=========================\n";
	cout << "Enter your choice: ";
	cin  >> choice;
	cin.ignore();
	choice = toupper(choice);

	while(choice != '+' && choice != '-' && choice != '*' && choice != '/' && choice != 'X')
	{
		cout << "Your selection is not valid please try again" << endl;
		cout << "Enter your choice: ";
		cin  >> choice;
		cin.ignore();
		choice = toupper(choice);
	}

	return choice;
		
}// end selection



void switchFunction(char &choice, int &firstOperand, int &secondOperand)
{

	switch (choice)
		{
		case '+':
			//case '+':
			cout << firstOperand << " + " << secondOperand << " is " << firstOperand + secondOperand<<endl;
			break;
		case '-':
			//case '-':
			cout << firstOperand << " - " << secondOperand << " is " << firstOperand - secondOperand<<endl;
			break;
		case '*':
			//case '*':
			cout << firstOperand << " * " << secondOperand << " is " << firstOperand * secondOperand<<endl;
			break;
		case '/':
			division(firstOperand, secondOperand);
			
		}//end switch

}//end switchFunction


//divides two operands
void division(int firstOperand, int secondOperand)
{
	char answer = 'N';

	while (secondOperand == 0)
	{
		cout << "Division by zero is not possible would you like to try again [Y/N]"<<endl;
		cin >> answer;
		cin.ignore();
		answer = toupper(answer);

		if (answer == 'Y')
		{
		cout << "Enter the second operand: ";
		cin >>secondOperand;
		cin.ignore();
		
		}

	}
	
	
	cout << firstOperand << "/ " << secondOperand << " is " << (double)firstOperand / secondOperand<<endl;


}// end division


//gets operands from the user
void getData(int &firstOperand, int &secondOperand)
{
		
	cout << "Enter the first operand: ";
	cin >> firstOperand;
	cin.ignore();

	cout << "Enter the second operand: ";
	cin >>secondOperand;
	cin.ignore();

}//end getData 
Last edited on
Topic archived. No new replies allowed.