|
| CheesyBeefy (80) | ||||
| Ok, in my C++ Console Project, I am prompting the user to make a choice of multiplication, addition, subtraction, or division, and then enter two numbers to perform the selected operation. But I am having some syntax errors I am not sure how to fix. Here is my code: [code=cpp] #include "stdafx.h" #include <iostream> #include <string> #include <time.h> #include <stdio.h> using namespace std; void Wait(int seconds) { clock_t endwait; endwait = clock () + seconds * CLK_TCK ; while (clock() < endwait) {} } int main( void ) { //variables int number1; int number2; int answer; char choice; char keepplaying = "y"; //code while(keepplaying == "y") { cout << "Enter your choice of math (+ , - , /, x): "; cin >> choice; if(choice = "+") { cout << "Enter two numbers to add together:" << endl; cin >> number1; cout << "+" << endl; cin >> number2; cout << endl; answer = number1 + number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else if(choice = "-") { cout << "Enter two numbers to subtract:" << endl; cin >> number1; cout << "-" << endl; cin >> number2; cout << endl; answer = number1 - number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else if(choice = "/") { cout << "Enter two numbers to divide:" << endl; cin >> number1; cout << "/" << endl; cin >> number2; cout << endl; answer = number1 / number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else if(choice = "x") { cout << "Enter two numbers to add together:" << endl; cin >> number1; cout << "x" << endl; cin >> number2; cout << endl; answer = number1 * number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else { cout << "That is not a valid option." << endl << endl << endl << endl; } cout << "Play again? (Y/N)" ; cin >> keepplaying; } } [/code] And here is the build log.
I am using Windows with Microsoft Visual C++ 2008 Express Edition. This is a console project. | ||||
| LacViet (82) | |||
I am running MinGW IDE, but I make the following changes to the code and it work. hehe.
| |||
| dgirdhar (6) | |||
| I compiled and following changes in visual studion 6.0, Please check. #include "stdafx.h" #include <iostream> #include <string> #include <time.h> #include <stdio.h> using namespace std; void Wait(int seconds) { clock_t endwait; endwait = clock () + seconds * CLK_TCK ; while (clock() < endwait) {} } int main( void ) { //variables int number1; int number2; int answer; char choice; char keepplaying = 'y'; //code while(keepplaying == 'y') { cout << "Enter your choice of math (+ , - , /, x): "; cin >> choice; if(choice = '+') { cout << "Enter two numbers to add together:" << endl; cin >> number1; cout << "+" << endl; cin >> number2; cout << endl; answer = number1 + number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else if(choice = '-') { cout << "Enter two numbers to subtract:" << endl; cin >> number1; cout << "-" << endl; cin >> number2; cout << endl; answer = number1 - number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else if(choice = '/') { cout << "Enter two numbers to divide:" << endl; cin >> number1; cout << "/" << endl; cin >> number2; cout << endl; answer = number1 / number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else if(choice = 'x') { cout << "Enter two numbers to add together:" << endl; cin >> number1; cout << "x" << endl; cin >> number2; cout << endl; answer = number1 * number2; cout << endl << "Calculating..."; Wait( 5 ); cout << " ... Done Calculating! Your answer is... " << answer << endl << endl; system("PAUSE"); } else { cout << "That is not a valid option." << endl << endl << endl << endl; } cout << "Play again? (Y/N)" ; cin >> keepplaying; } } | |||
| QWERTYman (351) | |||
| @dgirdhar: Please put in a [c0de] [/c0de] tag, but spell code correctly. @cheesebeefy:this
EDIT: char 'keepplaying' should be char keepplaying == 'y', as opposed to "y". Note the diff.Same for everywhere else you used double quotes to declare/compare a char type variable. | |||
Last edited on | |||
| CheesyBeefy (80) | |
| Thank you everyone! That fixed it. And I'll try out that switch statement QWERTY. :) | |
This topic is archived - New replies not allowed.
