My code is supposed to take the users input of their full name, and then display it. I used the cin >> fullName; and it only returned the first name, I was guided to use getline( cin, fullName); and still doesn't display properly. Thanks for any help on this matter.
//declaring header mains
#include <iostream>
#include <string>
usingnamespace std;
//Method: The main method
//Purpose: To get the users name, age, and money; and return the information
//Parameters: Name as string, age as integer, and money as a float value
//Returns: Users inputted name, age, and money
int main()
{
//Declaring variables
string fullName;
int age;
float money;
//Promt the user to enter their age
cout << "\nPlease enter your age: ";
cin >> age;
//Prompt the user to enter their amount of money
cout << "\nPlease tell me how much money you have: ";
cin >> money;
//Prompt the user to enter their full name
cout << "\nPlease enter your full name: ";
getline( cin, fullName);
//Display the name back to the user; and adds another line
cout << "\nThank you " << fullName << endl;
//Display the age back to the user
cout << "\nYou are " << age;
cout << " years old; ";
//Display the amount of money back to the user
cout << "\nand you have $" << money;
cout << " in your pocket.";
//Giving the user an ending message
cout << "\nGoodbye .....\n";
//Keeping the window open
system("PAUSE");
// and finally, return zero
return 0;
}
Please enter your age: 12
Please tell me how much money you have: 10
Please enter your full name: Joe Bloggs
Thank you Joe Bloggs
You are 12 years old;
and you have $10 in your pocket.
Goodbye .....
Press any key to continue . . .
If you added it after getline code still should not work properly. And I advise against use of sync() to clear input buffer. Actual behavior is implementation defined and will not work for everyone.
I should just log off now lol, I meant before... you know, when you have those days when the Wife will just not leave you alone... this is one of them.
I'm quitting, I'll take up ironing or something. :)
@Softrix
How is that possible? Under Linux using GCC and C++11 it skips the prompt for Full name on me. I had to add a call to cin.ignore(1, '\n'); for it to work on my end. Same with ideone.com.
//declaring header mains
#include <iostream>
#include <string>
usingnamespace std;
//Method: The main method
//Purpose: To get the users name, age, and money; and return the information
//Parameters: Name as string, age as integer, and money as a float value
//Returns: Users inputted name, age, and money
int main()
{
//Declaring variables
string fullName;
int age;
float money;
//Promt the user to enter their age
cout << "\nPlease enter your age: ";
cin >> age;
//Prompt the user to enter their amount of money
cout << "\nPlease tell me how much money you have: ";
cin >> money;
cin.ignore(1, '\n'); // had to add this for it to work right for me
//Prompt the user to enter their full name
cout << "\nPlease enter your full name: ";
getline( cin, fullName);
//Display the name back to the user; and adds another line
cout << "\nThank you " << fullName << endl;
//Display the age back to the user
cout << "\nYou are " << age;
cout << " years old; ";
//Display the amount of money back to the user
cout << "\nand you have $" << money;
cout << " in your pocket.";
//Giving the user an ending message
cout << "\nGoodbye .....\n";
//Keeping the window open
system("PAUSE");
// and finally, return zero
return 0;
}
Here is the corrected code, it works now after clearing the input buffer. I also want to make the money appear with a dollar sign and two decimal places. I also want to put the inputs for the money amount and age appear in the middle of the phrases, I know how to accomplish this in C# but not in C++.
//declaring header mains
#include <iostream>
#include <string>
usingnamespace std;
//Method: The main method
//Purpose: To get the users name, age, and money; and return the information
//Parameters: Name as string, age as integer, and money as a float value
//Returns: Users inputted name, age, and money
int main()
{
//Declaring variables
string fullName;
int age;
float money;
//Promt the user to enter their age
cout << "\nPlease enter your age: ";
cin >> age;
//Prompt the user to enter their amount of money
cout << "\nPlease tell me how much money you have: ";
cin >> money;
//Prompt the user to enter their full name
cout << "\nPlease enter your full name: ";
cin.ignore();
getline( cin, fullName );
//Display the name back to the user; and adds another line
cout << "\nThank you " << fullName << endl;
//Display the age back to the user
cout << "\nYou are " << age;
cout << " years old; ";
//Display the amount of money back to the user
cout << "\nand you have $" << money;
cout << " in your pocket.";
//Giving the user an ending message
cout << "\nGoodbye .....\n";
//Keeping the window open
system("PAUSE");
//Return zero
return 0;
}
Please enter your age: 25
Please tell me how much money you have: 10
Please enter your full name: John Doe
Thank you
You are 25 years old;
and you have $10 in your pocket.
Goodbye .....
Please enter your age: 25
Please tell me how much money you have: 10
Please enter your full name: John Doe
Thank you John Doe
You are 25 years old;
and you have $10 in your pocket.
Goodbye .....
Press any key to continue . . .
I actually meant his standard library might not be conforming to the standard. Some third-party STL implementation I seen in 2004 made getline skip leading whitespaces.