Cannot get string to display full name.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//declaring header mains
#include <iostream>
#include <string>
using namespace 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;
}

Works fine to me.


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 . . .
Works fine to me.
That is strange because it should not. You either have problems with compiler or did something wrong.

Visual Studio 2013, copied and pasted it in and compiled it.

Actually yes I added cin.sync(); after getline(cin, fullName);

Sorry, distractions are at a all time high today.
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. :)

Anyway MiiNiPaa, thanks for correcting my stupidity, now I will hang my head in shame :)

@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.

His code: http://ideone.com/jnDKZa
My correction: http://ideone.com/A5YOyk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//declaring header mains
#include <iostream>
#include <string>
using namespace 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;
}
Last edited on
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++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//declaring header mains
#include <iostream>
#include <string>
using namespace 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;
}

Yeah BHX Specter, scroll up as I messed up so apologies to OP.

EDIT: shes doing my head in ;-)
Last edited on
Your code is not working on this input :) : http://ideone.com/yWHI4E
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 .....
This is what I am getting on Visual Studio 2013


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 . . .
You did not entered input exactly like me. Check money line again.
@DEnumber50
http://www.cplusplus.com/reference/istream/istream/ignore/?kw=cin.ignore
If you notice, I did cin.ignore(1, '\n');.

MiiNiPaa wrote:
That is strange because it should not. You either have problems with compiler or did something wrong.

It doesn't work, but if it had been working, wouldn't that mean an issue with the OS running the program and not the compiler?
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.
Oh, okay. That is what kind of confused me. Thanks for clearing that up.
Topic archived. No new replies allowed.