如何循环但拒绝非数字输入?

时间:2015-09-14 18:52:24

标签: c++

  1. 如果用户说e,如何创建一个终止程序的函数,如果用户随时按下l,我该如何循环?

  2. 如果用户输入字母而不是数字,如何让程序重新为用户输入数字?例如,当我输入blah时,程序终止。我的障碍是bool die定义:我不确定如何使用bool die来代替die(我的老师需要使用bool die。)

    < / LI>
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;
    
    bool die(const string & msg);
    
    int main() {
    
    // declaring variables:
    unsigned QUARTERS;
    unsigned DIMES;
    unsigned NICKELS;
    unsigned PENNIES;
    double total;
    
    cout << "********************************************************" << endl;
    cout << "             Welcome to Crazy Coin Counter!             " << endl;
    cout << "********************************************************" << endl << endl;
    
    // user input:
    
    //QUARTERS
    do {
        cout << "   # QUARTERS: ";
        cin >> QUARTERS;
        if (cin){
            if (QUARTERS < 1000)
                cout << "               --> Input Successful!" << endl;
        }
        else die("             --> :( Input Unsuccessful!");
    
            cout << "               You must put in less than 1000 quarters! Please try again." << endl << endl << endl;
    } while (QUARTERS >= 1000 );
    
    //DIMES
    do{
        cout << endl << "   # DIMES: ";
        cin >> DIMES;
        if (cin){
            if (DIMES < 1000)
                cout << "               --> Input Successful!" << endl;
        }
        else die("             --> :( Input Unsuccessful!");
        if (DIMES >= 1000)
            cout << "                You must put in less than 1000 dimes! Please try again." << endl << endl << endl;
    } while (DIMES >= 1000);
    
    //NICKELS
    do {
        cout << endl << "   # NICKLES: ";
        cin >> NICKELS;
        if (cin){
            if (NICKELS < 1000)
                cout << "               --> Input Successful!" << endl;
        }
        else die("             --> :( Input Unsuccessful!");
        if (NICKELS >= 1000)
            cout << "                You must put in less than 1000 nickels! Please try again." << endl << endl << endl;
    } while (NICKELS >= 1000);
    
    //PENNIES
    do {
            cout << endl << "   # PENNIES: ";
            cin >> PENNIES;
            if (cin){
                if (PENNIES < 1000)
                    cout << "               --> Input Successful!" << endl;
            }
            else die("             --> :( Input Unsuccessful!");
            if (PENNIES >= 1000)
                cout << "                You must put in less than 1000 pennies! Please try again." << endl;
        } while (PENNIES >= 1000);
    
    
    // calculations:
    
    total = (QUARTERS * 0.25) + (DIMES * 0.1) + (NICKELS * 0.05) + (PENNIES * 0.01);
    
    // output:
    
    cout << endl <<endl<< "Congrats! You have       $" << total << "      worth of coins! " << endl << endl << endl;
    
    }
    
    // function definition
    bool die(const string & msg){
    cout << "  " << msg << endl;
    exit(EXIT_FAILURE);
    
    }
    

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

while ( true ) // Loop forever
{
  cout << "Enter 'e' to exit:";
  std::string answer;
  getline(cin, answer);
  if (answer == "e")
  {
    break;  // Break out of the loop
  }
  else
  {
    cout << "\nWrong answer.\n";
    continue; // The continue would start at the top of the loop.
  }
}

通过在StackOverflow中搜索“c ++ terminate loop”,可以找到许多其他技术。

编辑1:检查数字输入
检查数字输入的最简单方法是测试输入数字的结果:
unsigned int quarters; //使用无符号,因为数量不能为负数。

cout << "Enter number of quarters: ";
if (cin >> quarters) // Input and test in same statement.
{
  cout << "Your total is " << (quarters * 0.25) << "\n";
}
else
{
  // Handle incorrect input
  cout << "Invalid input, try again.\n";
}