not looping!

i made a program that acts like a calculator. the does its function but it needs to keep asking the user for new numbers and an operator until the user enters 'N' or 'n'. however it skips the process completely I tried moving the while statement around but I only got an infinite loop when i did so.


#include <iostream>
using namespace std;

int main(){

char operation;

int number1, number2;

cout << "Enter N to Quit";

cout << endl;

cout << "Enter a number ==> ";

cin >> number1;

cout << endl;

cout << "Enter second number ==> ";

cin >> number2;

cout << endl;

cout << "Choose on of the following; *, /, %, +, -";

cin >> operation;


while(number1 != 'n' || number1 != 'N')

cout << endl;

cout << "Enter second number ==> ";

cin >> number2;

cout << endl;

cout << "Choose on of the following; *, /, %, +, -";

cin >> operation;




switch(operation)

{
case '+':

cout<< number1 << " + " << number2 << "=" << number1+number2;

break;

case '-':

cout << number1 << " - " << number2 << " = " << number1-number2;

break;

case '*':

cout << number1 << " * " << number2 << " = " << number1*number2;

break;

case '/':

cout << number1 << " / " << number2 << " = " << number1/number2;

break;

case '%':

cout << number1 << " % " << number2 << " = " << number1%number2;

break;

default:

cout << "ERROR!";

}


cout << "Enter N to Quit";

cout << endl;

cout << "Enter a number ==> ";

cin >> number1;

cout << endl;

cout << endl;

cout << "Enter second number ==> ";

cin >> number2;

cout << endl;

cout << "Choose on of the following; *, /, %, +, -";

cin >> operation;





}
You need to have a "char" variable for the user to input the quit character into. You have it set as an "int variable".

1
2
3
4
5
6
 char loopControl = 'y';

 while(loopControl == 'y')
{

}


or,
1
2
3
4
do{

//Ask you exit or quit question at the end of the calculation.
}while(loopControl == 'y') 


There are many ways of doing this type of operation, but these ways usually work best for me.
This is also one way that I would do this type of program.

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
int main()
{
 char loopControl = 'y';

 while(loopControl == 'y')
 {
  cout << "Would you like to make another calculation?";
  cin << loopControl;
  cout << endl;


       if (loopControl == 'y')
       {
            //calculation code inside this if statement

       }


 }   //While bracket

//You don't need an "if else", or an "else" statement if loopControl is
//something other than 'y' it won't do the "if" statement and continue with
//the rest of the program.
//Such as.

cin.ignore(2); //Or system("pause");
return 0;
}
//And the program will close. 


I hope this makes sense and that it helps.

If it doesn't make sense I will write it up in C++ and post it again with only the code no comments.
how would i incorperate the my actual code to inclulde a loop control variable one of the catches of this is that I can not cin.get().

#include <iostream>
using namespace std;

int main(){

char operation, decision;



int number1, number2;

cout << "would you like to continue? if yes enter 'y' : ";

cin >> decision;

cout << "Enter N to Quit";

cout << endl;

cout << "Enter a number ==> ";

cin >> number1;

cout << endl;

cout << "Enter second number ==> ";

cin >> number2;

cout << endl;

cout << "Choose on of the following; *, /, %, +, - : ";





while((cin >> operation) && decision == 'y')


switch(operation)

{
case '+':

cout<< number1 << " + " << number2 << " = " << number1+number2;

break;

case '-':

cout << number1 << " - " << number2 << " = " << number1-number2;

break;

case '*':

cout << number1 << " * " << number2 << " = " << number1*number2;

break;

case '/':

cout << number1 << " / " << number2 << " = " << number1/number2;

break;

case '%':

cout << number1 << " % " << number2 << " = " << number1%number2;

break;

default:

cout << "ERROR!";

cout << "enter another number : ";

cin >> number1;

cout << "enter yet another number : ";

cin >> number2;

cin.ignore(2);




}




if( number1 == 'n' || 'N')

cout << "Thank you for using";

cin.ignore(2);
[ code]
PUT CODE HERE
[/code]
Im doing copy paste but its reading the code as regular font.
You don't need the (cin>>operation) in the while statement. As well you cannot put a character into an "int" variable. When you use a "char" variable you don't need to use the cin.get(); function unless your performing a specific operation with it. You can just use cin >> charVariable;. If you are being instructed not to use a intvariable then you will need to find a way around it. Like setting the character 'n', and 'N' equal to the ANSII value and then using the value in the "int" variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()

{
	char ctrl = 'y';
	int num;

	while(ctrl == 'y')
	{
		cout << "Would you like to make a selection : ";
		cin >> ctrl;
		cout << endl;
	}

	system("pause");
	return 0;
}


Create a new program and copy and paste this exact code. I'm using MS VC++, I do not know the syntax to any other compiler. But if you put "num" in for "ctrl" you will get an error, and an infinite loop.

Also for inputing code like vasilenko93 said type [ code ] paste your code here [ /code ],(with no spaces, don't know how you outed that vasilenko93) or click the button that looks like this <>, to your right, under "Format:".
http://www.cplusplus.com/articles/z13hAqkS/ this will clear up how to use code tags
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
#include <iostream>
using namespace std;

int main(){
		
	char operation, decision;

	
	
	int number1, number2;

		cout << "would you like to continue? if yes enter 'y' : ";

		cin >> decision;
	
		cout << "Enter N to Quit";

		cout << endl;
		
		cout << "Enter a number ==> ";

		cin >> number1;

		cout << endl;

		cout << "Enter second number ==> ";

		cin >> number2;

		cout << endl;

		cout << "Choose on of the following; *, /, %, +, - : ";

	

	
	
		while((cin >> operation) && decision == 'y')

		
			switch(operation)

			{
					case '+':

						cout<< number1 << " + " << number2 << " = " << number1+number2;
					
						break;
					
					case '-':

						cout << number1 << " - " << number2 << " = " << number1-number2;

						break;

					case '*':

						cout << number1 << " * " << number2 << " = " << number1*number2;

						break;

					case '/':

						cout << number1 << " / " << number2 << " = " << number1/number2;

						break;

					case '%':

						cout << number1 << " % " << number2 << " = " << number1%number2;

						break;

					default: 

						cout << "ERROR!";

					 cout << "enter another number : ";

					 cin >> number1;

					 cout << "enter yet another number : ";

					 cin >> number2;
						
						cin.ignore(2);
			
		
				

}
			
	
			
				
				if( number1 == 'n' || 'N')
					
					cout << "Thank you for using";

			cin.ignore(2);
			
thank you. I have been learning in college as well and hope it help will help me a lot.
Topic archived. No new replies allowed.