0

this simple cout statement and i do not know why. the outputs are mixed up and i get some random numbers i didnt even input.please help

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

int main()

{
	
	string Pname, CEmployer;
	int age;
	

	cout<<"Patients Name:"<<endl;
	getline (cin, Pname);


	cout<<"Age:"<<endl;
	cin>>age;

	cout<<"Current Employer"<<endl;
	getline (cin, CEmployer);




cout<<Pname<<endl;
cout<<age<<endl;
cout<<CEmployer;


};
5
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by mvmalderen
1

After line 19 you need to flush the input buffer of the '\n' character. cin.ignore(1000,'\n'); should do the trick.

Votes + Comments
very helpful
0

thanks alot!!
would i have to flush the input buffer after every other
time so it wont happen again?

1

When a cin >> is followed by getline( ), you will need to use the ignore( ) function in between. You normally don't need the ignore( ) between two input statements of the same type.

Votes + Comments
thanks for the help
1

>You normally don't need the ignore( ) between two input
>statements of the same type.

Or when the getline() is before the cin>>.

Votes + Comments
thanks for the help
0

>You normally don't need the ignore( ) between two input
>statements of the same type.

Or when the getline() is before the cin>>.

But when you do such reading in a loop, what comes before what?

while ( getline( cin, str ) )
{ 
      cin >> num;
      //do something with them

     //oops, need the cin.ignore( ) here
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.