Printing '$' and ',' with Output

Hello!

So i think I've worked out all the details of my program except for this one last one... Can you guys help me with printing a '$' and ',' sign with my output results? Haven't been able to find much about it! Thank you!
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <string>
#include <iostream>
#include <iomanip>

using namespace std; 

void GetData( string[], string[], double[],double[]);
void CalcData(int, int, double[],double[],double[], double[]);
void PrintData(string ename[4], string etitle[4], double curr_year[], double new_year[]);


int main()
{

int const hours = 40;
int const weeks = 52;

string ename[4];
string etitle[4];

double ewage[4]; 
double inc[4]; 
double curr_year[4];
double new_year[4];
GetData(ename,etitle, ewage, inc);
CalcData(hours, weeks, ewage, inc,curr_year, new_year);
PrintData(ename, etitle, curr_year, new_year);

cout<<endl; system("PAUSE");
return 0;

}

/* Function Definitions */
void GetData(string fename[], string fetitle[], double fewage[],double finc[])
{
	for (int i = 0; i< 4; i++)
	{
		cout<<"Please enter Employee Name: "<<endl;
		getline(cin,fename[i]);
		cout<<"Please enter Employee Title: "<<endl;
		getline(cin,fetitle[i]);
		cout<<"Please enter Employee Hourly Wage: $"<<endl;
		cin>>fewage[i];
		cout<<"Please enter Employee Wage Increase: % "<<endl;
		cin>>finc[i];
		cin.ignore(80,'\n');
	}
}

void CalcData(int hours, int weeks, double fewage[],double finc[],double fcurr_year[], double fnew_year[])
{
	for (int i=0; i<4; i++)
	{
		int hours = 40;
		int weeks = 52;
		fcurr_year[i] = fewage[i] * hours * weeks;
		fnew_year[i] = fcurr_year[i] * (( finc[i] + 100 ) / 100);
	}
}

void PrintData(string fename[4], string fetitle[4], double fcurr_year[], double fnew_year[])
{		
	int temp, tempa;
	string strtemp = "";
	string strtempa = "";

		cout << left << setw(20)<<"Name";
		cout << left << setw(30)<<"Title";
		cout << left << setw(15)<<"Current Income";
		cout << left << setw(15)<<"New Income";
		cout << "\n" << endl;
	
	for (int i=0; i<4; i++)
	{
		for (int j= 0; j <3; j++)
		{
			if (fnew_year[j]<fnew_year[j+1])
			{
				temp=fnew_year[j];
				fnew_year[j]=fnew_year[j+1];
				fnew_year[j+1]=temp;

				tempa=fcurr_year[j];
				fcurr_year[j]=fcurr_year[j+1];
				fcurr_year[j+1]=tempa;

				strtemp =fename[j];
				fename[j]=fename[j+1];
				fename[j+1]=strtemp;

				strtempa = fetitle[j];
				fetitle[j]=fetitle[j+1];
				fetitle[j+1]=strtempa;
			}
		}
	}

	for (int i=0; i <4; i++)
	{
		cout << fixed;
		cout << setprecision(2);
		cout << left << setw(20) << fename[i];
		cout << left << setw(30) << fetitle[i];
		cout << left << setw(15) << fcurr_year[i];
		cout << left << setw(15) << fnew_year[i];
		cout << "\n" << endl;
	} 
	
}




Please enter Employee Name:
Goofy
Please enter Employee Title:
Executive Chef
Please enter Employee Hourly Wage: $
15.85
Please enter Employee Wage Increase: %
2.8
Please enter Employee Name:
Mickey Mouse
Please enter Employee Title:
Vp of Entertainment
Please enter Employee Hourly Wage: $
26.75
Please enter Employee Wage Increase: %
3.2
Please enter Employee Name:
Donald Duck
Please enter Employee Title:
CFO
Please enter Employee Hourly Wage: $
20.15
Please enter Employee Wage Increase: %
2.6
Please enter Employee Name:
Cinderella
Please enter Employee Title:
VP of Customer Service
Please enter Employee Hourly Wage: $
23.45
Please enter Employee Wage Increase: %
2.7
Name                Title                         Current Income New Income


