跳过我上次的cin输入

时间:2016-08-20 20:13:11

标签: c++ char cout cin

这是一个非常简单的程序,但我遗漏了一些东西。我被要求选择最有效的存储数据的变量,然后在用户输入此信息之后我将使用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 . . .

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

2 个答案:

答案 0 :(得分:2)

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

的内容
if (!(std::cin >> value)) { report_error(); }

测试流是否遇到错误(对于那些即将挑选答案的人:是的,我知道这个构造实际上没有通过转换到bool而是使用{{1}为流定义;但这个细节对于这个答案来说并不重要。

operator!()的问题实际上是双重的:

  1. 您的输入gnp_florida不是整数,而变量声明为753896.45。这本身并不是一个错误。但是,小数点不是整数格式的一部分,即输入将在小数点之前停止,小数点将成为下一个输入读取的字符。
  2. unsigned int被解释为整数,但它太大而不适合753896 unsigned short的类型! gnp_florida的范围很可能是unsigned short0(您可以通过打印65535std::numeric_limits<unsigned short>::min()来验证范围)。试图读取一个对于存储的变量来说太大的值会导致输入失败。
  3. 问题的解决方法是对std::numeric_limits<unsigned short>::max()使用double(显然,除了验证流处于良好状态之外):为{{读取gnp_florida 1}}将成功并提取所有角色。使用753896.45代替gnp_florida会增加在打印值时恢复所有8位数的几率。

答案 1 :(得分:0)

当程序预期为753896.45时,您为佛罗里达州的GNP键入了double,这是unsigned short类型。

当对{GNP}使用std::cin <<时,它会尝试尽可能多地提取到变量中,其他任何内容都留在缓冲区内。因为735896.45不能全部适合短路,所以其中一些留在缓冲区中。

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

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

其次,我会说最好将变量声明为尽可能接近初始化时(在这种情况下恰好在std::cin之前)。如果你这样做了,你可能会自己看到这个错误。