Trouble with my looping

OK, so im making some progress here!!! My assignment is to open and read from a txt file ("GradeFile.txt") with 3 rows of data. Each row of data represents a different type of assignment (ie. Homework, Exams & Programs) Each row to consist of:
1. # of Assignments (ie. 3)
2. Total # of possible points (ie. 300)
3. A groups of grades that corresponds to # of Assignments (ie. 90 90 90)
4. A number that represents the weighted GPA of each assignment type (ie. .60)
The text file would look something like this:
3 300 90 90 90 .60
4 400 90 90 90 90 .25
5 500 90 90 90 90 90 .15
I can get the first line to read correctly but the ".6" is hard coded in and I need it to be a variable. So my questions are:
Why isn't it working for the next line?
Why is it not looping 3 times?
and
How do I make the decimals (weighted GPA) a variable? I'm trying to assign the decimal to "Weight". I just cant seem to get it in the right place, so I just took it out.

I hope I have explained it ok...

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
#include <iostream>
#include <fstream>

using namespace std;
//function declarations
int PerformFileOperations();

int main()
{
    cout << "Welcome to the GradeCaculator Deluxe." << endl;
    PerformFileOperations();
    system ("pause");
    return 0;
    
}
//////////////PerformFileOperations
int PerformFileOperations()
{
    int NoOfGrades;
    int TotalPtsAllowed;
    int Grades;
    int Weight;
    int index;
    int count;
    int Average;
    ifstream DataFile;
    
    DataFile.open("GradeFile.txt");
    if (!DataFile)
    {
       cout << "File Failed to open...Program terminated" << endl;
    return 1;
    }//end if
    cout << "Average     GPA" << endl;
    count = 0;
   DataFile >> NoOfGrades >> TotalPtsAllowed;
   while (DataFile)
   {
    count++;
    //cout << "Homework::" << endl;
    Average = 0;
    index = 0;
    while(index < NoOfGrades)
    {
                DataFile >> Grades;
                index++;
                Average = Average + Grades;
                }
                //cout << Average;
                cout << " " << int(Average * .5) << "       " << (int(Average * .5) * .6) << endl;
                DataFile >> NoOfGrades >> TotalPtsAllowed;
                }

       
    return 0;
}//end PerformFileOperations 
When working with numbers that should contain fractions, you should store them as double instead of int.

I'm assuming the last number on each of the lines from your datafile corresponds to the variable you call Weight. In which case you are not reading Weight from the file currently. At the end of the while loop on line 43-48 you could:
DataFile >> Weight; //of course earlier in the program: double weight;

Edit: I also notice that Average should be divided by the TotalPtsAllowed to be correct. This should be done before the average grade is cout'ed.
Last edited on
Ok, I guess my next question would be....how do I know where I should read from the DataFile?
Each DataFile >> anything; will go in order through the file. When "anything" is a number variable, any spaces in the file are treated like an end of input. Meaning that every space can separate two numbers. Also end-of-line characters are treated like end of input for any type of variable.

Knowing where you should read from the file is a simple matter of figuring out where the program needs data from the file. Like at the beginning of your while loop when you grab two numbers from the file and store them to NoOfGrades and TotalPtsAllowed. That makes sense because your program needs that data from the file to know how many times to loop itself. Then inside the loop the program needs a grade from the file. Then when the loop is done the program needs the weight from the file. Then the program reads two more numbers NoOfGrades and TotalPtsAllowed... and so forth.

It can almost be like plotting a timeline.
So since it grabs them in order, I need to grab Weight after the grades get added and averaged?
Or you could grab it and store it to Weight and then average the grades. It is up to you.
ok, so I've made progress again!!! Please tell me why I'm getting the first number as a scientific number.....
My text file looks like this:

6 600 87 88 89 90 91 92 0.70
4 400 95 96 97 98 0.20
3 300 100 100 100 0.10

My code:

#include <iostream>
#include <fstream>


using namespace std;
//function declarations
int PerformFileOperations();
int FileInfo();

