The program Always works in the order that I put the code in, I want to make loops.

In this program


#include <iostream>
#include <math.h>

using namespace std;


int main()
{
float thisnumber, thisnumber1, thisnumber2, x;
string Answer, yorn;
cout<< "CALCULATOR\n";
cout<< "Hello, Press A to add, S to subtract,\n";
cout<< "D to Divide, and M to multiply.\n";
cout<< "What would you like to calculate? (D, M, A, or S)\n";
cin>> Answer;

while (Answer == "D");
{
cout<< "Enter a Number: ";
cin >> thisnumber;
cout<< "Enter a Number: ";
cin >> thisnumber1;
cin.ignore();
x = thisnumber / thisnumber1
;cout<< "" <<thisnumber<< " / " << thisnumber1 << " = " << x << "\n";
cin.get();
}
while (Answer == "M");
{
cout<< "Enter a Number: ";
cin >> thisnumber;
cout<< "Enter a Number: ";
cin >> thisnumber1;
cout<< "Enter a Number: ";
cin >> thisnumber2;
cin.ignore();
x = thisnumber * thisnumber1 *thisnumber2;
cout<< "" <<thisnumber<< " x " << thisnumber1 << " x " << thisnumber2 << " = " << x << "\n";
cin.get();
}
while (Answer == "A");
{
cout<< "Enter a Number: ";
cin >> thisnumber;
cout<< "Enter a Number: ";
cin >> thisnumber1;
cout<< "Enter a Number: ";
cin >> thisnumber2;
cin.ignore();
x = thisnumber + thisnumber1 +thisnumber2;
cout<< "" <<thisnumber<< " + " << thisnumber1 << " + " << thisnumber2 << " = " << x << "\n";
cin.get();
}
while (Answer == "S");
{
cout<< "Enter a Number: ";
cin >> thisnumber;
cout<< "Enter a Number: ";
cin >> thisnumber1;
cout<< "Enter a Number: ";
cin >> thisnumber2;
cin.ignore();
x = thisnumber - thisnumber1 -thisnumber2;
cout<< "" <<thisnumber<< " - " << thisnumber1 << " - " << thisnumber2 << " = " << x << "\n";
cin.get();
}}


No matter if you type D,A,M, or S, it always does the order D,M,A, and then S. Why does it do this and how can I fix it?
Use if instead of while. While is a type of loop. If just checks to see if the statement is true, and if it is, then it performs whatever action you want. Also, use single quotes around your single letters such as 's' instead of "s" because single letters are just chars instead of strings. " " is used for strings, whereas ' ' is used for chars. A string is just a bunch of chars strung together, hence why it is called a string.

See if that fixes your problem. Also, for the sake of making it easier for us to read your code, put before your code, and then at the end.

So, an example of the changes you should make:

1
2
3
4
5
if(Answer == 'S')
{
	cout << "Enter a number: ";
	.etc
}
Last edited on
closed account (98qGz8AR)
the answer variable is a string, i've got it in my compiler right now trying to figure out what the problem is. i think it might be something with flushing the buffer.

@kyranstar: do you need to use any of the structure you have or would a restructured solution be ok? also
Last edited on
cin>>answer;
while(answer != "quit"){
if(answer = "a")add();
if(answer = "m")multiply();
// and so on
cin>>answer;
}


a while loop does a check on the expression (answer = "a") and executes the block of code if the expression is true... however if you never modify the expression within a while loop, it will repeatedly execute the code forever, such that a while loop can be used like a for loop...
if you did

int x=9;
while(x !=10 ){
cout<<"x is not equal to 10"<<endl;
}
you would get a neverending output of "x is not equal to 10".... however if you did

int x=9;
while(x!=10){
cout<<"x is not equal to 10<<endl;
x++; //or x = x+1;
}

then only one line of "x is not equal to 10" would be printed,..... you catch my drift?
closed account (98qGz8AR)
this is the simplest way to run the calculator program. i don't know if you need to have the answer as a string, but it's a lot easier for a program this simple to have it as a char.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout<< "CALCULATOR\n";
cout<< "Hello, Press A to add, S to subtract,\n";
cout<< "D to Divide, and M to multiply.\n";
cout<< "What would you like to calculate? (D, M, A, or S)\n";
cin>> answer;

if (answer == 'm' || answer == 'M'){
cout<< "Enter a Number: ";
cin >> first;
cout<< "Enter a Number: ";
cin >> second;
cout<< "Enter a Number: ";
cin >> third;
cin.ignore();
x = first*second*third;
cout<< "" <<first<< " times " << second << " times " << third << " = " << x << "\n";
cin.get();
}
Your problem occurs because you have semicolons ' ; ' after your while statements.

1
2
3
4
while (Answer == "D") /*THE CODE PUT HERE IS CONNECTED TO THE WHILE LOOP*/ ;
{
//This is not part of the while loop and will run every time
}


1
2
3
4
while (Answer == "D")
{
//With the semicolon gone this is now the while loop
}
Topic archived. No new replies allowed.