skipping over my last cin input

user5510527

this is a pretty simple program but I'm missing something. I was asked to choose variables that would be the most efficient means to store data and then after the user enters this info I am to use cout to display it. But for some reason it skips past the last cin statement and doesn't allow me to enter a char variable. I have tried using cin.ignore() before the prompt on the last question but with no luck. Here is the code:

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;
}

This is what my output looks like:

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 . . .

Please explain what's going on and thank you in advance for your help

user6739165

You typed in 753896.45 for the GNP of Florida, which is a double type, when the program expected an unsigned short.

When using std::cin << for the GNP, it tries to extract as much as it can into the variable, with anything else being left inside the buffer. So since the 735896.45 can't all fit into a short, some of it is left in the buffer.

So therefore, the next time you use std::cin << for the gender, it doesn't bother asking the user, it simply uses the digits already in the buffer and then tries to convert it to an ASCII character, which ends up being '╠'.

What I would recommend is firstly, not to use Unsigned integers just for the sake of saving one bit in storage (which is probably padded anyway). Unsigned integers create loads of other bugs, which mean that you should only use them if you have a really good reason to (not in this case).

Secondly, I would say that it is good practice to declare your variables as close as possible to when they are initialized (in this case right before the std::cin). If you had done that, you would proabably be able to see the bug yourself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related