Right now Im trying to write a program that can take an input file that has a bunch of integers, and then show the amount of numbers in certain ranges. For example:
If the input file has 20, 30, 40, 50, 60. And the ranges are 1-20, 21-40, 41-60
The output would be
1-20: 1 21-40: 2 41-60: 3
etc.
Im new to programming, so Im just having a little problem with my code, I know im close. Im using arrays to store them, but I wasnt sure how to make an array that adapts to how many integers there are in a file. So this is what I got so far. (Just for this exercise, Im trying to make the max amount the array will store 100 integers).
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
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int ranges(int);
int main()
{
ifstream indata;
ofstream outdata;
indata.open("scores1.txt");
outdata.open("scoresoutput.txt");
if(!indata)
{
cout << "Unable to open file" << endl;
return 1;
}
int range[] = {0, 0, 0, 0, 0, 0, 0, 0};
int score[50];
while(indata)
{
for(int i=0; i < 50 ; i++)
{
indata >> score[i];
if(0 <= score[i] <= 24)
{
range[0]++;
}
else if(25 <= score[i] <= 49)
{
range[1]++;
}
else if(50 <= score[i] <=74)
{
range[2]++;
}
else if(75<= score[i] <= 99)
{
range[3]++;
}
else if(100 <= score[i] <= 124)
{
range[4]++;
}
else if(125 <= score[i] <= 149)
{
range[5]++;
}
else if(150 <= score[i] <= 174)
{
range[6]++;
}
else if(175 <= score[i]<= 200)
{
range[7]++;
}
indata.ignore(1);
}
}
outdata << "Range" << setw(10) << "Number of Students" << endl <<
"0-24" << setw(20) << range[0] << endl<<
"25-49" << setw(20) << range[1] << endl <<
"50-74" << setw(20) << range[2] << endl <<
"75-99" << setw(20) << range[3] << endl <<
"100-124" << setw(20) << range[4] << endl <<
"125-149" << setw(20) << range[5] << endl <<
"150-174" << setw(20) << range[6] << endl <<
"175-200" << setw(20) << range[7] << endl;
return 0;
}
|
Right now the my output is just showing 0-24 50. And the other ranges arent holding any values. Can someone tell me what Im doing wrong?