-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
134 lines (99 loc) · 3.41 KB
/
input.cpp
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
133
134
// <Program Name> Programming Project #3 Average Income (Using Functions and Arrays)
// <Author> Brendan Jackson
// <Date of Programs Release> 08/05/15
// <Program Description> takes 3 arrays and displays them with average income
#include <iostream> // allows cin and cout statements
#include <iomanip> //allows setprecision
#include <string> //allows strings
using namespace std; // Namespace std allows program to use entities from <iostream>
int input(string[], int[], double[]); //function 1
double calculate_average_income(double[], int); //function 2
void display_survey_data(string[], int[], double[],int , double); //function 3
int main() // main function
{
//variables for functions
string name[10];
int members[10];
double income[10];
int count_of_households;
double average;
//get input
count_of_households = input(name, members, income);
//calculate average
average = calculate_average_income(income, count_of_households);
//output all data in table
display_survey_data(name, members, income, count_of_households, average);
return 0;
}
int input(string name[], int members[], double income[]) //function 1
{
// get household info
int count_of_households = 0;
cout << "How many house holds were there? ";
cin >> count_of_households;
//TODO: handle bad input (characters and decimals)
if (count_of_households >= 11 || count_of_households < 0)
{
cout << "must enter valid # " ; //TODO: more description
count_of_households = 0; //set back to safe value
}
else
{
//cycle through arrays
for (int count = 0; count < count_of_households; count++) //TODO: take out (count + 1) start from 1 alternatively
{
// get survey info for names
cout << "Enter household #" << (count + 1) << "'s head of household name\t" ;
cin.ignore() ; // ignores keyboard buffer characters
getline (cin, name[count]) ;
// get survey info for income
cout << "Enter household #" << (count + 1) << "'s annual income\t" ;
cin >> income[count];
// get survey info for members
cout << "Enter household #" << (count + 1) << "'s household members\t" ;
cin >> members[count];
}
}
return count_of_households;
}
double calculate_average_income(double income[], int count_of_households) //function 2
{
//add incomes together
double total = 0.0;
double average = 0.0;
//loop over income
for (int count = 0 ; count < count_of_households; count++)
{
//add income to runnning total
total += income[count];
}
// save calculations
average = total / count_of_households;
return average;
}
void display_survey_data(string name[], int members[], double income[],int count_of_households, double average) //funtion 3
{
//print out header
cout << setw(30) << ""
<< setw(30) << ""
<< setw(30) << "NUMBER OF\n" ;
cout << setw(30) << "HOUSEHOLD NAME"
<< setw(30) << "ANNUAL INCOME"
<< setw(30) << "HOUSEHOLD MEMBERS\n" ;
cout << setw(30) << "--------------------"
<< setw(30) << "---------------"
<< setw(30) << "------------------------\n" ;
///loop over values
for (int count = 0 ; count < count_of_households; count++)
{
cout << setw(30) << name[count]
<< setw(30) << setprecision(2) << fixed << showpoint << income[count]
<< setw(30) << members[count]
<< endl;
}
// display average
cout << endl
<< setw(30) << "AVERAGE INCOME"
<< setw(30) << average
<< endl;
}