Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multiple word inputs - Help please
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AnubisProgrammer




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: 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.
AnubisProgrammer




PostPosted: 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"
wtd




PostPosted: 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;
}
AnubisProgrammer




PostPosted: 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.
Andy




PostPosted: Tue Feb 07, 2006 10:31 pm   Post subject: (No subject)

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




PostPosted: 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.
wtd




PostPosted: 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. Smile

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




PostPosted: 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...
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: