🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

You guys must hate me by now

Started by
2 comments, last by Jonny Go 24 years, 3 months ago
Yes, I ask alot of questions, but thats just how ya get stuff done right? char string[31]; cout << endl<< "Enter a few words here"; cin.getline(string, 31); cout <<endl<< string; Whats wrong with this? When it compiles, it acts as if cin.getline isnt even there...
Its all fun and games till someone gets pregnant.
Advertisement
Arrgghh..

that didnt come out right


the last line of code should say:

cout << endl << string;


Its all fun and games till someone gets pregnant.
If I''m not mistaken, *a* problem is that you''re having it get up to 31 characters when you only should be getting up to 30. Don''t forget that your buffer size must account for the null terminator. So, set up your string to MAX_STR_LEN + 1, say, then grab MAX_STR_LEN characters.
The most likely case is that the newline character ''\n'' is left in the buffer; therefore, when get() and getline() functions are called, they appear to be skipped.
To remedy this use he ''ignore()'' function:

cin.ignore(n,delim);

Where:
n = number of characters to skip; stops after delim
delim = what''s to be removed

Add this line:
cout << endl<< "Enter a few words here";
cin.ignore(31, ''\n'');
cin.getline(string, 31);

Use it after every cin.get* function to be safe. Write a function that does both to save time and errors.

This topic is closed to new replies.

Advertisement