Abdul Ghani
Abdul Ghani

Reputation: 83

cin.getline ignoring first word?

I'm having a problem with cin.getline();. cin.getline is ignoring the first word

cout << endl << "Insert the reason: " << endl <<"> ";
cin >> Reason;
cin.getline(Reason,200);

I think thats how you do it anyway. I'm not sure if the problem is here, or when I input it into a file:

myfile.open("BudgetLog.txt", ios::app);
myfile << endl << "Time: " << Date << " " << Time << "\t\tAmount taken: " << Amount << "\t\tReason: " << Reason << " \t\tAmount left: " << CurrentAmount <<endl;
myfile.close();

Upvotes: 2

Views: 1454

Answers (2)

MOHAMED FATHEI
MOHAMED FATHEI

Reputation: 468

use 1 instruction only

cin >> Reason;

or

cin.getline(Reason,200);

Upvotes: 0

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143189

Well, you first read the reason with operator>> (the first word), then you read the rest (overwriting the result of >>) with getline().

Upvotes: 3

Related Questions