2014-10-07 52 views
0

因此,对于这个程序,用户必须输入一个从1到4的数字。目前,如果他错误地输入了一个不同的数字,程序会提醒用户,但问题与字符有关。如果用户输入除数字以外的任何类型的字符,则循环变成无限循环。如何在这种情况下验证用户输入?

我想知道这是否可能在C的介绍级别来完成++(很像目前的代码)

下面是代码

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 
#include <ctime> 
using namespace std; 

int main() 
{ 
    int firstNum, 
     secondNum, 
     subFirstNum, 
     subSecNum, 
     operNum, 
     num;    // User's input answer for operations 
    bool temBool = true; // Temporary boolean used in the while loop 
    unsigned seed;   // Random generator seed 

    // Use the time function to get a "seed" value for srand 
    seed = time(0); 
    srand(seed); 

    //Start of the loop 
    while (temBool) 
    { 

    //Random generated operands 
    firstNum = rand() % 40 + 10; 
    secondNum = rand() % 40 + 10; 

    // Set of randoms numbers for substraction where the denominator is always 
    // lower than the numerator. 
    subFirstNum = rand() % 30 + 20; 
    subSecNum = rand() % 10 + 10; 

    // Menu of Math Tutor 
    cout << "Math Tutor - Main Menu"; 
    cout << endl << endl; 
    cout << "1. Adittion\n"; 
    cout << "2. Subtraction\n"; 
    cout << "3. Multiplication\n"; 
    cout << "4. Exit Program\n"; 
    cout << endl << endl; 
    cout << "Choose your operation to practice (1-4) "; 
    cin >> operNum; 
    cout << endl << endl; 

    // Switch for the menu's options 
    switch (operNum) 
    { 
      srand(seed); 

    case 1: 
     cout << "Working with addition\n"; 
     cout << setw(3) << firstNum << "\n" 
      << "+" << secondNum << "\n" 
      << "---\n"; 
     cout << "Your answer: "; 
     cin >> num; 
      if(num == firstNum + secondNum) 
      { 
       cout << "Correct answer: " 
        << firstNum + secondNum << " Congratulations!\n"; 
      } 
      else 
      { 
       cout << "Correct answer: " 
        << firstNum + secondNum << " Sorry!\n"; 
      } 
     cout << endl << endl; 
      break; 
    case 2: 
     cout << "Working with subtraction\n"; 
     cout << setw(3) << subFirstNum << "\n" 
      << "-" << subSecNum << "\n" 
      << "---\n"; 
     cout << "Your answer: "; 
     cin >> num; 
      if(num == subFirstNum - subSecNum) 
      { 
       cout << "Correct answer: " 
        << subFirstNum - subSecNum << " Congratulations!\n"; 
      } 
      else 
      { 
       cout << "Correct answer: " 
        << subFirstNum + subSecNum << " Sorry!\n"; 
      } 
     cout << endl << endl; 
      break; 
    case 3: 
     cout << "Working with multiplication\n"; 
     cout << setw(3) << firstNum << "\n" 
      << "*" << secondNum << "\n" 
      << "---\n"; 
     cout << "Your answer: "; 
     cin >> num; 
      if(num == firstNum * secondNum) 
      { 
       cout << "Correct answer: " 
        << firstNum * secondNum << " Congratulations!\n"; 
      } 
      else 
      { 
       cout << "Correct answer: " 
        << firstNum * secondNum << " Sorry!\n"; 
      } 
     cout << endl << endl; 
      break; 
    case 4: 
     cout << "Thank you for using Math Tutor.\n\n"; 
     temBool = false; 
      break; 
    default: 
       cout << "Incorrect menu seletion. Please choose between 1 and 4.\n\n"; 
      break; 
    return 0; 
    } 
    } 
} 
+1

