You guys helped me a lot last time I posted so now that I have trouble i again turn to you :).Ok can someone tell me with what can I substitute the following code.It is supposed to get some lines and put them in strings.

system("cls");
string user,date,title,description;
cout<<"Please Enter Date:"<<endl;
getline(cin,date);
cout<<"Please Enter Title:"<<endl;
getline(cin,title);
out<<"Please Enter Description"<<endl;
getline(cin,description);
cout<<"Please enter your username to confirm"<<endl;
getline(cin,user);
user=Security::code(user,user);
file_handling::add_entry(date,title,description,user+".usdat");

The problem that really drives me crazy is that this code works perfectly when I compile it separately but when I try to put in my main program,instead of writing "Please Enter Date:"
then letting me type and then writing "Please Enter Title:" the program writes
"Please Enter Date:"
"Please Enter Title:"
and then lets me type which results in incorrect data being written in the database.
Any help with solving this problem will be appriciated.

Recommended Answers

All 5 Replies

what do you mean in this part?

The problem that really drives me crazy is that this code works perfectly when I compile it separately but when I try to put in my main program,instead of writing "Please Enter Date:"
then letting me type and then writing "Please Enter Title:" the program writes
"Please Enter Date:"
"Please Enter Title:"

the "please enter date" and "title" appears at the same time??

try using fflush(stdin); <<after the getline part.

Yes mate the trouble is just that "please enter date" and "title" appear at the same time and the program lets me type a string for only one of them.I tried to do what u said fflush(stdin); but it didn't do any good they still appear at the same time.Maybe the problem is somewhere in my other code because when i compile it separately it works.
Please take a look:

for(;;)
 {
 char n;
 n=user_menu();
 switch(n)
{
 case '5':return 0;
 case '4':break;
 case '1':{
 system("cls");
 string user,date,title,description;
 cout<<"Please Enter Date:"<<endl;
 getline(cin,date);
 cout<<"Please Enter Title:"<<endl;
 getline(cin,title);
 cout<<"Please Enter Description"<<endl;
 getline(cin,description);
 cout<<"Please enter your username to confirm"<<endl;
 getline(cin,user);
 user=Security::code(user,user);
 file_handling::add_entry(date,title,description,user+".usdat");
 }break;

Thanks in advance.

It works. I don't see the line he suggested in your code, so I don't know where you put it. The only change I'd make to carnage's advice is to put that line BEFORE the first getline, not after. You almost certainly have something "left over" in your cin input stream when you reach the first getline statement. His line of code will remove that and make that cin stream empty, which is what you want. Just stick his line in before the first getline:

fflush(stdin);

Now I get it.Thanks mates I didn't know what exactly did fflush(stdin); mean so I put it in the wrong place.Now that I put it infront of the first getline all works perfectly well.Thank you very much guys.You are live savers.

oh,was that BEFORE the getline?

well,go with what works.

although the fflush is not preferred gurus

try to do the right thing
see how HERE

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.