• 11
• 9
• 10
• 9
• 10

string input question

This topic is 4047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello! i recently finished my little project i was doing, but i have a minor problem. I ask the user for their fav video game, but if the game has multiple words, it counts that as 2 diff game names. Does anyone know how to make it so the user has put spaces in the games name and have it count as one string input?? ex:
string name;

cout << "Game Name: ";
cin >> name



Share on other sites
You want to use std::getline():

string name;cout << "Game Name: ";getline( cin, name );

jfl.

Share on other sites
oh yeeeeeeeeeeeeeeah!

i totally forgot about that! :D

Thanx a lot man!!!

Share on other sites
Just to add to jfl's post (if I remember correctly) getline leaves the delimiter in the stream, so you will have to strip it out if you want to capture more input after the call

Share on other sites
yeah, this is what i added :D

std::cout << "enter name: ";std::cin >> std::ws;getline(std::cin, game_title);

Share on other sites
Quote:
 Original post by CmpDevJust to add to jfl's post (if I remember correctly) getline leaves the delimiter in the stream, so you will have to strip it out if you want to capture more input after the call

Nope. It is extracted and not stored. (This is usually the behaviour you want.)

Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by CmpDevJust to add to jfl's post (if I remember correctly) getline leaves the delimiter in the stream, so you will have to strip it out if you want to capture more input after the call

Nope. It is extracted and not stored. (This is usually the behaviour you want.)

Rrr that's correct yes. It was get I was thinking off.
Sorry