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

Edited 7 Years Ago by dylank: Typo

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

Edited 7 Years Ago by u8sand: n/a

Try using getline(cin, user_input);

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

Edited 7 Years Ago by Clinton Portis: n/a

This question has already been answered. Start a new discussion instead.