I'm aware that there is google and several websites that might be more appropiate for asking questions on coding but going to post it here as I'm aware that some of you have experience with coding languages and I feel a bit more comfortable posting it here.
I'm currently getting a headache over this C++ program I'm trying to make: Read a set of exam marks between 0 and 100 and create a histogram that shows how many marks in each decades (0-10, 11-20, 21-30...).
The histogram should look like:
00-10 | *
11-20 | ****
21-30 | *******
It carries on up until 91-100.
The part that is giving me a headache is defining the array called values and I haven't found a way to define it without causing more errors in the coding (Using Visual Studio 2010) and the part where I'm trying to get the output that puts it into a histogram.
The code I have gotten so far:
The words in italics are not actually written in the code but there to show where I'm getting headaches from.
Code:#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { int i, file; ifstream infile("marks1.txt"); if (!infile) { cout << "Cant open file"; exit(EXIT_FAILURE); } for (i=0;i<file;i++) { infile >> values[i]; <---- Error shown with values (undefined) } int marks, x, hold; int decades [10] = {0-10, 11-20, 21-30, 31-40, 41-50, 51-60, 61-70, 71-80, 81-90, 91-100}; int count[10]; // Contains for each section, the occurence number. <----- Is this the correct way? for ( x = 0; x <= values.size(); x++) <---- Error shown with values (undefined) { if(values[x] <= 10) count[0]++; else if(values[x] <=20) count[1]++; else if (values[x] <=30) count[2]++; else if (values[x] <=40) count[3]++; else if (values[x] <=50) count[4]++; else if (values[x] <=60) count[5]++; else if (values[x] <=70) count[6]++; else if (values[x] <=80) count[7]++; else if (values[x] <=90) count[8]++; else if (values[x] <=100) count[9]++; } cout << "00-10 | " << count[0] * "*"; <----- Error shown with "*" (must be arithmetic or enum type) (For all cout) cout << "11-20 | " << count[1] * "*"; cout << "21-30 | " << count[2] * "*"; cout << "31-40 | " << count[3] * "*"; cout << "41-50 | " << count[4] * "*"; cout << "51-60 | " << count[5] * "*"; cout << "61-70 | " << count[6] * "*"; cout << "71-80 | " << count[7] * "*"; cout << "81-90 | " << count[8] * "*"; cout << "91-100| " << count[9] * "*"; }
I appreciate suggestions and advice offered as I only recently started learning C++. Bonus points if you can suggest a way (or point me in the right direction) to changing the histogram so it's vertical bars/stars going up the page. Apologies if this would be better asked in a different forum.