Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program not waiting for cin

int x=0;
string fullname = "";
float salary;
float payincrease;
float newsal;
float monthlysal;
float retroactive;
while(x<3){
    cout << "\n What is your full name?";
    cin >> fullname;
    cout << "\n What is your current salary? \t";
    cin >> salary;
    cout << "\n What is your pay increase? \t";
    cin >> payincrease;
    newsal = (salary*payincrease)+salary;
    monthlysal = newsal/12.00;
    retroactive = (monthlysal*6)-(salary/2);
    cout << "\n" << fullname << "'s SALARY INFORMATION";
    cout << "\n New Salary \t Monthly Salary \t Retroactive Pay";
    cout << "\n \t" << newsal << "\t" << monthlysal << "\t" << retroactive;
    x++;
}

My loop doesn't seem to stop for every time cin is asked, and instead instantly executes the loop 3 times on its own. How do I get it to stop when input is asked?

like image 862
user1647490 Avatar asked Sep 04 '12 22:09

user1647490


People also ask

What does Cin get mean?

The expression cin. get means call the get function of the cin object. You will learn more about functions of objects in chapter 7 so for now, just follow the syntax instructions. The cin. get function when used in a program will input the next character, which can be any character, white space or non-whitespace.

What is CIN in program?

Thus, cin means "character input". The C++ cin object belongs to the istream class. It accepts input from a standard input device, such as a keyboard, and is linked to stdin, the regular C input source. For reading inputs, the extraction operator(>>) is combined with the object cin.

Does C++ wait for CIN?

There is no specialized wait for user input function in C++ Utilize cin.

What does Cin fail mean?

cin. fail() - This function returns true when an input failure occurs. In this case it would be an input that is not an integer. If the cin fails then the input buffer is kept in an error state.


2 Answers

If the input stream isn't empty when you call cin, then cin uses the data already in the buffer instead of waiting for more from the user. You're using the extraction operator, so when cin is sending values to your variables, it skips leading whitespace in the buffer and stops on the next whitespace.

Put a breakpoint on this line:

cout << "\n What is your current salary? \t";

Run the program, and enter Bob Smith. When you hit the break point, hover your cursor over your string fullname. You'll see it stores only "Bob" not "Bob Smith". "Bob Smith" got put into the buffer, but when you use cin with the extraction operator, it skips any leading whitespace, puts the next value it finds into your variable, then stops on the next whitespace. To demonstrate this, try running this:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1,str2;
    cin >> str1;
    cin >> str2;
    cout << str1 << " " << str2 << "\n\n";
    return 0;
}

If you type in "Bob Smith", it will take your input only one time, even though you call cin twice. However, you'll see that both "Bob" and "Smith" got captured in the strings str1 and str2.

Therefore, you can conclude that cin stops populating your string fullname when it gets to the space between Bob and Smith. On your next call to cin, the buffer still contains "Smith", so instead of taking more input from the user, it attempts to fill your variable salary with "Smith". Obviously this isn't want you want to do. You can call flush and ignore on cin to wipe out the buffer before every time you use cin, or instead you could fix your logic and use getline to take in the full name, including spaces.

To fix your problem, all you need to do is use getline instead of cin >>, so replace this line:

cin >> fullname;

with this:

getline(cin,fullname,'\n');

Secondly, you're using a while loop to execute a set of actions a specific number of times. That's typically something you'd use a for loop for.

As an aside, you could also write tiny input validation loops that can help you debug or otherwise avoid attempting to put invalid input into your variables (such as "Smith" into a float). Something like this could work:

for(;;)
{
    if(cin >> salary)
        break;
    cin.clear();
    cin.ignore(INT_MAX,'\n');
}

Note that cin returns a value, so you can use it in an if statement. If it gets valid input, it will return true. If not, it will return false. To make it more explicit, you could also just use a normal call to cin without the if statement, and then check if cin.good(), which amounts to basically the same net effect. If you're not using Visual Studio and get an error about INT_MAX, you might need to #include limits.h to resolve it.

like image 51
derpface Avatar answered Sep 23 '22 13:09

derpface


That occurs if you input a char where an int is expected.

Use cin.clear(); and cin.ignore(numeric_limits<streamsize>::max(), '\n'); to limit an input to int's only.

Other than that, it won't skip if the correct data type is put in.

#include <string>
#include <iostream>
#include <limits>
using namespace std ;



int main(void)
{


    int x=0;
    string fullname = "";
    float salary;
    float payincrease;
    float newsal;
    float monthlysal;
    float retroactive;

    while(x<3)
    {
        cout << "\n What is your full name?";
        cin >> fullname;
    cin.ignore( 1000, '\n' );

        cout << "\n What is your current salary? \t";
        cin >> salary;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

        cout << "\n What is your pay increase? \t";
        cin >> payincrease;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

        newsal = (salary*payincrease)+salary;
        monthlysal = newsal/12.00;
        retroactive = (monthlysal*6)-(salary/2);
        cout << "\n" << fullname << "'s SALARY INFORMATION";
        cout << "\n New Salary \t Monthly Salary \t Retroactive Pay";
        cout << "\n \t" << newsal << "\t" << monthlysal << "\t" << retroactive;
        x++;
    }


      cout<<" \nPress any key to continue\n";
      cin.ignore();
      cin.get();

   return 0;
}
like image 21
Software_Designer Avatar answered Sep 19 '22 13:09

Software_Designer