Finding the Average from a Data File

Dec 13, 2018 at 3:51pm
Assignment Question: Read and parse each line in the data file credit.txt and store the data in appropriate variables. This is what I have, and it isn't even compiling, and I am not entirely sure what I did wrong. If someone could give me some insight and explain to me the problem, it would be much appreciated.

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 <math.h>
#include <fstream>
#include <string>
#include <numeric>
using namespace std;

int main()
{
string getline;
int number1;
ifstream infile;
string firstname;
string lastname;
string email;
float num;
float total;
float x;
float aver;

x=0; total=0;
infile.open("credit.text");

while(!infile.eof())
{ {
      getline (infile,firstname,'\t');
      getline (infile,lastname,'\t');
      getline (infile,num,'\t');
      getline (infile,email,'\t');
      cout << firstname << lastname << num << email;
total=total+num;
x++;
}

aver=(total-num)/(x-1);

cout << "The last number in this range is: " <<num<< "\n";
cout<< "The sum of this range is: " <<(total-num)<<'\n';
cout<< "The number of items in this range is: "<<x-1<<'\n';
cout<< "The average of this range is: "<<aver<<'\n';
cout<< ""<<'\n';

infile.close();

getchar();
return 0;
}
Dec 13, 2018 at 4:06pm
26 { {
Well the first problem is you have an excess of opening braces compared to closing braces.

> getline (infile,num,'\t');
Next you're treating num as if it were a string, whereas it's a float.
Perhaps make num a string, then do some kind of string-to-float conversion at line 32.

> while(!infile.eof())
eof() doesn't work as you think it does.
If the file is empty, then eof() isn't immediately true. eof() only becomes true when some file operation (getline or >> for example) has failed.
Assuming well-formed lines for the moment, you could try
1
2
3
4
5
6
while(getline (infile,firstname,'\t'))
{
      getline (infile,lastname,'\t');
      getline (infile,num,'\t');
      getline (infile,email,'\n');  // perhaps \n rather than \t for the last field.
}


Good job on the code tags on your first post.
Dec 13, 2018 at 4:17pm
I have completely redone most of the code, to make it a little easier on myself.
Now, the code compiles. BUT, when it compiles the answers are not what they should be.
My professor said the Average should look something like: 12345.67
and the average is coming out to -0.


#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <sstream>
#include <string>
#include <numeric>
using namespace std;

int main()
{
string line, firstname, lastname, email;
int number;
ifstream infile;
float num;
float total;
float x;
float aver;

x=0; total=0;
infile.open("credit.text");

while(getline(infile, line))
{ istringstream iss1(line);
iss1 >> firstname;
iss1 >> lastname;
iss1 >> num;
iss1 >> email;
total=total+num;
x++;
}

aver=(total-num)/(x-1);

cout << "The last number in this range is: " <<num<< "\n";
cout<< "The sum of this range is: " <<(total-num)<<'\n';
cout<< "The number of items in this range is: "<<x-1<<'\n';
cout<< "The average of this range is: "<<aver<<'\n';
cout<< ""<<'\n';

infile.close();

getchar();
return 0;
}
Dec 13, 2018 at 4:56pm
Shame about the lack of code tags :(

Add some debug code, or use a debugger.

1
2
3
4
5
6
7
8
9
{ istringstream iss1(line);
iss1 >> firstname;
iss1 >> lastname;
iss1 >> num;
iss1 >> email;
total=total+num;
x++;
cout << "Debug: x=" << x << ", num=" << num << ", total=" << total << endl;
}

Does it look right at the start, or does it blow up later on?
Or is it borked from the start.

Dec 13, 2018 at 5:12pm
The answer when it compiles is:

the last number in this range is: 0
The sum of this range is: 0
The number of items in this range: -1
The average: -0
Dec 13, 2018 at 5:18pm
So are you sure of the filename, because it doesn't look like you opened the file at all.

That is, if you put some cout in your loop and received nothing.

1
2
3
4
5
infile.open("credit.text");
if ( !infile ) {
  cerr << "Failed to open file" << endl;
  return 1;
}


Note that the usual convention is to call text files credit.txt, not credit.text
Dec 13, 2018 at 5:19pm
Even when I try the debugger it still gives me the same answers, there must be something wrong with my equation, and I have tried so many different ways, and one time I got average="nan".
Dec 13, 2018 at 5:20pm
So I get an answer that is a lot closer to the one I was supposed to get, I can probably figure it out from here, I will repost if I need help. I cannot believe I even put .text, that is not like me at all, haha.
Topic archived. No new replies allowed.