失败的流提取将该流引发到失败状态。你必须[**清除()**](http://en.cppreference.com/w/cpp/io/basic_ios/clear)它有另一个去(我强烈建议通过换行消耗所有数据,同时做所以,免得你编码一个完全不同的,同样烦人的无限循环)。你应该在销售点测试提取。 'if(std :: cin >> operNum)'...... – WhozCraig 2014-10-07 21:31:11

+0

尝试类似于'cin.clear();'接着'cin.ignore(INT_MAX);'在输入之前。 – RPGillespie 2014-10-07 21:37:10

回答

1

一旦流元首南部因非法开采,格式错误等,有非常小的,你可以用它做除了检测一下,确定是否清除的F ailure状态适用,如果是,则继续。有些时候它是而不是适用(例如到达EOF;那里不太好)。

事情是这样的:

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 
#include <ctime> 
using namespace std; 

int main() 
{ 
    int firstNum, 
    secondNum, 
    subFirstNum, 
    subSecNum, 
    operNum, 
    num;    // User's input answer for operations 
    bool temBool = true; // Temporary boolean used in the while loop 

    // Use the time function to get a "seed" value for srand 
    std::srand(static_cast<unsigned>(std::time(nullptr))); 

    //Start of the loop 
    while (temBool) 
    { 
     //Random generated operands 
     firstNum = rand() % 40 + 10; 
     secondNum = rand() % 40 + 10; 

     // Set of randoms numbers for substraction where the denominator is always 
     // lower than the numerator. 
     subFirstNum = rand() % 30 + 20; 
     subSecNum = rand() % 10 + 10; 

     // Menu of Math Tutor 
     cout << "Math Tutor - Main Menu"; 
     cout << endl << endl; 
     cout << "1. Adittion\n"; 
     cout << "2. Subtraction\n"; 
     cout << "3. Multiplication\n"; 
     cout << "4. Exit Program\n"; 
     cout << endl << endl; 
     cout << "Choose your operation to practice (1-4) "; 

     // test for successful extraction 
     if (!(std::cin >> operNum)) 
     { 
      // yes there are times when you actually use .eof() 
      if (std::cin.eof()) 
       break; 

      // flush out the stream through the pending newline 
      std::cin.clear(); 
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
      continue; 
     } 

     cout << endl << endl; 

     // Switch for the menu's options 
     switch (operNum) 
     { 
      case 1: 
       cout << "Working with addition\n"; 
       cout << setw(3) << firstNum << "\n" 
       << "+" << secondNum << "\n" 
       << "---\n"; 
       cout << "Your answer: "; 
       cin >> num; 
       if(num == firstNum + secondNum) 
       { 
        cout << "Correct answer: " 
        << firstNum + secondNum << " Congratulations!\n"; 
       } 
       else 
       { 
        cout << "Correct answer: " 
        << firstNum + secondNum << " Sorry!\n"; 
       } 
       cout << endl << endl; 
       break; 
      case 2: 
       cout << "Working with subtraction\n"; 
       cout << setw(3) << subFirstNum << "\n" 
       << "-" << subSecNum << "\n" 
       << "---\n"; 
       cout << "Your answer: "; 
       cin >> num; 
       if(num == subFirstNum - subSecNum) 
       { 
        cout << "Correct answer: " 
        << subFirstNum - subSecNum << " Congratulations!\n"; 
       } 
       else 
       { 
        cout << "Correct answer: " 
        << subFirstNum + subSecNum << " Sorry!\n"; 
       } 
       cout << endl << endl; 
       break; 
      case 3: 
       cout << "Working with multiplication\n"; 
       cout << setw(3) << firstNum << "\n" 
       << "*" << secondNum << "\n" 
       << "---\n"; 
       cout << "Your answer: "; 
       cin >> num; 
       if(num == firstNum * secondNum) 
       { 
        cout << "Correct answer: " 
        << firstNum * secondNum << " Congratulations!\n"; 
       } 
       else 
       { 
        cout << "Correct answer: " 
        << firstNum * secondNum << " Sorry!\n"; 
       } 
       cout << endl << endl; 
       break; 
      case 4: 
       cout << "Thank you for using Math Tutor.\n\n"; 
       temBool = false; 
       break; 
      default: 
       cout << "Incorrect menu seletion. Please choose between 1 and 4.\n\n"; 
       break; 
       return 0; 
     } 
    } 
} 
+0

这工作得很好。谢谢!我现在假设教授不希望我把它放到程序中,因为它需要添加我还不熟悉的代码。我将学习这些代码。 – RufioLJ 2014-10-07 21:48:49

0

环绕开关与if语句

if (operNum==1||operNum==2||operNum==3||operNum==4){ 
    ... 
} 
+0

咦?因为覆盖'switch'中的那些不够好? – WhozCraig 2014-10-07 21:34:27

0

我的组织对这个唯一的目的下面的代码。

#include <cctype> 
#include <cstdlib> 
#include <string> 

/** 
* @brief function returns the integer value of the string entered, negative 
     numbers in the input string are not allowed. First non-digit character 
     (including minus sign (-)) terminates parsing of the input string 
* @exception throws no exception, return value for every input 
* @param[in] input string containing the response of the user 
* @return <int> errorValue = -1, non-negative value is the response of the user 
*/ 
int menuResponse(std::string input) 
{ 
    int response = 0; 
    int length = 0; 
    response = atoi(input.c_str()); // <--- magic happens here 
    // check for an error in the 1st character itself, only source of false 0 
    // as a response value 
    if (isdigit(input[0])) 
    { 
     return response; 
    } 
    else 
    { 
     return -1; 
    } 
}