I can't for the life of me find out what is wrong with my code. I am only a few weeks into this beginners c++ course so please understand my noobiness.
This is the prompt I have to follow:
Your program should work as follows. First, you need to prompt for and input a file path to the poll file.
Next, you'll check to make sure that the file at that path opened correctly. If not, please report an
appropriate error message and quit the program. Then, echo the contents of the file to the screen. Finally,
display the averages for each of the four demographics required. The numbers you're looking for are:
Republicans under $30000: 51%
Republicans over $30000: 65%
Democrats under $30000: 40%
Democrats over $30000: 47%
The percentages shown are what I need to find out but it keeps printing as zero. Thanks for the 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 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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main(){
// init vars
string first, last;
char party;
int salary, rating, ratingDPoor = 0, ratingDRich = 0, ratingRPoor = 0, ratingRRich = 0;
ifstream inFile("poll.txt");
int democratRich = 0;
int republicanRich = 0;
int democratPoor = 0;
int republicanPoor = 0;
// read from file and store
while(inFile >> first >> last >> party >> salary >> rating)
{{
// find the amount of people in each group
if (salary > 30000 && party == 'D')
democratRich++;
else if (salary < 30000 && party == 'D')
democratPoor++;
else if (salary > 30000 && party == 'R')
republicanRich++;
else if (salary < 30000 && party == 'R')
republicanPoor++;
}
// echo to screen
cout << left;
cout << setw(10)<< first
<< setw(10) << last
<< setw(10) << party
<< setw(10) << salary
<< setw(1) << rating
<< endl;
}
while(inFile >> first >> last >> party >> salary >> rating)
{{{{
// democrats who make more than 30000
if (rating == 0 && party == 'D' && salary > 30000)
ratingDRich = ratingDRich + 0;
else if (rating == 1 && party == 'D' && salary > 30000)
ratingDRich = ratingDRich + 1;
else if (rating == 2 && party == 'D' && salary > 30000)
ratingDRich = ratingDRich + 2;
else if (rating == 3 && party == 'D' && salary > 30000)
ratingDRich = ratingDRich + 3;
else if (rating == 4 && party == 'D' && salary > 30000)
ratingDRich = ratingDRich + 4;
else if (rating == 5 && party == 'D' && salary > 30000)
ratingDRich = ratingDRich + 5;
}
// democrats who make less than 30000
if (rating == 0 && party == 'D' && salary < 30000)
ratingDPoor = ratingDPoor + 0;
else if (rating == 1 && party == 'D' && salary < 30000)
ratingDPoor = ratingDPoor + 1;
else if (rating == 2 && party == 'D' && salary < 30000)
ratingDPoor = ratingDPoor + 2;
else if (rating == 3 && party == 'D' && salary < 30000)
ratingDPoor = ratingDPoor + 3;
else if (rating == 4 && party == 'D' && salary < 30000)
ratingDPoor = ratingDPoor + 4;
else if (rating == 5 && party == 'D' && salary < 30000)
ratingDPoor = ratingDPoor + 5;
}
// republicans who make more than 30000
if (rating == 0 && party == 'R' && salary > 30000)
ratingRRich = ratingRRich + 0;
else if (rating == 1 && party == 'R' && salary > 30000)
ratingRRich = ratingRRich + 1;
else if (rating == 2 && party == 'R' && salary > 30000)
ratingRRich = ratingRRich + 2;
else if (rating == 3 && party == 'R' && salary > 30000)
ratingRRich = ratingRRich + 3;
else if (rating == 4 && party == 'R' && salary > 30000)
ratingRRich = ratingRRich + 4;
else if (rating == 5 && party == 'R' && salary > 30000)
ratingRRich = ratingRRich + 5;
}
// republicans who make less than 30000
if (rating == 0 && party == 'R' && salary < 30000)
ratingRPoor = ratingRPoor + 0;
else if (rating == 1 && party == 'R' && salary < 30000)
ratingRPoor = ratingRPoor + 1;
else if (rating == 2 && party == 'R' && salary < 30000)
ratingRPoor = ratingRPoor + 2;
else if (rating == 3 && party == 'R' && salary < 30000)
ratingRPoor = ratingRPoor + 3;
else if (rating == 4 && party == 'R' && salary < 30000)
ratingRPoor = ratingRPoor + 4;
else if (rating == 5 && party == 'R' && salary < 30000)
ratingRPoor = ratingRPoor + 5;
}
ratingDPoor = (ratingDPoor * 20) / democratPoor;
ratingDRich = (ratingDRich * 20) / democratRich;
ratingRPoor = (ratingRPoor * 20) / republicanPoor;
ratingRRich = (ratingRRich * 20) / republicanRich;
cout << endl << endl;
cout << "Republicans under $30000: " << ratingRPoor << "%" << endl;
cout << "Republicans over $30000: " << ratingRRich << "%" << endl;
cout << "Democrats under $30000: " << ratingDPoor << "%" << endl;
cout << "Democrats over $30000: " << ratingDRich << "%" << endl;
// pause and exit
getchar();
return 0;
}
|