string input question

Started by
6 comments, last by nobodynews 17 years ago
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

Advertisement
You want to use std::getline():

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



jfl.
oh yeeeeeeeeeeeeeeah!

i totally forgot about that! :D

Thanx a lot man!!!
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
yeah, this is what i added :D

std::cout << "enter name: ";std::cin >> std::ws;getline(std::cin, game_title);
Quote:Original post by CmpDev
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


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

Quote:Original post by Zahlman
Quote:Original post by CmpDev
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


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
[History]CmpDev may be thinking of Visual c++ 6.0 which had incorrect behavior with std::getline, as laid out in this article.[/History]

Edit: apparently not!

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement