| | |
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; 22 Days Ago at 11:46 pm. Reason: Typo
0
#3 22 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; 22 Days Ago at 12:20 am.
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
0
#5 22 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 |
.dll ada api array binary bitmap c# c++ calculator char char* character class codeblocks console convert crashcourse data database decide development download drawing email embedded encryption error file filename format fpx fstream function functions game gdi+ graph guess gui hash html ifstream int introductory java linker linux list math maze method modal newbie number numbers opengl output parallel pause pong practice primenumbersinrange print problem proficiency program programmer programming projects python read recursion recursive regex regqueryvalueex reverse rpg search send single sql store string struct template text timer toolkit traverse tree tutorial unicode url variable visual web win32 win32api wxwidgets year






