You cannot use the stream syntax with cin in this szenario. When streaming into cin the space character is used as a delimiter.
The reason you get just the leading parts after a space is simple:
You read the first word until the first occuring space. Then you use getline() to read the full line, but the carret is already set to the second word (due to the cin stream behavior), overriding the first word in firstname with the leading part.
In order to get you program working, you need to replace all "cin >> string" calls with getline(). To read into other types then string (like int or double) you need to read the remaining stuff fromt he line (the newline character, for example). I've created a "garbage" string for this purpose, but there are many ways to solve this problem.
Just an working example:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string garbage;
int numbervariable;
cout << "How Old Are You?\n I am : ";
cin >> numbervariable;
// we only got the number, but there is still a newline character we need to skip
getline(cin, garbage);
string firstname;
cout << "What is your first name?\n My first name is : ";
getline (cin, firstname, '\n');
string lastname;
cout << "What is your last name?\n My last name is : ";
getline (cin, lastname, '\n');
string charactervariable = firstname + " " + lastname;
double decvar;
cout << "How much do you weight in kilograms?\nI weight : ";
cin >> decvar;
// same as for the numbervariable above, read the newline character
getline(cin, garbage);
cout << "Your name is : " << charactervariable << ".\nYou are : " << numbervariable << " Years old." << "\nYour weight is : " << decvar << "kg." << "\n \n";
int numplus = 0;
cout << ++numplus;
return 0;
}
output:
How Old Are You?
I am : 123
What is your first name?
My first name is : Abc Defg
What is your last name?
My last name is : van long name
How much do you weight in kilograms?
I weight : 1337
Your name is : Abc Defg van long name.
You are : 123 Years old.
Your weight is : 1337kg.
1