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
7 Years
Discussion Span
Last Post by mvmalderen
Featured Replies
  • After line 19 you need to flush the input buffer of the '\n' character. [icode]cin.ignore(1000,'\n');[/icode] should do the trick. Read More

  • 1
    vmanes 1,165   7 Years Ago

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

  • 1

    >You normally don't need the ignore( ) between two input >statements of the same type. Or when the getline() is before the cin>>. Read More

  • 1

    There's an extensive thread about flushing the input stream, you can find it [URL="http://www.daniweb.com/forums/thread90228.html"]here[/URL] ... Read More

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.

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

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.