Author |
Message |
AnubisProgrammer
|
Posted: Tue Feb 07, 2006 9:44 pm Post subject: Multiple word inputs - Help please |
|
|
Alright, when you use a cin statement for a string you can only enter one word.
code: | #include<iostream.h>
#include<string>
using namespace std;
int main(){
string a;
cin>>a;
cout<<a;
return()
} |
In that code, if you tried to input "help me" it would crash. I tried using the getline function, and then it doesn't crash, but it still only outputs the first word. Is there any way to save multiple words as a variable? For instance if I typed "Help me" I could get it to output that? I know this question is worded oddly but it's late at night where I am and I'm very frustrated with this concept at the moment. Anyways, if anyone can help, it would be appreciated. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue Feb 07, 2006 9:53 pm Post subject: (No subject) |
|
|
You need to use the std::getline function to get an entire line of input. |
|
|
|
|
![](images/spacer.gif) |
AnubisProgrammer
|
Posted: Tue Feb 07, 2006 9:55 pm Post subject: (No subject) |
|
|
Thanks. However, as I mentioned, my teacher doesn't teach. Would it be possible to see an example of std::getline? I'm not familiar with using std except for after "using namespace" |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue Feb 07, 2006 10:01 pm Post subject: (No subject) |
|
|
code: | #include <iostream>
#include <string>
int main()
{
std::string foo;
std::getline(std::cin, foo);
return 0;
} |
Or if you prefer:
code: | #include <iostream>
#include <string>
using namespace std;
int main()
{
string foo;
getline(cin, foo);
return 0;
} |
|
|
|
|
|
![](images/spacer.gif) |
AnubisProgrammer
|
Posted: Tue Feb 07, 2006 10:04 pm Post subject: (No subject) |
|
|
Thanks a lot. I'll try that. You seem to be answering all my questions lately. Thanks, you're way better then my teacher. |
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Tue Feb 07, 2006 10:31 pm Post subject: (No subject) |
|
|
wtd is better than most of the highschool computer science teachers in canada |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Tue Feb 07, 2006 11:12 pm Post subject: (No subject) |
|
|
I have yet to find an area of programing where wtd doesn't contribute something useful... when I do I'm gonna be awefully scared. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue Feb 07, 2006 11:15 pm Post subject: (No subject) |
|
|
Cornflake wrote: I have yet to find an area of programing where wtd doesn't contribute something useful... when I do I'm gonna be awefully scared.
For sufficiently loose definitions of "useful", perhaps.
And some of my useful contributions seem to scare you a fair deal too. ![Wink Wink](images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Wed Feb 08, 2006 12:22 am Post subject: (No subject) |
|
|
Useful in that they provide another level of insite, or a new approach aht I missed, or were just plain confirmed what I was thinking.
And yes... you scare me lots... you and your Lisp... |
|
|
|
|
![](images/spacer.gif) |
|