Good day folks,

Can anyone tell me why my code only accept the first word of the input.

E.g., I input Ana mae.. The output will only consider Ana as my input hence it will produce like
Hello, Ana!

#include <iostream>
#include <string>

using namespace std;

int main()
    string name;

    cout << "Enter your name : " ;
    cin >> name;
    cout << endl;

    const string greeting  = "Hello, " + name + "!";    

    const string spaces(greeting.size(), ' ');
    const string second = "* " + spaces + " *";
    const string first(second.size(), '*');

    cout << first << endl;
    cout << second << endl;
    cout << "* " + greeting + " *" << endl;
    cout << second << endl;
    cout << first << endl;

    return 0;


It will disregard "mae" in the output

Edited by GeekPlease: Incomplete details

5 Years
Discussion Span
Last Post by GeekPlease

I assume you mean at line 11, that is because operator>> on an istream into a string uses any white space as a separator, it is specifically coded to only read up to the space. If you want to get an entire line into a string use std::getline, declared in string.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.