【问题标题】:Using do while loop to write a program that will ask user to key in again if type character [duplicate]使用do while循环编写一个程序,如果键入字符,将要求用户再次键入[重复]
【发布时间】:2014-09-22 21:56:38
【问题描述】:

这是一个随机猜谜游戏。游戏的秘密数字是由用户输入的从最小值到最大值。猜测者要求猜测秘密数字,最后应该询问他们是否想再玩一次。如果猜测者太高或太低,还必须有多种打印输出选项。我是编程新手,这让我很困惑。任何帮助,将不胜感激。

现在在 do while 循环中键入字母后,我无法让用户再次输入 cin。因此,任何关于最佳途径的建议都将不胜感激。

cout<<"\nEnter ur amount to play game: ";
cin>> amount;
do{
    cout<<"\n\n"<< name<< ", What is ur betting amount? ";
    cin>> bet_amount;

    if(bet_amount > amount)
    cout << "Your betting amount is more than your current balance!\n";

    else if(!(cin>> bet_amount)){
            cin.clear();
            cin.ignore();
        }

}while(bet_amount > amount || !(cin>> bet_amount));

while(1){
// Read in guess
cout<< "\n\nEnter a guess to bet: ";
cin >> guess;
...

【问题讨论】:

  • 你为什么使用 cin>> bet_amount) in while(bet_amount > amount && !(cin>> bet_amount));
  • @Sorcler 嗨,确保用户输入的是数字,而不是字母
  • 你能指定 bet_amount > amount && !(cin>> bet_amount) 如何检查输入的值是否为数字??
  • @Sorcler 这是示例输出:i59.tinypic.com/jb35o9.png
  • 流上的失败位是粘性的。您需要使用ignore 成员函数丢弃不是有效数字的输入,然后使用clear 成员函数重置失败位。

标签: c++


【解决方案1】:

尝试以下更改-

#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int amount,bet_amount;
    string name;

    cout << "Enter your name and betting amount!\n";
    cin >> name >> amount;
    cout << "Your current balance is " << name << " " << amount << endl;
    cout << "\n *** To stop play press CTRL + c *** \n\n";

    while(1){
            cout << "Enter the amount to bet! : ";
            cin >> bet_amount;

            if(bet_amount == 0){
                    cin.clear(); // takes care of resetting the fail bits
                    cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); //removes any wrong input left in the stream
                    cout << "Please enter the numerical value(Amount)!\n";
                    continue;
            }
            if(bet_amount > amount)
                    cout << "Your betting amount is more than current balance!\n";
            else if(bet_amount == amount)
                    cout << "Your betting amount is equal to current balance!\n";
            else
                    cout << "Your betting amount is less than current balance!\n";
    }
    return 0;
}

没有cin.clearcin.ignorecin in while(1) 下次无法扫描用户的输入。

样本输出-

root@ubuntu:~/c/basics# ./a.out 
Enter your name and betting amount!
ABC
50
Your current balance is ABC 50

 To stop play press CTRL + c  

Enter the amount to bet! : 25
Your betting amount is less than current balance!
Enter the amount to bet! : w
Please enter the numerical value(Amount)!
Enter the amount to bet! : q
Please enter the numerical value(Amount)!
Enter the amount to bet! : 50
Your betting amount is equal to current balance!
Enter the amount to bet! : 75
Your betting amount is more than current balance!
Enter the amount to bet! : ^C // when you press ctrl+c program terminates!

【讨论】:

  • 你好,不是猜数量,是为了确保如果输入像'j'这样的字符,它会要求用户再次输入cin..
  • 这部分程序是确保用户输入数字,而不是字母。如果用户输入字母,它会要求用户再次输入,直到输入的是数字
  • @AvIx 试试新程序!
猜你喜欢
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2013-07-09
  • 1970-01-01
相关资源
最近更新 更多