#include<iostream>
#include<fstream>
usingnamespace std;
int main()
{
int count = 0;
int ar[500];
int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
ifstream myReadFile;
ofstream myOutFile;
myReadFile.open("example.txt");
myOutFile.open("output.txt");
char output[100];
int n = 0;
while (!myReadFile.eof())
{
myReadFile >> output;
// cout<<output << "\n"; this line you can remove cooments if you want the output on the screen.
n = (int)(output); // converting character to integer.
ar[count] = n;
if (n >= 180)
{
c5++;
}
elseif (n >= 160)
c4++;
elseif (n >= 140)
c3++;
elseif (n >= 120)
c2++;
else
c1++;
count++;
}
myOutFile << c1 << c2 << c3 << c4 << c5 << count;
// here yoiu can print the array into the file if you wish in to the file.
myReadFile.close();
myOutFile.close();
return 0;
}
Almost certainly you are overrunning the boundaries of array int ar[500]
But there are a number of problem areas in the code.
What is the contents of file "example.txt" and what is it you are attempting to do?
Line 21 almost certainly doesn't do what you think it does. And looping on eof() - not recommended at all. For example if the file was not opened, it will never reach eof, instead go into infinite loop.
This is my assignment:
The attached text data file contains an unknown quantity of integers whose values are between 100 and 200. Write a program that opens the file, counts how many values it contains, and creates a 1D array to store the values. Once the array is populated, calculate average and standard deviation. Also count how many values are between 100 and 119 (inclusive), 120 and 139 (inclusive), 140 and 159 (inclusive), 160 and 179 (inclusive), and 180 and 200 (inclusive). Output all the results (including the count of how many total values there are and the counts of how many values are in each range) to a separate text file
Line 21 is certainly wrong, it attempts to cast the address of the array into an integer. This is not the way to read the data from your file.
Did you consider how many numbers are in the file, even if the code worked, it would be more than the capacity of the array ar[500]
I suggest you start with some simple code to read the data and count how many there are.
Remember, the ifstream is a stream, just like cin. So, you can use the same syntax as cin >> number;
I should add, if you intend to use a fixed-size array to store an unknown quantity of data, then you should add a check to ensure that the capacity is not exceeded. Or use a std::vector which can resize itself as needed.
Thanks! You were right there were 1300 elements. However, I still can not do it. My output is the address of the array I think, its "000013011301" What I was trying to do it was maybe change int ar[1300]
to
I't's getting late where I am, so I can't give a full answer.
But look at this line: myOutFile << c1 << c2 << c3 << c4 << c5 << count;
if we assume c1 to c4 are zero, and both c5 and count are 1301, would that not give your output of
"000013011301" ?
It does worry me that the number 1301 should have appeared at all, since you said earlier that there were 1300 elements. It sounds like you are still looping on eof() which is unreliable for all sorts of reasons, but in this case it may have processed the last entry twice, thus causing an array out of bounds condition if you allocated exactly 1300 elements.
In any case, the code should be checking for that out of bounds error.