C++ Strings For Input

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster

C++ Strings For Input

 
0
  #1
26 Days Ago
Hi, I have a program that I am using to convert a string into binary. My current code is as follows:
  1. int main(void) {
  2. string userInput;
  3.  
  4. cout << "Enter a string to be converted:" << endl;
  5. cin >> userInput;
  6. for (int index = 0; index <= userInput.length(); index++) {
  7. cout << userInput[index] << endl;
  8. }
  9.  
  10.  
  11. system("pause");
  12. return 0;
  13. }

The program only looks at the first word of user input and then the space but ignores the rest of the sentance. Eg.
  1. input: hello dolly
  2. output: hello

How can I include all of the user input into the string (including white spaces)?
--Dylan
Last edited by dylank; 26 Days Ago at 11:46 pm. Reason: Typo
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster
 
0
  #2
26 Days Ago
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster
 
0
  #3
26 Days Ago
  1. #define MAX_LENGTH 256
  2. int main(void) {
  3. char userInput[MAX_LENGTH]; // use a character array not string
  4.  
  5. cout << "Enter a string to be converted:" << endl;
  6. cin.get(userInput,256); // cin.get(char*,int) not cin >> ;
  7. cin.ignore(); // ignore the leading '\n' character
  8. for (int index = 0; index < strlen(userInput) /* strlen gives you the length of a character array */; index++) {
  9. cout << userInput[index] << endl;
  10. }
  11. system("pause");
  12. return 0;
  13. }

didn't realize the "never-mind" : o hope this helps you anyway.
Last edited by u8sand; 26 Days Ago at 12:20 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 329
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 37
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz
 
0
  #4
26 Days Ago
Try using getline(cin, user_input);

edit: went to go take a wiz, when I came back ya'll had the answers already.
Last edited by Clinton Portis; 26 Days Ago at 12:44 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster
 
0
  #5
26 Days Ago
Yea, the thread here: http://www.daniweb.com/tutorials/tutorial71858.html

had the answer, basically what you said.
cin.get(userInput,256) fixed the length of the string, and I am attempting to convert an entire book into binary with this code.
Reply With Quote Quick reply to this message  
Reply

Tags
c++, string

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC