Public Group

# Setting a value to a String...

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

OK, I have just come back to programming after like forever... I still remember a good deal of it and am getting back into it. However the book/article online I am reading both say I can use a "cin>>StringVar;" approach to fill my string up until a space etc... however when I try compiling my code it gives an error for that line. Same thing with the cout<< but for that I remembered I had to use the c_str function. Am I doing something wrong? Or am I using some out of date sources, or just people who aren't all that accurate? :( So I guess what I am asking is how can I input to a string, even where cin's getline is yelling at me. edit: By the way I am using VC2005. My includes are as follows in case this is important at all. #include <iostream> #include <conio.h> #include <string.h>

##### Share on other sites
Since you don't post a code snippet i guess:

You need to change cout to std::cout (dito with cin), or put a using namespace std; after your includes in the cpp file.

If that is the cause your tutorial source is not quite up to the standard.

##### Share on other sites
Quote:
 Original post by EndurionSince you don't post a code snippet i guess:You need to change cout to std::cout (dito with cin), or put a using namespace std; after your includes in the cpp file.If that is the cause your tutorial source is not quite up to the standard.

I already put 'using namespace std;' so I am at a loss right now :|

##### Share on other sites
Check against this one:

#include <iostream>#include <string>int main (){	std::string s ;        std::cout << "Input a string: " ;	std::cin >> s ;	std::cout << "The string input is: " << s ;	return 0 ;}

##### Share on other sites
Quote:
 Original post by Xelenedit: By the way I am using VC2005. My includes are as follows in case this is important at all.#include #include #include

It is important. You must include <string>, not <string.h>. The later imports the definition of some old-fashioned (read: asciiz based) C strings, while the former imports the definition of the std::string class, which is what you want.

Also, unless you are trying to do some really fancy stuff with the console, you don't need to include conio.h.

HTH,

##### Share on other sites
Actually I was using Conio for 'getch()' at the end to pause the program, even though its deprecated, it still works for my machine and testing purposes ;)

Ahh thanks, I did do that, though it gave me a 'warning' about that getch() deprecation, and I just misread what column the number was on and saw it as error, so I hadn't realised I already fixed the problem, haha. Then I changed it back cause I thought it was still broken, oh wow. Guess thats what I get for starting all this at like 2-3 in the morning, haha.

Thanks again.

Edit: Alright, so whats the best thing I should be using to make my program suspend at the end? Its getting annoying seeing that warning every time I compile :S I used to never get that, now I can't make it shut up.

Edit 2: Using cin.get() now, and its working fine and I'm enjoying no warnings upon compile!

[Edited by - Xelen on March 2, 2006 4:04:04 AM]

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631764
• Total Posts
3002210
×