Hello soccer53,
No, it does not look well and is hard to read along with some flaws in the program.
As a start if you put some blank lines in your code to break it up it becomes easier to read and easier to fine where a problem might be.
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
|
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// method signature
float rowAverage(float, float, float, float, float);
int main()
{
// local variable declared
float n1, n2, n3, n4, n5, avg;
string line, name;
ifstream fin;
ofstream fout;
// opening the both file
fin.open("inputdata.txt", ios::in);
fout.open("averages.txt", ios::out);
// checking ig file opened or not
if (!fin || !fout)
{
cout << "Error in opening file !! exiting app \n";
exit(-1);
}
// geting the name of person
getline(fin, name);
// using eof to read till last line
while (!fin.eof()) {
getline(fin, line);
// for splitting string using sstream
stringstream ss(line);
string temp;
int i = 0;
// splitting number by space and assigning it to float variable
while (ss >> temp)
{
++i;
if (i == 1)
n1 = stoi(temp);
if (i == 2)
n2 = stoi(temp);
if (i == 3)
n3 = stoi(temp);
if (i == 4)
n4 = stoi(temp);
if (i == 5)
n5 = stoi(temp);
}
if (i == 5)
{
// getting the average
avg = rowAverage(n1, n2, n3, n4, n5);
// writing to file
fout << avg << endl;
}
}
cout << "Data sent by : " << name << endl;
cout << "File name : averages.txt" << endl;
return 0;
}
float rowAverage(float n1, float n2, float n3, float n4, float n5)
{
float avg, sum;
sum = n1 + n2 + n3 + n4 + n5;
avg = sum / 5;
return avg;
}//end of code
| |
It is also helpful to break up the instructions into smaller pieces so that they are easier to follow.
Referring to the above code it is best not to use line 6 as it
WILL get you in trouble some day. Better to learn what is in the standard namespace and how to qualify it now slowly than all at once in the future.
Line 8. "method signature" works, but I would just use "proto types". It is more accurate.
Line 14. The instructions show the use of "float", but "double" is the preferred type these days and it is more accurate and will store the number better. Keep this in mind for the future.
Lines 20 and 21. You have defined "fin" and "fout" as an "istream" and "ostream". Therefore the "ios::in" and "ios::out" are not needed here. "ios::in" and "ios::out" are only needed when you define "fin" and "fout" as a "fstream" . As an "fstream" you have to tell it what type of file it is.
Line 24. This single if statement is better made into two separate if statements and better written this way:
1 2 3 4 5 6 7
|
// checking if file opened or not
if (!fin)
{
cout << "Error in opening input file !! exiting app \n";
std::this_thread::sleep_for(std::chrono::seconds(5)); // Requires header files "chrono" and "thread"
exit(1);
}
| |
The second line puts a pause in the program before it ends because the are times when the program ends the window it was running in will close and you will not be able to read the message. Notice I changed the "exit" number form "-1" to "1". Zero means that the program ended normally and any number greater than 1 means that there is a problem. Having more than one "exit" statement in a program the numbers greater than zero can help you track down whhere the problem is. Then I would create a second if statement for "fout". This way you will know which file stream is the problem and not have to guess.
Line 31. Is what you need to make the while condition wor properly.
Line 35. Reads the numbers form the file, but after that the string stream is not needed. Line 35 could easily be written as:
fin >>n1 >>n2 >>n3 >>n4 >>n5;
and you could avoid using the string stream.
Line 45. The inner while loop to process the string stream is not need and appears to be the most difficult way to process this.
Line 60. The "if" statement is not need as with the above while loop the it is connected to. You just need to call the function and write to the output file here.
Sorry I missed putting in a blank line at line 64.
After you write to the output file at line 65 this would be a good place to add to your "sum" not in the function which I will explain later.
In order to use "!eof()" in the while condition the last line of code in the while loop needs to be reading the next name before checking for "eof()".
Line 75. The function definition is correct and inside the function works
On line 81. The 5 will be promoted to a float for the calculation, but it would be better to write that as 5.0. Just a small thing and not really important, but a more proper way of writing the formula.
The program did compile for me, but I still need to check it.
With out the input file I do not know if some of my suggestions will work. It is just a guess at this point. It would be helpful if you would post the input file so everyone can work with the same information without guessing.
I still need to reread the instructions and see how they match the program. When I have done this I will let you know what I find.
Hope that helps,
Andy