Line 20: Why are you calling your output file "infile". Confusing to the reader.
Line 35: You are analyzing the numbers as you generate them. The instructions say to open the file and read the numbers from the file. You want to analyze the numbers as you read them.
Line 35: max is undefined.
Line 35: You don't actually write the numbers out to the file.
Line 40: You need to open the file for input. Read until you reach eof. Note: Do not do:
while (! infile.eof())
. See note below. For each record read, you want to call analyze.
Line 8: You don't want to calculate average inside analyze if analyze is being called for each record. After your read loop, divide sum by total_num to get the average.
Line 8: Not sure what the difference between sum and max is.
Line 8: You don't need to pass infile to analyze() if your read loop is in main.
Line 17: You want to initialize your variables.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1 2 3
|
while (stream >> var) // or while (getline(stream,var))
{ // Good operation
}
|
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
|
#include<iostream>
#include<cmath>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include <limits>
using namespace std;
void analyze(int number, int & sum, int & large, int & small, int & total_num)//where the calculations happen
{
sum += number;
if (number > large)
large = number;
if (number < INT_MAX)
small = number;
total_num++;
}
int main()
{
int sum = 0, large = 0, small = INT_MAX, average, total_num = 0;
int number;
ofstream ofile;
srand(time(0));
ofile.open("random.txt");//opens the text file in the folder of the program alongside the cpp file
if (ofile.fail())
{
cerr << "Error opening file" << endl;//if there are any errors opening the file it exits the program
exit(1);
}
for (int i = 0; i < 100; i++)
{ int number = rand() % 100 + 1;
ofile << number << endl;
}
ofile.close();//closes the file then open up the text file to get random numbers
ifstream ifile("random.txt");
while (ifile >> number)
analyze(number, sum, large, small, total_num);
cout << "Largest number is: " << large << endl;
cout << "Smallest number is: " << small << endl;
cout << "sum is: " << sum << endl;
cout << "Average is: " << sum / total_num << endl;
system("pause");
}
|