Spaces?

Hey working on a little program here and when i enter a full name like BOB JONES it comes up with name BOB rank JONES and if i enter just BOB for a name and Master Sgt for rank it just has Master. How do I fix this? So there can be spaces?

Comments

: Hey working on a little program here and when i enter a full name like BOB JONES it comes up with name BOB rank JONES and if i enter just BOB for a name and Master Sgt for rank it just has Master. How do I fix this? So there can be spaces?: : [code]: #include: #include: #include: : main (void): : : : : {: std::string name;: std::cout << "Enter name: ";: std::cin >> name;: : std::string rank;: std::cout << "Enter Rank: ";: std::cin >> rank;: : system("CLS");: : std::cout << "Your name is: " << name << std::endl;: : std::cout << "Rank of " << name << " is " << rank << std::endl;: : return 0;: }: [/code]:

Hi,

You could use [b]cin.getline( , )[/b] in order to include spaces. Try out the following:

[b][red]This message was edited by mfeik at 2002-10-20 22:17:59[/red][/b][hr]: Hey working on a little program here and when i enter a full name like BOB JONES it comes up with name BOB rank JONES and if i enter just BOB for a name and Master Sgt for rank it just has Master. How do I fix this? So there can be spaces?: : [code]: #include: #include: #include: : main (void): : : :

But I still think [b]cin.getline( , )[/b] is the best way in order to overcome this problem. Note that you need the header file [b]#include [/b] in order to use the functions like [b]cin.getline( , )[b], [b]strcat( , )[/b].

: Hey working on a little program here and when i enter a full name like BOB JONES it comes up with name BOB rank JONES and if i enter just BOB for a name and Master Sgt for rank it just has Master. How do I fix this? So there can be spaces?: the changes that i made are just adding cin.getline(char*,int len);len is how many characters MAX that you want to pull in. btw why are you using the standard templet library?: [code]: #include: #include: #include: : main ():{: std::string name;: std::cout << "Enter name: ";: std::cin.getline(name,100);: std::string rank;: std::cout << "Enter Rank: ";: std::cin.getline(rank,100);: : system("CLS");: : std::cout << "Your name is: " << name << std::endl;: : std::cout << "Rank of " << name << " is " << rank << std::endl;: : return 0;: }