I haven't done much with C++ and even less with string.h. I'm trying to get user input into a string, how do I do it? Again here's what I have:
Code:void go()
{
cout << "URI: ";
string uri;
cin >> uri;
}
Printable View
I haven't done much with C++ and even less with string.h. I'm trying to get user input into a string, how do I do it? Again here's what I have:
Code:void go()
{
cout << "URI: ";
string uri;
cin >> uri;
}
What doesn't work for you?
If your string is going to have spaces in it, you're probably going to need to use cin.getline(), but that doesn't work directly with the string header, so...it's been awhile.
It's giving me an error about string being undeclared, so that means it's not in the string header, right? As you can tell, I don't know what I'm doing here :D
How are you including the header? I do:
I've seen other people do <iostream.h> and <string.h>, but this is how I've always done it.Code:#include <iostream>
#include <string>
using namespace std;
try this:
end of file is ^Z (control-Z)Code:#include <string>
#include <iostream>
using namespace std;
void main(){
string str;
char c;
while(!cin.eof()){
c = cin.get();
str += c;
}
cout << str << endl;
}
or you could use a newline char instead.
You always need to use standard headers.Code:#include <string>
#include <iostream>
using namespace std;
void main(){
string str;
char c;
while(c != '\n'){
c = cin.get();
str += c;
}
cout << str << endl;
}
Code:#include <string>
#include <iostream>
using namespace std;
Thanks guys. I'll try that and see if it works. Although I've found with MS Visual C++ that if I use the use namespace std and miss of the .h in the includes it doesn't compile :confused: I think VC is a bit ****, because it work in Dev C++ OK.
I got it to work. I think it's because I was putting .h at the end and adding using namespace std. It seems to be OK now I took off the .h bit.