Sunday, August 2, 2009

How do i keep the function getline for assigning to the next variable in C++?

when i run the program if u put a space in any of the strings it assigns the stuff after the space to the next string. how do i stop it so that the entire phrase is one and not two?





Code:


#include %26lt;iostream.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string%26gt;


using namespace std;


string adja;


string namea;


string placea;


string adjb;


string thinga;


string nouna;





int main()


{


cout%26lt;%26lt;"Name a word that fits the description giving."%26lt;%26lt;endl;


cout%26lt;%26lt;"an adjetive"%26lt;%26lt;endl;


getline(cin,adja);


cout%26lt;%26lt;"a name"%26lt;%26lt;endl;


getline(cin,namea);


cout%26lt;%26lt;"a place"%26lt;%26lt;endl;


getline(cin,placea);


cout%26lt;%26lt;"a adjetive"%26lt;%26lt;endl;


getline(cin,adjb);


cout%26lt;%26lt;"a thing/noun"%26lt;%26lt;endl;


getline(cin,thinga);


cout%26lt;%26lt;"a thing/noun"%26lt;%26lt;endl;


getline(cin,nouna);


cout%26lt;%26lt;adja%26lt;%26lt;" "%26lt;%26lt;namea%26lt;%26lt;" was walking home from "%26lt;%26lt;placea%26lt;%26lt;" when a "%26lt;%26lt;adjb%26lt;%26lt;endl;


cout%26lt;%26lt;thinga%26lt;%26lt;" threw a "%26lt;%26lt;nouna%26lt;%26lt;" on top of "%26lt;%26lt;adja%26lt;%26lt;" "%26lt;%26lt;namea%26lt;%26lt;endl;


system("PAUSE");


return 0;


}

How do i keep the function getline for assigning to the next variable in C++?
It seems to work fine for me, and thats exactly what getline is for. What compiler are you using?
Reply:I hardly do anything in c++ anymore (C and C# mostly), but there are different ways to retrieve user input, some will split at the space, others will read until the end of the line. In C for example you have getc, read, etc.





I'd look at it from that angle.


No comments:

Post a Comment