2014-10-02 86 views
1

我真的需要一些關於這個問題的幫助。我的項目是與像這樣的菜單創建一個數學家教程序:運行時檢查失敗#3 - 變量沒有啓動

cout << "Select one of the following options" << endl; 
     cout << "-----------------------------------------" << endl; 
     cout << "1. [A]dd   [+]" << endl; 
     cout << "2. [S]ubtract [-]" << endl; 
     cout << "3. [M]ultiply [x]" << endl; 
     cout << "4. [D]ivide  [/]" << endl; 
     cout << "5. [Q]uit   " << endl; 

當用戶選擇增加或減少時,程序會生成2張隨機數,並拿出結果。當用戶選擇乘法或除法時,程序會再次生成第二個數字並進行數學運算。

這裏是我的代碼

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

int main() 
{ //Introduction to user 
    cout << "Welcome to Math Tutor. This program is designed to help student" << endl; 
    cout << "to learn how to do basic math such as addition, subtraction,..." << endl; 
    cout << endl; 


    //Declare variables 
    char choice; 
    int num1, num2, answer, useranswer, remainder, userremainder; 

    //Seed 2 random numbers 
    srand(static_cast<unsigned int>(time(0))); 
    num1 = rand() % 200 + 800; 
    num2 = 1 + rand() % 200; 

    do 
    { 
     //Display the menu to user and get user input 
     cout << "Select one of the following options" << endl; 
     cout << "-----------------------------------------" << endl; 
     cout << "1. [A]dd   [+]" << endl; 
     cout << "2. [S]ubtract [-]" << endl; 
     cout << "3. [M]ultiply [x]" << endl; 
     cout << "4. [D]ivide  [/]" << endl; 
     cout << "5. [Q]uit   " << endl; 
     cin >> choice; 
     cout << "Your choice is: " << choice << endl; 


     //Respond to user's selection 
     switch (choice) 
     { 

     case 'A': 
     case 'a': 
     case '1': 
     case '+': 
      answer = num1 + num2; 
      cout << "" << num1 << endl; 
      cout << "+" << num2 << endl; 
      cout << "-----" << endl; 
      cout << "Enter your answer: " << useranswer; 
      if (answer == useranswer) 
      { 
       cout << "Congratulation, Your answer is correct" << endl; 
       cout << num1; 
       cout << "+" << num2; 
       cout << "------" << endl; 
       cout << answer; 

      } 
      else 
      { 
       cout << "You answer is not correct" << endl; 
       cout << "The answer is: " << answer; 
      } 
      break; 
     case 'S': 
     case 's': 
     case '2': 
     case '-': 
      answer = num1 - num2; 
      cout << num1; 
      cout << "-" << num2; 
      cout << "------" << endl; 
      cout << "Enter your answer: " << useranswer; 
      if (answer == useranswer) 
      { 
       cout << "Congratulation, Your answer is correct" << endl; 
       cout << num1; 
       cout << "-" << num2; 
       cout << "------" << endl; 
       cout << answer; 
      } 
      else 
      { 
       cout << "You answer is not correct" << endl; 
       cout << "The answer is: " << answer; 
      } 
      break; 

      //Seed the second number again 
      num2 = 1 + rand() % 10; 

     case 'M': 
     case 'm': 
     case '3': 
     case 'x': 
      answer = num1 * num2; 
      cout << num1; 
      cout << "x" << num2; 
      cout << "------" << endl; 
      cout << "Enter your answer: " << useranswer; 
      if (answer == useranswer) 
      { 
       cout << "Congratulation, Your answer is correct" << endl; 
       cout << num1; 
       cout << "x" << num2; 
       cout << "------" << endl; 
       cout << answer; 
      } 
      else 
      { 
       cout << "You answer is not correct" << endl; 
       cout << "The answer is: " << answer; 
      } 
      break; 



     case 'D': 
     case 'd': 
     case '4': 
     case '/': 
      answer = (num1/num2); 
      cout << "  __________" << endl; 
      cout << num2 << ")" << num1 << endl; 
      cout << "Enter your answer, both the quotient"; 
      cout << "and the remainder separated by a space: " << endl; 
      cin >> useranswer >> remainder; 
      if ((useranswer == answer) && (userremainder = remainder)) 
      { 
       cout << "Congratulation, Your answer is correct" << endl; 
       cout << "  __________" << endl; 
       cout << num2 << ")" << num1 << endl; 
       cout << "The answer is: " << answer << "and the remainder is: " << remainder << endl; 
      } 
      else 
      { 
       cout << "Sorry. The correct answer is: " << answer << " and the remainder is: " << remainder << endl; 

      } 
     case 'Q': 
     case 'q': 
     case '5': 
      cout << "Exiting the program....." << endl; 
      break; 

      //Validate user's chocie 
     default: 
     { 
      cout << "Please enter a valid choice" << endl; 
      cout << "You can enter a number from 1 to 5 or" << endl; 
      cout << "A letter corresponding to the calculation that you want to do" << endl; 
      cin >> choice; 
      cout << "Your choice is: " << choice; 
     } 


     } 

     //Pause the screen 
     if (choice != 5) 
     { 
      cout << endl; 
      cin.ignore(80, '\n'); 
      cout << "Hit the enter key to continue....\n"; 
      cin.get(); 
      cout << endl; 
     } 


    } while (choice != 5); 

    return 0; 
} 

回答

0

你的問題是線

cout << "Enter your answer: " << useranswer 

useranswer是你想要的用戶輸入,所以更改這些行

cout << "Enter your answer: "; 
if(cin >> useranswer) { ... } else { ... } // handle non-integer input too 
相關問題