0

I've been doing C++ for two weeks now, i'm creating a very simple program which allows the user to input their name, and then it outputs there full name, here is the code;

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string first ;
    string middle ;
    string last;

    cout << "What is your firstname?" << endl;
    getline(cin, first);
    cout << "Your firstname is ";
    first = first;
    cout << first << endl;
    cout << endl;

    cout << "What is your middle name?" << endl;
    getline(cin, middle);
    cout << "Your middle name is ";
    middle = middle;
    cout << middle << endl;
    cout << endl;

    cout << "What is your lastname?" << endl;
    getline(cin, last);
    cout << "Your lastname is ";
    last = last;
    cout << last << endl;
    cout << endl;

    cout << "Your full name is " <<  first + middle + last << endl;

    system ("pause");
    return 0;
}

The issue I'm having is that it won't output spaces in the last cout

cout << "Your full name is " <<  first + middle + last << endl;

Any help would be appreciated! Thanks.

inquam
  • 12,664
  • 15
  • 61
  • 101
John Brown
  • 151
  • 3
  • 14
  • 1
    Well, you're not telling to print any spaces, why would it? – Mat Oct 25 '12 at 09:29
  • 1
    Is that what you want to do: `cout << "Your full name is " << first << " " << middle << " " << last << endl;` – Zane Oct 25 '12 at 09:30
  • 1
    You're managing to print sentences already; think about it. A space is `" "`. – Mat Oct 25 '12 at 09:30
  • @Zane I had actually tried that, and it worked fine. The problem is that it looked untidy, is there a better way of doing it? Or is that the only way. – John Brown Oct 25 '12 at 09:31
  • You can certainly use a 'joiner' of some kind that joins with a specified character. [See this question](http://stackoverflow.com/a/1833499/721269) for the Boost way. – David Schwartz Oct 25 '12 at 09:35

3 Answers3

3

change your output line to

cout << "Your full name is " <<  first << " " << middle << " " << last << endl;
Zoneur
  • 1,101
  • 9
  • 20
tomahh
  • 13,441
  • 3
  • 49
  • 70
0

That is because you are not telling it to write spaces where I think you want them.

My guess is that you get spaces just fine in

"Your full name is "

Then you want spaces between you first, middle and last strings? But you never add any spaces there.

The part that looks like

<<  first + middle + last <<

must be changed to include spaces

<<  first + " " + middle + " " + last <<

So you entire cout at the end should be

cout << "Your full name is " <<  first + " " + middle + " " + last << endl;
inquam
  • 12,664
  • 15
  • 61
  • 101
0
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string first ;
    string middle ;
    string last;

    cout << "What is your firstname?\n";
    getline(cin, first);
    cout << "Your firstname is " << first << "\n\n";

    cout << "What is your middle name?\n";
    getline(cin, middle);
    cout << "Your middle name is " << middle << "\n\n";

    cout << "What is your lastname?\n";
    getline(cin, last);
    cout << "Your lastname is " << last << "\n\n";

    cout << "Your full name is " <<  first +" "+ middle +" "+ last << "\n\n";

    system ("pause");
    return 0;
}
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147