need help doing math with arrays

im doing some math with arrays, but my output is a lot of garbage, i dont have any errors and cant seem to figure out why it isnt out putting correctly

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
  //*****************************************************************************************************

#include <stdafx.h>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//*****************************************************************************************************

void getHour(int hour[], const string names[], const int SIZE);
void getPayRate(float payrate[], const string names[], const int SIZE);
void calcWages(float wages[], float payrate[], int hour[], const int SIZE);
void display (float wages[], const string names[], const int SIZE);

//*****************************************************************************************************

int main()
{
	const int SIZE = 7;
	int hour[SIZE];
	float payrate[SIZE],
		  wages[SIZE];
	const string names[SIZE] = {"John", "Oliver", "Luke", "Henry", 
				                "Lily", "Chloe", "Mike"};
	getHour(hour, names, SIZE);
	getPayRate(payrate, names, SIZE);
	calcWages(wages, payrate, hour, SIZE);
	display(wages, names, SIZE);
	cin.get();
	cin.get();
	return 0;
}
//*****************************************************************************************************
//
//*****************************************************************************************************
void getHour(int hour[], const string names[], const int SIZE)
{
	const int lowest = 0;
	for(int i = 0; i < SIZE; i++)
	{
		do
		{
			cout << "Please enter the hours for employee " << names[i] << ": ";
			cin >> hour[i];
			if(hour[i] < lowest)
				cout << "that in not a vaild amount of hours" << endl;
		}
		while(hour[i] < lowest);
	}
	cout << "" << endl;
	cout << "=========================================" << endl;
}
//*****************************************************************************************************
//
//*****************************************************************************************************
void getPayRate (float payrate[], const string names[], const int SIZE)
{
	float low = 7.25;
	for(int i = 0; i < SIZE; i++)
	{
		do
		{
		cout << "Please enter the pay rate for employee " <<names[i]        << ": ";
		cin >> payrate[i];
		if(payrate[i] < low)
			cout << "Pay rate must be higher than " << low << endl;
		}
		while(payrate[i] < low);
	}
	cout << "" << endl;
	cout << "=========================================" << endl;
}
//******************************************************************************
//
//******************************************************************************
void calcWages (float wages[], float payrate[], int hour[], const int SIZE)
{
	for(int i = 0; i < SIZE; i++)
	{
		wages[i] = payrate[i] * hour[i];
	}
}
//******************************************************************************
//
//******************************************************************************
void display (float wages[], const string names[], const int SIZE)
{
	cout << "**Wages information for the week: **" << endl;
	cout << "" << endl;
	for(int i = 0; i < SIZE; i++)
	{
		cout << names[i] << setw(15) << " $" << showpoint << setprecision(2) << wages[i] << endl;
	}
}
but my output is a lot of garbage

What exactly are you inputting into the program?

With that input what exactly is your program outputting?

With that input what exactly do you expect your program to output?

im inputting hours that theyve worked, so between 0-infinity
and then their wage which has to be a minimum of 7.25
the output should look like this

john: $130.80
Oliver: $416.50
so on so forth

but im getting this:

john: $1.3e+002
oliver: $4.2e+002
so on so on.

im not sure what ive done to make the output come out like this.
So the output is in scientific format instead of the fixed format you desire. You will probably need to add the "fixed" format flag to your output function.

1
2
3
4
5
6
7
8
9
10
void display (float wages[], const string names[], const int SIZE)
{
	cout << "**Wages information for the week: **" << endl;
        // These flags only need to be done once.
	cout << showpoint << setprecision(2) << fixed << endl;
	for(int i = 0; i < SIZE; i++)
	{
		cout << names[i] << setw(15) << " $"  << wages[i] << endl;
	}
}
ah thank you, that worked. now the other issue im having is that the output name and amount earned need to be evenly spaced apart. which is why i have setw(15) there. but depending on how long the name is, it will offset the amount they earned, making them not line up. how exactly do you counter that?

it should look like this
since spacing isnt working i use the (---) to indicate distance
john(------------)$130.80
oliver(-----------)$416.50
so on so forth
but im getting this

john(------------)$130.80
oliver(------------)$416.50

im not sure how to stop that. since it is looping.
Last edited on
Actually there should be 16 spaces between the end of the name and the '$' character (using code tags should preserve the spacing). You'll probably want to use a setw() before the names if you want to space the rest of the data evenly away from the names.

Also what happens if the dollar amount is less than one hundred or greater than nine hundred ninety nine?

the desired effect is to have the $ stay solid, and the numbers conform accordingly to it.

$99.99
$1000.00
Okay then just using a setw() in front of the names variable should do the trick.

Last edited on
this still isnt giving me the desired effect. i want the names to be left aligned then equal spacing between the names and the $ followed by the amount they earned. doing setw() prior to the names off sets them and does align everything else. but theyre not what im looking for

Then you need to also play with the "left" and "right" manipulators. Remember these manipulators are "sticky", meaning they stay in effect until you change them.

thank you so much
Topic archived. No new replies allowed.