跳过我最后的 cin 输入

skipping over my last cin input

这是一个非常简单的程序,但我遗漏了一些东西。我被要求选择最有效的存储数据方式的变量,然后在用户输入此信息后,我将使用 cout 来显示它。但出于某种原因,它跳过了最后一个 cin 语句,不允许我输入 char 变量。我曾尝试在最后一个问题的提示之前使用 cin.ignore() 但没有成功。这是代码:

using namespace std;

int main()
{
    unsigned int population;
    float avg_income, hourly_wage;
    unsigned short int students, gnp_florida;
    char gender;

    // Instructions for all answers

    cout << "For all answers don't type a comma(,). For example the number 4,598,453.00 should be listed";
    cout << " as 4598453.00\n";


    // Get user input and assign it to the variables

    cout << "What is the population of the US?: ";
    cin >> population;
    cout << "What is the average family income in the US?: ";
    cin >> avg_income;
    cout << "Give the hourly wage of 1 family member: ";
    cin >> hourly_wage;
    cout << "Enter the total number of students attending SPC: ";
    cin >> students;
    cout << "What is the total GNP of Florida?: ";
    cin >> gnp_florida;
    cout << "Enter a gender (M for male or F for female): ";
    cin >> gender;

    // Display the variable's values using cout

    cout << "These are your answers......\n ";
    cout << "The total US population is " << population << endl;
    cout << "The average family income in the US is " << avg_income << endl;
    cout << "The hourly wage of 1 person in a household is " << hourly_wage << endl;
    cout << "The number of students attending SPC is " << students << endl;
    cout << "The GNP for Florida is " << gnp_florida << endl;
    cout << "The gender you entered is " << gender << endl;


    // Make the program beep 5 times using escape sequences
    cout << "\a\a\a\a\a";

    system("pause");
    return 0;
}

这是我的输出结果:

For all answers don't type a comma(,). For example the number 4,598,453.00 should be listed as 4598453.00
What is the population of the US?: 300000000
What is the average family income in the US?: 53453.24
Give the hourly wage of 1 family member: 15.35
Enter the total number of students attending SPC: 30253
What is the total GNP of Florida?: 753896.45
Enter a gender (M for male or F for female): These are your answers......
        The total US population is 300000000
The average family income in the US is 53453.2
The hourly wage of 1 person in a household is 15.35
The number of students attending SPC is 30253
The GNP for Florida is 52428
The gender you entered is ╠
Press any key to continue . . .

请解释发生了什么,提前感谢您的帮助

您的代码的主要问题是您没有测试输入的结果:您总是需要验证您的输入是否成功之后 正在努力阅读。如果您这样做了,您会注意到 gnp_florida 的输入失败了。一旦输入失败 std::istream 对象将被置于 失败模式 并且它们不会接受任何进一步的输入,直到失败模式被 clear()ed。方便地将流转换为 bool,即,您可以使用类似

的东西
if (!(std::cin >> value)) { report_error(); }

测试流是否遇到错误(对于那些想要挑剔答案的人:是的,我知道这个构造实际上没有经过到 bool 的转换,而是使用 operator!() 为流定义;不过,这个细节对于这个答案来说并不重要)。

gnp_florida 的问题实际上有两个方面:

  1. 您输入的 753896.45 不是整数,而变量声明为 unsigned int。这本身并不是一个错误。但是,小数点不是整数格式的一部分,即输入将在小数点之前停止,小数点将成为下一个输入读取的字符。
  2. 753896 被解释为整数,但它太大而无法放入类型为 gnp_floridaunsigned short 中! unsigned short 的范围很可能是 065535(您可以通过打印 std::numeric_limits<unsigned short>::min()std::numeric_limits<unsigned short>::max() 来验证范围)。尝试读取一个对于存储它的变量来说太大的值会导致输入失败。

问题的解决方法是对 gnp_florida 使用 double(显然,除了验证流是否处于良好状态之外):读取 753896.45 for gnp_florida 将成功并提取所有字符。使用 double 而不是 float 会增加打印值时恢复所有 8 位数字的机会。

您为佛罗里达州的 GNP 输入了 753896.45,这是一个 double 类型,而程序期望的是 unsigned short

当对 GNP 使用 std::cin << 时,它会尝试尽可能多地提取到变量中,其他任何内容都留在缓冲区中。因此,由于 735896.45 不能全部放入短裤中,因此其中一些留在缓冲区中。

因此,下次您使用 std::cin << 作为性别时,它不会询问用户,它只是使用缓冲区中已有的数字,然后尝试将其转换为 ASCII 字符,最终成为 '╠'.

我首先要推荐的是,不要仅仅为了在存储中节省一位(无论如何都可能被填充)而使用无符号整数。无符号整数会产生大量其他错误,这意味着您应该只在有充分理由的情况下使用它们(不是在这种情况下)。

其次,我要说的是,最好在变量初始化时尽可能接近地声明变量(在本例中,就在 std::cin 之前)。如果您这样做了,您可能会自己看到错误。