Sign in to follow this  

Setting a value to a String...

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Endurion
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.


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Xelen
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>


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 this post


Link to post
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]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this