Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

There are always strings tied to strings

Status
Not open for further replies.

kenjisamurai

Programmer
Mar 14, 2002
4
0
0
US
Ok I've figured out how to make a string variable. How do I get it to have a value? This little code down here produces errors at the user input line:


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


int main()
{
string stupid;

cout << &quot;Please type your name here:&quot; << endl;
cin >> stupid;
cout << &quot;\nYour name is &quot; << stupid << &quot;!&quot;;

return 0;
}
 
try using cin.getline(stupid). getline might require a second paramater that tells it the max string size so if the 1st 1 doesn't work try, cin.getline(stupid, 1024) or something. There's a bug in c++ where you gotta press enter twice when u use getline but if you do a google search, there are instructions on how to fix it.
 
Nooooo, its not working. It said something about not being able to convert string stupid to something something and more errors. Any ideas? Isn't this suppose to be one of those simple basic fundamental things like cout that doesn't require head banging?
 
cin and cout can deal with char *, but not std::string.
 
Try this:

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


int main()
{
char stupid[20];

cout << &quot;Please type your name here:&quot; << endl;
cin >> stupid;
cout << &quot;\nYour name is &quot; << stupid << &quot;!&quot;;

return 0;
}
 
That is awesome Lenky. Thanks a lot. Now if I can only figure out how to allow whitespace in the string........
 
to get whitespace (by this I assume you mean two names) you can do the following

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


int main()
{
char firstName[20],surname[20];

cout << &quot;Please type your first name here:&quot; << endl;
cin >> firstname;
cout << Please enter your second name:&quot; << endl;
cin >> surname;
cout << &quot;\nYour name is &quot; << firstname
<< &quot; &quot;<< surname << &quot;!&quot;;

return 0;
}
 
lemming, that is pretty good code, but is there any possible way to read the whole line in one go? For example:

What is your name?
Joe Shmo

and then you can assign Joe Shmo to the string. This is easily done in Java, and yet I find it nearly impossible in C++??? How can this be? I'm not even talking about string being an array of chars, but rather a string as made by the string class. Any help would be greatly appreciated.
 
kenji, I was having the same issues. The solution thankfully is quite simple.

Change the iostream.h in the inclusion section to iostream :) getline(cin, string) will work after that. However, note: I have problems with this getline statement after importing numbers via cin anywhere else in the program eg: cin >> double. I found that using getline(cin,string) skips and it will not allow for input. If you find this is the case, and you're doing a simple question and answer, type in : getline(cin,string) before the question as a dummy getline of sorts, and then again after your &quot;question&quot;. Such as:

getline(cin, string);
cout >> &quot;How old are you?&quot;;
getline(cin, string);

This should work :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top