944,135 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1065
  • C++ RSS
Nov 7th, 2009
0

C++ Strings For Input

Expand Post »
Hi, I have a program that I am using to convert a string into binary. My current code is as follows:
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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; Nov 7th, 2009 at 11:46 pm. Reason: Typo
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Nov 8th, 2009
0
Re: C++ Strings For Input
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Nov 8th, 2009
0
Re: C++ Strings For Input
C++ Syntax (Toggle Plain Text)
  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; Nov 8th, 2009 at 12:20 am.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Nov 8th, 2009
0
Re: C++ Strings For Input
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; Nov 8th, 2009 at 12:44 am.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 8th, 2009
0
Re: C++ Strings For Input
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.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: first time using a class: help?
Next Thread in C++ Forum Timeline: C++ IM Client





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC