Calculator won't compile

Calculator won't compile. Here is the code: (any help is would be great)





#include <iostream>

using namespace std; // so we dont have to use std anymore

int main () // start of function

{
char quit;
char op; // operator
double num1; // first number
double num2; // second number
double num3;

do
{
cout << "welcome to my simple calcualtor\n";

do
{
cout << "enter number\n";
cin >> num1;
cout << "enter operator ( + - * / or =)'\n";
cout << "enter number\n";

// begin switch
switch ( op )

{ case '+' : cout<< " total is " << num3 = num1 + num2;
double num3 = num1;
break; // addition

case '-': cout<< " total is " << num3 = num1 - num2;
double num3 = num1;
break; // subtration

case '*': cout<< " total is " << num3 = num1 * num2;
num3 = num1;
break; // mutilpication

case'/': cout<< "total is" << num3 = num1 / num2;
num3 = num1;
break; // division

case '=': cout<< "answer is" << num3 << endl;
break; // equals

default: break; // anything else
}



} while (op != '=');


cout << "Quit? (Y or N) /n";
cin >> quit;
} (while quit != 'Y');

return 0;
}

updated code still won't compile:



#include <iostream>

using namespace std; // so we dont have to use std anymore

int main () // start of function

{
char quit; // quit
char op; // operator
double num1; // first number
double num2; // second number
double num3; // third number

do
{
system: " cls " ;
cout << "welcome to my simple calcualtor\n"; // welcome screen

cout << "enter number\n"; // user enters number
cin >> num1; // input number

do
{
cout << "enter operator ( + - * / or =)'\n"; // user enters operator
cin >> op; // input operator
cout << "enter number\n"; // user enters another number
cin >> num2; // input another number

// begin switch
switch ( op )

{ case '+' : cout<< " total is " << num3 = num1 + num2;
double num3 = num1;
break; // addition

case '-': cout<< " total is " << num3 = num1 - num2;
num3 = num1;
break; // subtration

case '*': cout<< " total is " << num3 = num1 * num2;
num3 = num1;
break; // mutilpication

case'/': cout<< "total is" << num3 = num1 / num2;
num3 = num1;
break; // division

case '=': cout<< "answer is" << num3 << endl;
break; // equals

default: break; // anything else
}



} while (op != '='); // do loop while operator is not equals


cout << "Quit? (Y or N) /n"; // quir option
cin >> quit; // quit answer
} while (quit != 'Y'); // do loop while answer is not Y

return 0; // end main
}
We like to see errors. Most people on this site won't copy, paste and compile your code.

I did because of the number of issues I saw at first glance.

- When you type something like this:
 
case '+' : cout<< " total is " << num3 = num1 + num2;

You should have braces like this:
 
case '+' : cout<< " total is " << (num3 = num1 + num2);

So that you compiler knows what to do first, in this case it will assign the sum of num1 & num2 to num3; THEN it knows to output to std::cout.

- This:
1
2
3
cout << "Quit? (Y or N) /n";
cin >> quit;
} (while quit != 'Y');

Should be:
1
2
3
cout << "Quit? (Y or N) /n";
cin >> quit;
} while (quit != 'Y'); /*See here where I moved the brace?*/


- Where do you grab "num2" from the user?

- Where do you grab "op" from the user?

- Your Quit option should be inside of the do while loop.

- You only need to declare num3 once. So fix this case:
1
2
3
{ case '+' : cout<< " total is " << num3 = num1 + num2;
double num3 = num1;
break; // addition  

That should be enough for now. Fix those and we'll continue later.

Last edited on
Thank you. It now compiles, but doesnt work quite like I want it to. heres my new code:


#include <iostream>

using namespace std; // so we dont have to use std anymore

int main () // start of function

{
char quit; // quit
char op; // operator
double num1; // first number
double num2; // second number
double num3; // third number

do
{
system: " cls " ;
cout << "welcome to my simple calcualtor\n"; // welcome screen

cout << "enter number\n"; // user enters number
cin >> num1; // input number

do
{
cout << "enter operator ( + - * / or =)'\n"; // user enters operator
cin >> op; // input operator
cout << "enter number\n"; // user enters another number
cin >> num2; // input another number

// begin switch
switch ( op )

{ case '+' : cout<< " total is\n " << (num3 = num1 + num2);
num3 = num1;
break; // addition

case '-': cout<< " total is\n " << (num3 = num1 - num2);
num3 = num1;
break; // subtration

case '*': cout<< " total is\n " << (num3 = num1 * num2);
num3 = num1;
break; // mutilpication

case'/': cout<< "total is\n" << (num3 = num1 / num2);
num3 = num1;
break; // division



default: break; // anything else
}

cout<< "answer is" << num3 << endl;



} while (op != '='); // do loop while operator is not equals


cout << "Quit? (Y or N) /n"; // quit option
cin >> quit; // quit answer

} while (quit != 'Y'); // do loop while answer is not Y

return 0; // end main
}
Please use [code][/code] tags and describe the problem.
Thank you. It now compiles, but doesnt work quite like I want it to.


What does this mean?

What do you want it to do? What is it doing differently?


Help us help you. Don't give us vague problems and expect us to read your mind. Give us details. We're much less likely to give you a meaningful response if we have to coax the problem out of you like this.


Suggested reading:
http://www.cplusplus.com/forum/articles/1295/
Topic archived. No new replies allowed.