Mickey Mouse        Vp of Entertainment           55640.00       57420.48


Cinderella          VP of Customer Service        48776.00       50092.95


Donald Duck         CFO                           41912.00       43001.00


Goofy               Executive Chef                32968.00       33891.00


Press any key to continue . . .


So I would need to output $55,640.00 and $57,420.28
Last edited on
@ilovelearning

Here's a small program that prints a dollar sign, and an amount, complete with the thousand commas in it. with 2 places for the cents. Incorporate it into your program..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// thousands.cpp : main project file.

#include <locale>
#include <iostream>
#include <iomanip>

using namespace std;
int main()

{
#ifdef _WIN32
     locale loc("English_America");
#else
     locale loc("en_US.UTF8");
#endif
     cout.imbue(loc); // or use your ofstream object
     cout << fixed << setprecision(2) << "$" << 123456789.47 << '\n';
}
Hey thanks! That solved the comma issue, but i'm still having trouble as to incorporating the dollar sign into the main function? I also tried to put it in my PrintData function with some errors as follows:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* 1. Write a function to accept the data from the table in the order given (no $, no % entered).
2. Write a function to calculate current yearly income and new yearly income taking the wage increase into consideration
(assume 40 hr work week, 52 weeks for the year).
3. Write a function to display the data in the format given below:

Name			Title						Current_income	New_income
Goofy			Executive_Chief				$xx,xxx.xx		$xx,xxx.xx
Mickey Mouse	VP_of_Entertainment			$xx,xxx.xx		$xx,xxx.xx
Donald Duck		CFO							$xx,xxx.xx		$xx,xxx.xx
Cinderella		VP of Customer Service		$xx,xxx.xx		$xx,xxx.xx

4. As part of the function for #3, sort the employee information by descending New Income.
Note: Use passing parameters by pointer/reference, manipulators, arrays, variables of type string, etc.
*/
#include <locale>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std; 

void GetData( string[], string[], double[],double[]);
void CalcData(int, int, double[],double[],double[], double[]);
void PrintData(string ename[4], string etitle[4], double curr_year[], double new_year[]);


int main()
{
#ifdef _WIN32
     locale loc("English_America");
#else
     locale loc("en_US.UTF8");
#endif
     cout.imbue(loc); // or use your ofstream object
     cout << fixed << setprecision(2) << "$" << 123456789.47 << '\n';
int const hours = 40;
int const weeks = 52;

string ename[4];
string etitle[4];

double ewage[4]; 
double inc[4]; 
double curr_year[4];
double new_year[4];
GetData(ename,etitle, ewage, inc);
CalcData(hours, weeks, ewage, inc,curr_year, new_year);
PrintData(ename, etitle, curr_year, new_year);


cout<<endl; system("PAUSE");
return 0;

}

/* Function Definitions */
void GetData(string fename[], string fetitle[], double fewage[],double finc[])
{
	for (int i = 0; i< 4; i++)
	{
		cout<<"Please enter Employee Name: "<<endl;
		getline(cin,fename[i]);
		cout<<"Please enter Employee Title: "<<endl;
		getline(cin,fetitle[i]);
		cout<<"Please enter Employee Hourly Wage: $"<<endl;
		cin>>fewage[i];
		cout<<"Please enter Employee Wage Increase: % "<<endl;
		cin>>finc[i];
		cin.ignore(80,'\n');
	}
}

