将 Getline 与 CIN 一起使用以指定用户输入

using getline with cin to specify user input

本文关键字:用户 输入 Getline CIN 一起      更新时间:2023-10-16

每当此函数中的输入类型出现错误时,它会自动将 *_cost 的值设置为 0。 为什么会这样?

void Item::setCost(string input){
float entered;
istringstream stin;
stin.str(input);
if(!(stin >> entered)){
    do{
        cout << "invalid input" << endl;
        stin.clear();
        getline(cin, input);
        stin.str(input);
        *_cost = entered;
    }
    while(!(stin >> entered));
}
else{
    *_cost = entered;
}
}

我在主函数中使用该函数,如下所示:

istringstream stin;
string input;
cout << "enter cost" << endl;
getline(cin, input);
items[i]->setCost(input);

您正在将*_cost设置为一个值,由于 if 语句,该值始终是一个必然不正确的值。
*_cost = entered行仅在程序通过其"无效输入"代码时执行。程序仅在输入不是合法值时打印"无效输入"。因此,_cost只能设置为非法值。
要解决您的问题,请将*_cost = entered放在 do-while 循环之后。

我不确定为什么你不只是使用 std::cin 直接读取数据,而不是将标准输入转换为 std::string 的实例,然后转换为 istringstream。

您需要

将第一个*_cost = entered移出do .. while块,成为它之后的第一个语句。完成此操作后,您将看到进一步的重构很有帮助,尽管不是必需的。

while(!(stin >> entered))
{
    cout << "invalid input" << endl;
    stin.clear();
    getline(cin, input);
    stin.str(input);
}
*_cost = entered;

在代码中执行*_cost = entered;时,entered无效。

我刚刚用你的初衷纠正了你的代码

bool Item::setCost(string input) {
    bool ret_val = true;
    float entered = 0.0;
    istringstream stin;
    stin.str(input);
    while ( !(stin >> entered) ) {  // loop till you read a valid input 
        if ( !stin.rdbuf()->in_avail() ) {
            ret_val = false;
            break;
        }
    }
    *_cost = entered;
    return ret_val;
}

stin.rdbuf()->in_avail()可用于获取准备从字符串流中读入的可用字符数,您可以使用它来检查字符串流是否为"空"。

例如,如果您想从 istringstream 中提取浮点数,但您得到了其他东西(失败条件),然后查看是否有任何剩余字符(即数字),您可以检查是否stin.rdbuf()->in_avail() == 0 .