Placing information into the correct area.

I would like to get this to show the amount of money complete with a $ and to two decimal places, I know how to accomplish this in C# but not C++. Also the age so I don't have to separate them into two cout statments. Thanks for your help.

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 << "Please enter your age: ";
	cin >> age;

	//Prompt the user to enter their amount of money
	cout << "Please tell me how much money you have: ";
	cin >> money;

	//Prompt the user to enter their full name; adding a clear for the input buffer
	cout << "Please enter your full name: ";
	cin.ignore();
	getline( cin, fullName );
	
	//Display the name back to the user; and adds another line
	cout << "Thank you " << fullName << endl;

	//Display the age back to the user
	cout << "You 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;
}
Last edited on
1) You have already managed output with dollar sign. Here is the way to show exactly two digits after decimal point:
1
2
3
4
5
6
7
#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::fixed << std::setprecision(2) << 3525.3456456664;
}
http://ideone.com/cUh5D9

2) I I understand you correctly you want to do that: cout << "You are " << age << " years old;\nand you have $" << money << " in your pocket.";

And please, read my first answer in your other thread and use one of the methods there. (either ignoring everything until newline or skipping whitespace character)
Your current solution violates C++ streams console philosophy: whitespaces are not important
Last edited on
Here is the updated code. I think this sums it up nicely.
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>
#include <iomanip>
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; adding a clear for the input buffer
	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 << "You are " << age << " years old";

	//Display the amount of money back to the user
	cout << "\nand you have $";
	std::cout << std::fixed << std::setprecision(2) << money << " in your pocket";

	//Giving the user an ending message
	cout << "\nGoodbye .....\n";

	//Keeping the window open
	system("PAUSE");

	//Return zero
	return 0;
}
Topic archived. No new replies allowed.