Hi, I have a program that I am using to convert a string into binary. My current code is as follows:

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.

input: hello dolly
output: hello

How can I include all of the user input into the string (including white spaces)?
--Dylan

#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.

Try using getline(cin, user_input);

edit: went to go take a wiz, when I came back ya'll had the answers already.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.