| | |
C++ Strings For Input
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
Hi, I have a program that I am using to convert a string into binary. My current code is as follows:
The program only looks at the first word of user input and then the space but ignores the rest of the sentance. Eg.
How can I include all of the user input into the string (including white spaces)?
--Dylan
C++ Syntax (Toggle Plain Text)
int main(void) { string userInput; cout << "Enter a string to be converted:" << endl; cin >> userInput; for (int index = 0; index <= userInput.length(); index++) { cout << userInput[index] << endl; } system("pause"); return 0; }
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)
input: hello dolly 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
0
#3 26 Days Ago
C++ Syntax (Toggle Plain Text)
#define MAX_LENGTH 256 int main(void) { char userInput[MAX_LENGTH]; // use a character array not string cout << "Enter a string to be converted:" << endl; cin.get(userInput,256); // cin.get(char*,int) not cin >> ; cin.ignore(); // ignore the leading '\n' character for (int index = 0; index < strlen(userInput) /* strlen gives you the length of a character array */; index++) { cout << userInput[index] << endl; } system("pause"); return 0; }
didn't realize the "never-mind" : o hope this helps you anyway.
Last edited by u8sand; 26 Days Ago at 12:20 am.
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Searching strings (C#)
- strings as input, substr and len (C++)
- Need help parsing strings w/ plain text and drop down lists (PHP)
- strings, arrays, length program (C++)
- Pointer/String input (C)
- how to read an array of strings in C++ (C++)
- Strings (C)
- Virgin programmer (Java)
Other Threads in the C++ Forum
- Previous Thread: first time using a class: help?
- Next Thread: C++ IM Client
| Thread Tools | Search this Thread |
ada add animation api array based binary bitmap bmp c# c++ calculator char char* character class classes client codeblocks compile console convert count data database datetime decide development download drawing dwmapi dynamic ect email embed encryption error examples file filename format fstream function functions game gdi+ graph hash ifstream input insert int java jni kioti16 linker linux math maze method multiple music newbie objects opengl output parsing position print problem program programming python read recursion recursive regex regqueryvalueex reverse rpg search string struct superclass template text timer toolkit traverse tree unicode university url user validation visual visualization web win32 year






