正在尝试验证C++; 这个代码在C++中的思想是计算所有输入的数字的和。当用户输入0时,程序应停止。这部分代码按照我的预期工作,但我想包括一个变体,它可以识别输入的字符与浮点数不同,在计算中忽略它,并允许用户继续输入浮点数。此时,输入除浮点数以外的任何内容都会停止程序

正在尝试验证C++; 这个代码在C++中的思想是计算所有输入的数字的和。当用户输入0时,程序应停止。这部分代码按照我的预期工作,但我想包括一个变体,它可以识别输入的字符与浮点数不同,在计算中忽略它,并允许用户继续输入浮点数。此时,输入除浮点数以外的任何内容都会停止程序,c++,validation,while-loop,C++,Validation,While Loop,我知道有一个“if(!(cin>>numb))”条件,我尝试在代码中的不同位置解析它,但我不知道如何强制程序忽略这些无效输入。我将非常感谢任何帮助 #include <iostream> #include <stdlib.h> using namespace std; float numb; float sum=0; int main() { cout << "This app calculates the sum of all entered

我知道有一个“if(!(cin>>numb))”条件,我尝试在代码中的不同位置解析它,但我不知道如何强制程序忽略这些无效输入。我将非常感谢任何帮助

#include <iostream>
#include <stdlib.h>

using namespace std;

float numb; float sum=0;

int main()
{
    cout << "This app calculates the sum of all entered numbers." << endl;
    cout << "To stop the program, enter 0." << endl << endl;
    cout << "Enter the first number: ";
    cin >> numb;

    while(true)
    {
        sum += numb;

        if (numb!=0)
        {
            cout << "Sum equals: " << sum << endl << endl;
            cout << "Enter another number: ";
            cin >> numb;
        }
        else
        {
            cout << "Sum equals: " << sum << endl << endl;
            cout << "Entered 0." << endl;
            cout << "Press Enter to terminate the app." << endl;
            exit(0);
        }
    }
    return 0;
}
#包括
#包括
使用名称空间std;
漂浮麻木;浮点数和=0;
int main()
{

您有三种选择:

  • 尝试和错误:尝试读取一个浮点,如果出现错误,清除错误标志,忽略错误输入并重新读取。问题是,您不知道有多少输入将被忽略
  • 读取字符串:读取空格分隔的字符串,尝试使用
    stringstream
    转换字符串,并在出错时忽略完整字符串。问题是,如果输入以有效浮点开头,但随后包含无效字符(例如12X4),则将忽略无效部分(例如X4)
  • 控制解析:读取空格分隔的字符串,尝试使用转换字符串,并检查成功读取的字符串的所有字符
这里是第二种方法,使用稍微重组的循环,因此0条目将导致退出循环而不是整个程序:

string input;  
while(cin >> input)
{
    stringstream sst(input); 
    if (sst>>numb) {
        sum += numb;
        cout << "Sum equals: " << sum << endl << endl;
        if (numb==0)
        {
            cout << "Entered 0." << endl;
            break;  // exits the while loop 
        }
        cout << "Enter another number: ";
    }
    else 
    {
        cout << "Ignored entry "<<input<<endl; 
    }
}
cout << "Press Enter to terminate the app." << endl;
字符串输入;
同时(cin>>输入)
{
stringstream sst(输入);
如果(sst>>麻木){
sum+=麻木;

cout读取失败后,您必须清除failbit。之后,您可以将无效内容读入字符串(您只需忽略该字符串)。此函数将读入值并将其相加,直到遇到
0
或输入流结束

int calc_sum_from_input(std::istream& stream) {
    int sum = 0; 
    // If it couldn't read a value, we just read the thing into here
    std::string _ignored;
    while(stream) // Checks if the stream has more stuff to read
    {
        int value;
        if(stream >> value) 
        {
            if(value == 0) // Exit if it read the value 0
                break;
            else 
                sum += value; // Otherwise update the sum
        }
        else {
            // Clear the failbit
            stream.clear(); 
            // Read ignored thing
            stream >> _ignored;
        }
    }
    return sum; 
}
逻辑基本上是:

  • 将初始和设置为0
  • 检查是否有东西要读
    • 如果有,请尝试读取一个值
    • 如果成功,请检查该值是否为0
      • 如果为0,则退出并返回总和
      • 否则,将该值添加到总和
    • 否则,清除failbit(以便可以再次读取内容),并将错误值读入字符串(该字符串将被忽略)
  • 否则,返回值

步骤1:测试以确认
cin>>numb
在读取
float
if(cin>>numb){使用numb}否则{清除错误标志并丢弃错误数据}
事实上,您走的路是对的。请看一下,但请注意倾向于最大流大小的评论……非常感谢!这正是我要找的!
int calc_sum_from_input(std::istream& stream) {
    int sum = 0; 
    // If it couldn't read a value, we just read the thing into here
    std::string _ignored;
    while(stream) // Checks if the stream has more stuff to read
    {
        int value;
        if(stream >> value) 
        {
            if(value == 0) // Exit if it read the value 0
                break;
            else 
                sum += value; // Otherwise update the sum
        }
        else {
            // Clear the failbit
            stream.clear(); 
            // Read ignored thing
            stream >> _ignored;
        }
    }
    return sum; 
}