Hello rahaf201,
Sorry I thought I sent this earlier, but something went wrong and it did not send.
Along wit what
keskiverto has said.
This is a different approach to your code:
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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string temp;
int max{}, min{}, avg, i, sum = 0;
ifstream in;
in.open("temp.txt");
// <--- How do you know the file is open?
if (!in)
{
std::cout << "\n File " << std::quoted("temp.txt") << " Did not open!\n";
return 1; // <--- No point in continuing until you fis the problem.
}
for (i = 0; i < 20; i++)
{
if (max < temp[i])
{
max = temp[i];
temp[i] = max;
}
if (min > temp[i])
{
min = temp[i];
temp[i] = min;
}
sum = sum + temp[i];
avg = sum / 20;
}
in.close();
cout << "The maximum temprature is: " << max;
cout << "The minimum temprature is : " << min;
cout << "The average of the farenhiet temprature is: " << avg;
return 0;
}
|
Notice a few blank lines makes the code easier to read and that is the first goal.
Rearranging the header files is more a personal preference, but in the end the order makes no difference.
Lines 19 - 24 check if the file stream is open and ready to use. This is a must when dealing with an input stream. Not so much on an output stream as the output will create the file if it does not exist.
I was wondering int max{}, min{}, avg, i, sum = 0; why the difference when you define "sum"? What you did with "min" and "max" is good and can be used for "sum" Also "avg" and "i" should be initialized. Even if they do not need to be initialized it is still a good idea just to know that they do not contain a garbage value.
for (i = 0;i < 20;i++) // Why 20? "Assume that you DON’T know how" .
DO NOT ASSUME anything. You were told that the file is of unknown length. If you want to use a for loop then do this:
for (i = 0; in >> celsius >> farenheiht; i++). When you read past end of file the condition will fail and so will the loop.
Now that I had had a chance to give your code a test this is what I found.
In the line
string temp;
replace it with
int celsius{}, fahrenheit{};
. As a "string" you would have to change the "string" into an "int" before you could work with it. By reading them as "int"s you do not have to worry about the conversion.
The average calculation should be done outside the for loop and you need to divide by "i" not 20. This way you are dividing by the number of temps read and not a magic number the is most likely wrong.
1 2 3 4 5 6
|
if (max < temp[i])
{
max = temp[i];
temp[i] = max;
}
|
First off "temp[i]" is a single character (char) that you are comparing to an "int". Not what you want.
Not sure what line 5 is for, but if you read the file into an "int" it is not needed.
You have completely missed point 3 of the instructions. The average that you have is on the Celsius temperatures when it needs to be on the Fahrenheit temperatures.
You have the line of code
sum = sum + temp[i]
it should be
sum += fahrenheit;
.
Andy