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...
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.
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...
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:
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!?