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

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.

Yay. thanks pal. that solved my problem :)

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.