Hi everyone!
I am working on a project about movie database with Implemention of AVL trees. I have a problem with getline() and reusing it(using it for second time or more) for Loop! this is my code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string a_name;
int n_actors
cout << "Enter the number of Actors"<<endl;
cin>>n_actors
for (int i=0 ; i<n_actors ; i++)
{
cout<<i+1<<" .Enter the "<<" Actor\n";
actorsNode *temp;
temp=new actorsNode;
cin.get();
getline(cin,temp->a_name);
cout<<"a_name is: "<<temp->a_name<<endl;
}
look at the first code! when i input first string (temp->a_name) it works correctly but when I input the string for second time (temp->a_name) (i mean the loop execute for second time) it ignored First Letter of the string!! (I check them by outputing them right after inputing them !)
So would you plz tell me what i do ?!
any help would be appreciated.
regards;
You use cin.get(); to remove the newline character left over from the input on line 4. OK, fine so far.
std::getline reads the whole line including the new line character so if you call cin.get(); afterwards you will remove the first character on the next line instead.
You probably don't want to call cin.get(); inside the loop but before the loop.
Peter! thank you so much ... ^_^
i think my mistake was that i use cin.get() inside the for loop. in my new code, i use it before the loop and it works fine ...