Not allowing a user to input data

Hi, I am trying to create a program that a user types in their filing status, then types in their income. It allows the user to type in their filing status, but no matter what I try, the program always ends before allowing the user to type in their income. What am I able to do to fix this? thanks!!

int main()
{

std::cout << std::fixed;
std::cout << std::setprecision(2);
double S, J, F, H;
cout << "Please enter your filing status";
cout << "\n Enter S for Single Fillers,";
cout << "\n J for married filing Jointly,";
cout << "\n F for married filing separately, or";
cout << "\n H for head of household";
double status;
cout << "\n Status: "; cin >> status;
cout << "Please enter your income: "; int income;
cin >> income;
return 0;
Hello BB0404,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Your program is working correctly just not the way that you want.

You define double status;, but the formatted input of cin >> status; is expecting a floating point number, but you ate trying to give it a "char" which causes "cin" to fail and become unusable.

Because "cin" has failed it just skips over the cin >> income; and executes the "return".

In the future post the whole code that can be compiled and tested. This way no one has to guess at what is missing or do something different than what you did.

Consider this compared to what you wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>

int main()
{
    double S, J, F, H;  // <--- Have no idea what these variables are for. They should have better names.

    std::cout << std::fixed;
    std::cout << std::setprecision(2);

    std::cout <<
        "\n Please enter your filing status"
        "\n Enter S for Single Fillers,"
        "\n J for married filing Jointly,"
        "\n F for married filing separately, or"
        "\n H for head of household";

    char status;  // <--- Changed.
    
    std::cout << "\n Enter status: ";
    std::cin >> status;
    
    int income;  // <--- May want to consider a double.
    
    std::cout << "Please enter your income: ";
    std::cin >> income;

    return 0;  // <--- Not required, but makes a good break point.
}

Although there is nothing wrong with the way you wrote the menu. This makes it easier to visualize what the output will look like and works the same.

The blank lines break up the code and make it easier to read and follow.

Writing a line of code cout << "\n Status: "; cin >> status;is fine for the compiler, but you are not writing this file for the compiler you are writing it for someone else to read. Hiding the "cin" makes it easy to miss.

Since you are new to the language make the code easy to read and save the shortcuts for later. Understand the code first.

Just watched this video and found it very helpful. It is about an hour long, but well worth the time. https://www.youtube.com/watch?v=MBRoCdtZOYg

Andy
Topic archived. No new replies allowed.