Hello all. I'm experimenting with C++ for the Visual Studio 2008. I have written this code here below the first and I'm wondering how I'm supposed to enter a code allows the user to choose if they want to do an addition subtraction, multiplication, or division problem. I got some help from someone else that says that I need something like this in here. I'm wondering where exactly this should be put in the code and where I should replace some text with the math option of my choice. Code from other person Code: #include int main(void) { int choice = 2; if((choice>10) || (choice <1)) choice = 11; switch(choice) { case 7: printf("\nCongratulations!"); break; case 2: printf("\nA"); break; case 8: printf("\nB"); break; case 11: printf("\nC"); default: printf("\nSorry, you lose.\n"); break; } My code Code: #include <iostream> #include <cstime> #include <cstdlib> using namespace std; int main() { char continueLoop = 'Y'; while (continueLoop == 'Y') { // Execute body once long startTime = time(0); int correctCount = 0; // Count the number of correct answers int incorrectCount = 0; int count = 0; // Count the number of questions while(count < 4) { //generate random numbers srand(time(0)); int number1 = rand() % 10; int number2 = rand() % 10; //if number 1 < number 2, swap if(number1 { int temp = number1; number1 = number2; number2 = temp; } //ask question "what is number1 - number2?" cout << "What is " << number1 << " - " << number2 << "?"; int answer; cin >> answer; //see if answer is correct if (number1-number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " - " << number2 << " should be " << (number1 - number2) << endl; incorrectCount++; } // Increase the count count++; //ask question "what is number1 + number2?" cout << "What is " << number1 << " + " << number2 << "?"; cin >> answer; //see if answer is correct if (number1+number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " + " << number2 << " should be " << (number1 + number2) << endl; incorrectCount++; } // Increase the count count++; //ask question "what is number1 * number2?" cout << "What is " << number1 << " * " << number2 << "?"; cin >> answer; //see if answer is correct if (number1*number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " * " << number2 << " should be " << (number1 * number2) << endl; incorrectCount++; } // Increase the count count++; //ask question "what is number1 / number2?" cout << "What is " << number1 << " / " << number2 << "?"; cin >> answer; if (number1 == 0) number1 = 1; if (number2 == 0) number2 = 1; //see if answer is correct if (number1/number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " / " << number2 << " should be " << (number1 / number2) << endl; incorrectCount++; } // Increase the count count++; } long endTime = time(0); long testTime = endTime - startTime; double correctPercent = (correctCount * 100) / (correctCount + incorrectCount); cout << "correct count is " << correctCount << "\n"; cout << "incorrect count is " << incorrectCount << "correctPercent is " << correctPercent << "\nTest time is " << testTime << " seconds\n"; // Prompt the user for confirmation cout << "Enter Y to continue and N to quit: "; cin >> continueLoop; } system("pause"); return 0; } Edit: Take note that I did post the < iostream > etc part. For some reason this site doesn't show them.
See if you want something like this : Code: #include <iostream> #include <cstime> #include <cstdlib> using namespace std; int main() { char continueLoop = 'Y'; int ProblemChoice; while (continueLoop == 'Y') { // Execute body once long startTime = time(0); int correctCount = 0; // Count the number of correct answers int incorrectCount = 0; int count = 0; // Count the number of questions while(count < 4) { //generate random numbers srand(time(0)); int number1 = rand() % 10; int number2 = rand() % 10; //if number 1 < number 2, swap if(number1 { int temp = number1; number1 = number2; number2 = temp; } ShowMenu: // Show the Menu and ask for choice : cout << "Enter you choice :\n"; cout << "1 = Subtraction Problem\n"; cout << "2 = Addition Problem\n"; cout << "3 = Multiplication Problem\n"; cout << "4 = Division Problem\n"; cout << "Enter you choice number : "; cin >> ProblemChoice; switch(ProblemChoice) { case 1 : goto SubtractionProblem; break; case 2 : goto AdditionProblem; break; case 3 : goto MultiplicationProblem; break; case 4 : goto DivisionProblem; break; default : goto ShowMenu; } SubtractionProblem : //ask question "what is number1 - number2?" cout << "What is " << number1 << " - " << number2 << "?"; int answer; cin >> answer; //see if answer is correct if (number1-number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " - " << number2 << " should be " << (number1 - number2) << endl; incorrectCount++; } // Increase the count count++; continue; AdditionProblem : //ask question "what is number1 + number2?" cout << "What is " << number1 << " + " << number2 << "?"; cin >> answer; //see if answer is correct if (number1+number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " + " << number2 << " should be " << (number1 + number2) << endl; incorrectCount++; } // Increase the count count++; continue; MultiplicationProblem : //ask question "what is number1 * number2?" cout << "What is " << number1 << " * " << number2 << "?"; cin >> answer; //see if answer is correct if (number1*number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " * " << number2 << " should be " << (number1 * number2) << endl; incorrectCount++; } // Increase the count count++; continue; DivisionProblem : //ask question "what is number1 / number2?" cout << "What is " << number1 << " / " << number2 << "?"; cin >> answer; if (number1 == 0) number1 = 1; if (number2 == 0) number2 = 1; //see if answer is correct if (number1/number2 == answer) { cout << "You are correct!\n"; correctCount++; } else { cout << "Your answer is wrong.\n" << number1 << " / " << number2 << " should be " << (number1 / number2) << endl; incorrectCount++; } // Increase the count count++; } long endTime = time(0); long testTime = endTime - startTime; double correctPercent = (correctCount * 100) / (correctCount + incorrectCount); cout << "correct count is " << correctCount << "\n"; cout << "incorrect count is " << incorrectCount << "correctPercent is " << correctPercent << "\nTest time is " << testTime << " seconds\n"; // Prompt the user for confirmation cout << "Enter Y to continue and N to quit: "; cin >> continueLoop; } system("pause"); return 0; }