void CalcData(int hours, int weeks, double fewage[],double finc[],double fcurr_year[], double fnew_year[])
{
	for (int i=0; i<4; i++)
	{
		int hours = 40;
		int weeks = 52;
		fcurr_year[i] = fewage[i] * hours * weeks;
		fnew_year[i] = fcurr_year[i] * (( finc[i] + 100 ) / 100);
	}
}
void PrintData(string fename[4], string fetitle[4], double fcurr_year[], double fnew_year[])
{		
	int temp, tempa;
	string strtemp = "";
	string strtempa = "";

		cout << left << setw(20)<<"Name";
		cout << left << setw(30)<<"Title";
		cout << left << setw(15)<<"Current Income";
		cout << left << setw(15)<<"New Income";
		cout << "\n" << endl;
	
	for (int i=0; i<4; i++)
	{
		for (int j= 0; j <3; j++)
		{
			if (fnew_year[j]<fnew_year[j+1])
			{
				temp=fnew_year[j];
				fnew_year[j]=fnew_year[j+1];
				fnew_year[j+1]=temp;

				tempa=fcurr_year[j];
				fcurr_year[j]=fcurr_year[j+1];
				fcurr_year[j+1]=tempa;

				strtemp =fename[j];
				fename[j]=fename[j+1];
				fename[j+1]=strtemp;

				strtempa = fetitle[j];
				fetitle[j]=fetitle[j+1];
				fetitle[j+1]=strtempa;
			}
		}
	}

	for (int i=0; i <4; i++)
	{
		cout << fixed;
		cout << setprecision(2);
		cout << left << setw(20) << fename[i];
		cout << left << setw(30) << fetitle[i];
		cout << left << setw(15) << "$" << fcurr_year[i] << endl;
		cout << left << setw(15) << "$" << fnew_year[i] << endl;
		cout << "\n" << endl;
	} 
	
}

$123,456,789.47
Please enter Employee Name:
Goofy
Please enter Employee Title:
Executive Chef
Please enter Employee Hourly Wage: $
15.85
Please enter Employee Wage Increase: %
2.8
Please enter Employee Name:
Mickey Mouse
Please enter Employee Title:
VP of Entertainment
Please enter Employee Hourly Wage: $
26.75
Please enter Employee Wage Increase: %
3.2
Please enter Employee Name:
Donald Duck
Please enter Employee Title:
CFO
Please enter Employee Hourly Wage: $
20.15
Please enter Employee Wage Increase: %
2.6
Please enter Employee Name:
Cinderella
Please enter Employee Title:
VP of Customer Service
Please enter Employee Hourly Wage: $
23.45
Please enter Employee Wage Increase: %
2.7
Name                Title                         Current Income New Income


Mickey Mouse        VP of Entertainment           $              55,640.00
$              57,420.48


Cinderella          VP of Customer Service        $              48,776.00
$              50,092.95


Donald Duck         CFO                           $              41,912.00
$              43,001.00


Goofy               Executive Chef                $              32,968.00
$              33,891.00



Press any key to continue . . .


Thanks again for the response!
Hey we just went over this in our CS class and I think it applies to this case:

Displaying Special Characters \ ‘ “ Using Escape Sequences
precede the character to be printed with a backslash
\\ print backslash
\’ print single quote
\” print double quote

So I'm thinking cout << "\$" << endl;
Last edited on
$ and , aren't special characters; you can print them normally.
1
2
	cout << left << setw(15) << "$" << fcurr_year[i] << endl;
	cout << left << setw(15) << "$" << fnew_year[i] << endl;


That's two separate lines you're writing on there. You're also setting the width of the "$" field to 15.
@ilovelearning

Sorry about the messed up output caused by the addition I showed you. I corrected the program to accommodate it.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// I Love Learning.cpp : main project file.

