gpt4 book ai didi

c++ - C++中的输入限制和错误检查

转载 作者:行者123 更新时间:2023-12-02 10:36:35 25 4
gpt4 key购买 nike

我的以下C++程序有问题。它根据用户输入的信息,使用Widmark公式计算并打印出血液中的酒精浓度。存在一些输入错误检查,例如,在询问他们喝了哪种饮料之后是否输入了随机字符,或者在询问重量之后是否输入了大于400的任何数字。然后出现一条错误消息(这是故意的),并要求用户再次输入。

但是,它们是输入错误检查的问题,例如,如果在询问性别后输入x> 1个字符,则错误消息将出现x次(应该只出现一次)。如果要求输入重量后输入了任何非整数字符,则错误消息将无限次出现,从而导致无休止的循环(输入非浮点值后,公升的公升与醉酒一样)。

有什么方法可以限制输入,以便错误消息在任何无效输入之后仅在之后出现一次?

#include<iostream> // for std::cin and std::cout
#include<iomanip> // for std::fixed and std::setprecision

float BAC (char gender, int weight, char drink, float litres) // function to calculate blood alcohol concentration
{
// local variables
float bac, rate, vol; // blood alcohol concentration, widmark rate, volume percentage

// determine widmark rate
if (gender == 'm' || gender == 'M') // not case-sensitive
{
rate = 0.68;
}
if (gender == 'f' || gender == 'F')
{
rate = 0.55;
}
if (gender == 'c' || gender == 'C')
{
rate = 0.75;
}

// determine volume percentage
if (drink == 'b' || drink == 'B')
{
vol = 5;
}
if (drink == 'w' || drink == 'W')
{
vol = 12;
}
if (drink == 's' || drink == 'S')
{
vol = 38;
}

// calculating blood alcohol concentration
bac = ((litres * 1000) * (vol / 100) * 0.8) / (weight * rate);
// ((litres * 1000) * (vol / 100) * 0.8) --> formula for alcohol mass
// (litres * 1000) --> conversion to millilitres

return bac;
}

int main () // main function for user input/output
{
// variables to be entered
char gender, drink;
int weight;
float litres;

// getting user input
std::cout << "Are you male (m), female (f), or a small child (c)?" << std::endl;
do
{
std::cin >> gender;
if (gender != 'm' && gender != 'M' && gender != 'f' && gender != 'F' && gender != 'c' && gender != 'C') // check input
{
std::cout << "\nInvalid input: Please enter 'm', 'f' or 'c' (capital letters are allowed)." << std::endl;
}
}
while (gender != 'm' && gender != 'M' && gender != 'f' && gender != 'F' && gender != 'c' && gender != 'C');
// repeat as long as valid input is entered

std::cout << "\nWhat is your weight (in kg)?" << std::endl;
do
{
std::cin >> weight;
if (weight < 10 || weight > 400)
{
std::cout << "\nInvalid input: Please enter a number bigger than or equal to 10 and smaller than or equal to 400." << std::endl;
}
}
while (weight < 10 || weight > 400);

std::cout << "\nWhich beverage did you drink? beer: (b), wine: (w), schnapps: (s)" << std::endl;
do
{
std::cin >> drink;
if (drink != 'b' && drink != 'B' && drink != 'w' && drink != 'W' && drink != 's' && drink != 'S')
{
std::cout << "\nInvalid input: Please enter 'b', 'w' or 's' (capital letters are allowed)." << std::endl;
}
}
while (drink != 'b' && drink != 'B' && drink != 'w' && drink != 'W' && drink != 's' && drink != 'S');

std::cout << "\nHow many litres did you drink?" << std::endl;
do
{
std::cin >> litres;
if (litres < 0 || litres > 10)
{
std::cout << "\nInvalid input: Please enter a number bigger than or equal to 0 and smaller than or equal to 10." << std::endl;
}
}
while (litres < 0 || litres > 10);

// print the result
std::cout << std::endl << std::endl << "Your blood alcohol concentration is: "
<< std::fixed << std::setprecision(2) // show only 2 decimal places after the dot
<< BAC (gender, weight, drink, litres) // function call
<< " thousandths." << std::endl;

return 0;
}

最佳答案

我确定您已经意识到,您的问题是读取每个输入后std:cin流中的“剩余字符”。现在,尽管您实际上无法“刷新”一个input stream,但是可以在每次输入之前使用如下代码清除所有“等待中”的输入字符:

std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin >> weight;

有关此的更多讨论,请参见此处: How do I flush the cin buffer?

您可能需要考虑的另一点是,当任何输入操作失败时会发生什么。您应该使用“无效”值初始化变量,以确保用户实际提供了良好的数据:

char gender = '?', drink = '?';
int weight = -1.0;
float litres = -1.0;

关于c++ - C++中的输入限制和错误检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60017440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com