reading whitespaces in input

I am using 'string' header in program, the problem is that the function cin.getline() uses char* as its first argument rathar than a string class object... I also tried using getline(cin , string) function but I am encountering some problems in, for example, in this code..

getline(cin, YourGame);
for (;YourGame!="quit";)
{
MyGames.push_back(YourGame);
getline(cin, YourGame);
}

once I input 'quit', it is still waiting for another input and this time it can be anything..
here MyGames is vector<string> object...

So, I need a solution for this problem..

From the code you have given I don't particularly understand what the rest of your code might look at but I think you should use a while loop instead of a for loop.
hey, thanks for the suggestion, but that was not my concern...
this all the code...


#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
using namespace std;

int main()
{
char choice='y';
string YourGame;
vector<string> MyGames;
vector<string>::iterator i;

cout << "Enter all the games you like(enter 'quit' to exit): ";

while (choice=='y' || choice=='Y')
{
getline(cin, YourGame);
for (;YourGame!="quit";)
{
MyGames.push_back(YourGame);
getline(cin, YourGame);
}

cout << "You entered a total " << MyGames.size() << " games.\n";
cout << "Would you like to enter some more? ";
cin >> choice;
}

cout << "\nHere are all the games you just entered: \n";

for (i=MyGames.begin(); i<MyGames.end(); i++)
cout << *i << endl;

getch();

return 0;
}

it runs with no problems if I use cin >>, but I wanted to read spaces, so I can't use cin.getline with string object and getline() function is giving problems as I stated in my first post...
EDIT: Removed because I misread the question.
Last edited on
You have some unnecessary code such as while (choice=='y' || choice=='Y'), and your for loop is not helpful. Take a look at this program bearing in mind that it hasn't got everything you want in it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::string yourGame;
    std::vector<std::string> myGames;
    std::vector<std::string>::iterator it;
    
    std::cout <<"Enter all the games you like: ";
    
    while(getline(std::cin, yourGame), yourGame.compare("quit")) {
       myGames.push_back(yourGame);
       std::cout << "Enter Game: ";
    }
    
    for (it=myGames.begin(); it<myGames.end(); it++)
      std::cout << *it << std::endl;
      
    std::cin.get();
    return 0;
}
arghhh, I didnt asked for what is necessary or what is unnecessary in my code!

the only problem is, once I input 'quit' in getline(), it should come out of the loop, but it doesnt, I have to give some other input in order to getout, I even used the string.compare function, but it still the same,

Are there functions to read a line with 'string objects'??? Is there something wrong with getline() function!?
I don't have this problem. When I enter quit on its own in its own line, it gets out on the double.

-Albatross
Last edited on
arghhh, I didnt asked for what is necessary or what is unnecessary in my code!

I know you didn't, that was just a bonus input from me for your poorly written code.

Are there functions to read a line with 'string objects'??? Is there something wrong with getline() function!?

No. If there was it probably wouldn't be in the standard library.

That's the last comment i'm making in this thread.
Topic archived. No new replies allowed.