如何仅在C ++中创建用户输入数字?

时间:2017-04-17 10:19:15

标签: c++ input numbers numeric-limits

到目前为止,这是我的代码:

while(bet > remaining_money || bet < 100)
    {
        cout << "You may not bet lower than 100 or more than your current money. Characters are not accepted." << endl;
        cout << "Please bet again: ";
        cin >> bet;
    }

它工作正常,但我试图弄清楚如果用户输入任何不是数字的东西,它如何循环。

当我按下一个字母或说出符号/符号时,代码就会中断。

2 个答案:

答案 0 :(得分:1)

我会使用import numpy as np import re with open('pc1.txt', 'r') as myfile: data = myfile.read() processor = re.findall(r"processor time : ([\d.]+)", data, re.IGNORECASE | re.DOTALL | re.MULTILINE) processor = [float(i) for i in processor] # we convert the matching list of strings to floats if processor: print (np.median(processor)) print (np.mean(processor)) memory = re.findall(r"memory\\%.*?: ([\d.]+)", data, re.IGNORECASE | re.DOTALL | re.MULTILINE) memory = [float(i) for i in memory] # we convert the matching list of strings to floats if memory: print (np.median(memory)) print (np.mean(memory)) boot_time = re.findall(r"BootTime ([\d]+)", data, re.IGNORECASE | re.DOTALL | re.MULTILINE) boot_time = [float(i) for i in boot_time] # we convert the matching list of strings to floats if boot_time: print (np.median(boot_time)) print (np.mean(boot_time)) std::getline来读取整行,然后只有在将整行转换为double时才会跳出循环。

std::string

答案 1 :(得分:0)

使用功能

isdigit() 
  

如果参数是十进制数字(0-9)

,则此函数返回true

别忘了

#include <cctype>