Help! reading in data from a file etc...

closed account (SNhURXSz)
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{
	string dataFile;
	string lastName, firstName, middleInitial;
	int totalDuck, totalGoose, sumDuck=0, sumGoose=0;
	int num;
	double average, totalAverage;


	cout << "Enter the Name of the Data File: " << endl;
	cin >> dataFile;

	ifstream mainFile, studentFile;
	
	mainFile.open(dataFile.c_str());

	if (!mainFile)
	{
		cout << "Error." << endl;
		return 1;
	}

	cout << left << setw(20) << "Students" << right << setw(20) << "Average\n";

	while(mainFile >> lastName >> firstName >> middleInitial)
	{
		cout << firstName << " " << middleInitial << " " << lastName << endl;
		dataFile = firstName + lastName + ".dat";
		
		studentFile.open(dataFile.c_str());
			if (!studentFile)
			{
				cout << "No Data File" << endl;
				return 1;
			}
			else
			{
				totalDuck = 0, totalGoose = 0;
				while (studentFile >> num)
				{
					if (num==1)
					{
						totalDuck++;
					}
					else
					{
						totalGoose++;
					}
					sumDuck += totalDuck;
					sumGoose += totalGoose;
				}
				average = totalDuck/totalGoose;
				cout << right << setw(20) << fixed << setprecision(2) << average << endl;		
			}
	}
	totalAverage = sumDuck/sumGoose;
	cout << left << setw(20) << "\"Duck\" Average" << fixed << setprecision(2) << right << setw(2) << totalAverage;
	
	return 0;

}


This is the output I am trying to get:
Enter Name of Data File:
Students Average
Joe L Timms 11.50
Julie C Walters 6.17
Bob D Smith 3.00

"Duck" Average: 6.68

This is the output I get:

--- First Run ---
Enter the Name of the Data File:
Students Average
Joe L Timms
bash: line 1: 9977 Floating point exception./gprog2.exe

An average isn't displayed and my first loop wont iterate to read in the second name and so on.

I have no idea where I am going wrong. Any help would be much appreciated.

More on what I am supposed to do:
Ask user for file containing student names. Each student name has a file containing how many ducks and many gooses he/she said during the game. duck = 1, goose = 0 - so the format for the file:
JoeTimms.dat

111111111011111111111111110111111
111111110111111111111101111111110
111111110

I have to display an average of ducks per student, and then a total group average of the number of ducks said.
Last edited on
Well, I saw
Floating point exception
so I looked for some division.

What happens on line 59 if totalGoose is 0? This could happen if there were no "gooses" in the file or if line 46 failed before reading any in.

Pay attention to the hint those error messages give you.
You are reading the entire string of numbers into an integer variable which can't hold it. Your input file either needs spaces between each number or you need to read in to a string variable and iterate through each character.
Last edited on
Topic archived. No new replies allowed.