int main()
{
cout << "Welcome to the GradeCaculator Deluxe." << endl;
//FileInfo();
PerformFileOperations();
system ("pause");
return 0;

}
//////////////PerformFileOperations
int PerformFileOperations()
{
int NoOfAssignments;
int TotalPtsAllowed;
int Grades;
float Weight;
int RowNumber;
int TotalWeightedGrade;
int TotalScore;
float Average;
int index;
int count;
ifstream DataFile;

DataFile.open("GradeFile.txt");
if (!DataFile)
{
cout << "File Failed to open...Program terminated" << endl;
return 1;
}//end if
NoOfAssignments = 0;

cout << "NAME:: A.C. Millard" << endl;


//DataFile >> NoOfAssignments >> TotalPtsAllowed;
while (DataFile)
{//start outter loop
DataFile >> NoOfAssignments >> TotalPtsAllowed;
index = 0;
TotalScore = 0;


while(index < NoOfAssignments)
{//start (inner loop)
DataFile >> Grades;
index++;

TotalScore = TotalScore + Grades;

}//end while (inner loop)
Average = TotalScore/NoOfAssignments;

cout << TotalScore << " " << Average << " " << Average * Weight << endl;

DataFile >> Grades >> Weight;

}//end while (outer loop)
return 0;
}//end PerformFileOperations
DataFile >> Grades >> Weight;
should just be:
DataFile >> Weight;
Because all the grades are read during the inner while loop.
Which means the only thing left on the current line in the DataFile would be just the weight at the end of the line. This is probably messing up the math somehow.
Also if you want to setprecision on float numbers look here:
http://www.cplusplus.com/reference/iomanip/setprecision/
Yep, I started on it early this morning and it jumped right out at me. I was grabbing weight in the wrong place. Moved it up a few lines and I was all set. I don't know why I was having a hard time with the loops, I understand them perfectly ( or so I thought). Thanks for your willingness to help out!
I took the time to re-write the idea for this program for my own practice. I took in to consideration that you will more than likely have multiple files for multiple students as well. I tested this code and it functions and even tested with erroneous choices (files that were out of reach, or non existent)

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <vector>

#define BUFSIZE MAX_PATH

using namespace std;

int GrabGrades(string FileName) {

	ifstream DataFile;

	int Assignments = 0, HighestPoints = 0, TotalPoints = 0;
	int tempHold = 0, Counter = 0;
	double WeightedGPA = 0;

	DataFile.open(FileName);

	if(!DataFile) {
		cout << "File doesn't exist. Terminating program."; return 1;
	}
	
	while(!DataFile.eof()) {
		DataFile >> Assignments >> HighestPoints;

		while(Assignments > Counter) {
			DataFile >> tempHold;
			TotalPoints += tempHold;
			Counter++;
		}

		DataFile >> WeightedGPA;
		cout << "Total Number of Assignments: " << Assignments << endl;
		cout << "Highest Possible Points: " << HighestPoints << endl;
		cout << "Average Points: " << (TotalPoints/Assignments) << endl;
		cout << "Average w/ Weight: " << (TotalPoints/Assignments)*WeightedGPA << endl;
		cout << "Weighted GPA: " << WeightedGPA << endl << endl;

		Counter = 0;
		WeightedGPA = 0;
		Assignments = 0;
		HighestPoints = 0;
		TotalPoints = 0;
		tempHold = 0;
	}

	DataFile.close();

}
vector <const char *> files(0);
int findfiles() {

	WIN32_FIND_DATA data;
	HANDLE h = FindFirstFile(L"C:\\Users\\Lucian\\Documents\\Visual Studio 2010\\Projects\\testingcode\\testingcode\\*.txt",&data);
	
	if( h!=INVALID_HANDLE_VALUE ) 
	{
		do
		{
			char * nPtr = new char [lstrlen( data.cFileName ) + 1];
			for( int i = 0; i < lstrlen( data.cFileName ); i++ )
				nPtr[i] = char( data.cFileName[i] );

			nPtr[lstrlen( data.cFileName )] = '\0';
			//cout << nPtr << endl;
			files.resize(files.size()+1);
			files[files.size()-1] = nPtr;
		} while(FindNextFile(h,&data));
	} 
	else {
		cout << "Error: No such folder." << endl; }
	
	FindClose(h);
	
	return 0;

}

int main() {
	
	int decision = 0;
	cout << "Welcome to the GradeCalculator Deluxe." << endl << endl;
	
	findfiles();

	cout << "Please select the student file using the numbers below: " << endl;

	for ( int i = 0; i<files.size(); i++ ) {
		cout << (i+1) << ": " << files[i]<< endl;
	}
	cout << endl << "Your choice: ";
	cin >> decision;

	decision--;

	if((decision<files.size()) && (decision > -1))
		GrabGrades(files[decision]);
	else
		cout << "Invalid choice. Goodbye."<<endl; return 1;

	system ("pause");
	return 0;
}


Of course the File search API is Windows based, an assumption on my part. If your using anything other, it wont work.

Now for my prediction, we enter BOOST arguments :)

Edit: You would obviously have to edit the directory where the grade files are being kept in findfiles() function.
Last edited on
Topic archived. No new replies allowed.