/* 1. Write a function to accept the data from the table in the order given (no $, no % entered).
2. Write a function to calculate current yearly income and new yearly income taking the wage increase into consideration
(assume 40 hr work week, 52 weeks for the year).
3. Write a function to display the data in the format given below:

Name			Title						Current_income	New_income
Goofy			Executive_Chief				$xx,xxx.xx		$xx,xxx.xx
Mickey Mouse	VP_of_Entertainment			$xx,xxx.xx		$xx,xxx.xx
Donald Duck		CFO							$xx,xxx.xx		$xx,xxx.xx
Cinderella		VP of Customer Service		$xx,xxx.xx		$xx,xxx.xx

4. As part of the function for #3, sort the employee information by descending New Income.
Note: Use passing parameters by pointer/reference, manipulators, arrays, variables of type string, etc.
*/

#include <locale>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std; 

#ifdef _WIN32
     locale loc("English_America");
#else
     locale loc("en_US.UTF8");
#endif

void GetData( string[], string[], double[],double[]);
void CalcData(int, int, double[],double[],double[], double[]);
void PrintData(string ename[4], string etitle[4], double curr_year[], double new_year[]);


int main()
{
int const hours = 40;
int const weeks = 52;

string ename[4];
string etitle[4];

double ewage[4]; 
double inc[4]; 
double curr_year[4];
double new_year[4];
GetData(ename,etitle, ewage, inc);
CalcData(hours, weeks, ewage, inc,curr_year, new_year);
PrintData(ename, etitle, curr_year, new_year);


cout<<endl; system("PAUSE");
return 0;

}

/* Function Definitions */
void GetData(string fename[], string fetitle[], double fewage[],double finc[])
{
	for (int i = 0; i< 4; i++)
	{
		cout<<"Please enter Employee Name: "<<endl;
		getline(cin,fename[i]);
		cout<<"Please enter Employee Title: "<<endl;
		getline(cin,fetitle[i]);
		cout<<"Please enter Employee Hourly Wage: $"<<endl;
		cin>>fewage[i];
		cout<<"Please enter Employee Wage Increase: % "<<endl;
		cin>>finc[i];
		cin.ignore(80,'\n');
	}
}

void CalcData(int hours, int weeks, double fewage[],double finc[],double fcurr_year[], double fnew_year[])
{
	for (int i=0; i<4; i++)
	{
		int hours = 40;
		int weeks = 52;
		fcurr_year[i] = fewage[i] * hours * weeks;
		fnew_year[i] = fcurr_year[i] * (( finc[i] + 100 ) / 100);
	}
}
void PrintData(string fename[4], string fetitle[4], double fcurr_year[], double fnew_year[])
{		
	double temp, tempa; // Changed from int to double
	string strtemp = "";
	string strtempa = "";

		cout << left << setw(15)<<"Name";
		cout << left << setw(25)<<"Title";
		cout << left << setw(20)<<"Current Income";
		cout << left << setw(20)<<"New Income";
		cout << "\n" << endl;
	
	for (int i=0; i<4; i++)
	{
		for (int j= 0; j <3; j++)
		{
			if (fnew_year[j]<fnew_year[j+1])
			{
				temp=fnew_year[j];
				fnew_year[j]=fnew_year[j+1];
				fnew_year[j+1]=temp;

				tempa=fcurr_year[j];
				fcurr_year[j]=fcurr_year[j+1];
				fcurr_year[j+1]=tempa;

				strtemp =fename[j];
				fename[j]=fename[j+1];
				fename[j+1]=strtemp;

				strtempa = fetitle[j];
				fetitle[j]=fetitle[j+1];
				fetitle[j+1]=strtempa;
			}
		}
	}
	for (int i=0; i <4; i++)
	{
		cout << left << setw(15) << fename[i] << setw(25) << fetitle[i];
		cout.imbue(loc);

		cout  << "$" << setw(19) << fixed << left << setprecision(2) << fcurr_year[i]; 
		cout  << "$" << setw(19) << fixed << left << setprecision(2) << fnew_year[i] << endl;
	} 
}
Last edited on
Thanks so much everyone!

My program works wonderfully now. I truly appreciate all of your time and help! =DD
@whitenite1 not a problem at all, thank you sir!
Topic archived. No new replies allowed.