Computer Science Canada

Multiple word inputs - Help please

Author:  AnubisProgrammer [ 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.

Author:  wtd [ Tue Feb 07, 2006 9:53 pm ]
Post subject: 

You need to use the std::getline function to get an entire line of input.

Author:  AnubisProgrammer [ Tue Feb 07, 2006 9:55 pm ]
Post 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"

Author:  wtd [ Tue Feb 07, 2006 10:01 pm ]
Post 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;
}

Author:  AnubisProgrammer [ Tue Feb 07, 2006 10:04 pm ]
Post 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.

Author:  Andy [ Tue Feb 07, 2006 10:31 pm ]
Post subject: 

wtd is better than most of the highschool computer science teachers in canada

Author:  md [ Tue Feb 07, 2006 11:12 pm ]
Post 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.

Author:  wtd [ Tue Feb 07, 2006 11:15 pm ]
Post 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. Smile

And some of my useful contributions seem to scare you a fair deal too. Wink

Author:  md [ Wed Feb 08, 2006 12:22 am ]
Post 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...


: