Why am I getting different values here
Jul 12, 2013 at 12:05am UTC
HELP!!!!
I know I have something simple out of place here but I've been working on this for a few hours now and I cant get it right. The part of the code that I think Im having problems with is the array part. During my loop, I'm trying to build and array and by the looks of the first "cout", it appears to be correct(6 450 87, 4 291 95, 3 200 100) . but in the second "cout" (after "Total GPA is...") at the end of the loop, I get these weird values. What is happening to my array?
somebody please help......
My text file looks like this:
6 87 88 89 90 91 92 0.70
4 95 96 97 98 0.20
3 100 100 100 0.10
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
//
// main.cpp
// GradeCalculator
//
// Created by Andrew on 6/4/13.
//
//
#include <iostream>
#include <fstream>
#include <string>
#include "Assignment4.h"
using namespace std;
//--------build structure
struct Grade
{
string StudentName;
int NoOfHomeWorkGrades;
double HomeworkGPA;
double HomeworkWeight;
int NoOfProjectGrades;
double ProjectGPA;
double ProjectWeight;
int NoOfExamGrades;
double ExamWeight;
double ExamGPA;
double TotalGPA;
char LetterGrade;
string Comment;
}GradeStructure;
//----------function declarations
void GetInFileName(string&);
void GetOutFileName(string&);
int ReadFile(double &);
string GetComment(char &);
void GetLetterGrade(double &);
int OpenInFile(ifstream&);
int OpenOutFile(ofstream&);
//string GetOutFileName(string&);
int WriteData(char &, string&);
int CalculateGrade();
//-----------Main Body
int main()
{
double TotalGPA;
ifstream InDataFile;
ofstream OutDataFile;
string InFileName;
string OutFileName;
string Quote;
//char LetterGrade;
cout << "Welcome to the GradeCaculator Deluxe." << endl;
ReadFile(TotalGPA);
GetLetterGrade(TotalGPA);//this function calls "GetComment()"
//WriteData(LetterGrade, Quote);
//---------create structure--------------------
//system ("pause");
return 0;
}//end main
//-----------GetFileName
//-----------Get file name to read from
void GetInFileName(string& InFileName)
{
cout << "Enter the name of the file to gather info from (including extension): " ;
cin >> InFileName;
//reference variables do not need a return
}//end GetInFileName
//----------GetOutFileName
//----------Get name of output file
void GetOutFileName(string& OutFileName)
{
cout << "Enter the name of the file to write info to (including extension): " ;
cin >> OutFileName;
}//end GetOutFileName
//----------OpenInFile
//----------open the user specified file to read from
int OpenInFile(ifstream& InDataFile)
{
string InFileName;
GetInFileName(InFileName);
InDataFile.open(InFileName.c_str());
if (!InDataFile)
{
cout << "File Failed to open...Program terminated" << endl;
return 1;
}//end if
//ReadFile(TotalGPA);
return 0;
}//end OpenInFile
int OpenOutFile(ofstream& OutDataFile)
{
string OutFileName;
GetOutFileName(OutFileName);
OutDataFile.open(OutFileName.c_str());
if (!OutDataFile)
{
cout << "File Failed to open...Program terminated" << endl;
return 1;
}//end if
//ReadFile(TotalGPA);
return 0;
}//end OpenInFile
//---------ReadFile
//---------read the user specified file
int ReadFile (double & TotalGPA)
{
ifstream InDataFile;
int RowNumber;
int NoOfAssignments;
float TotalPtsAllowed;
int index;
float TotalScore;
int Grades;
double Weight;
double Average;
int Assignments[3];
//int GradeArray[50];
//double WeightArray[3];
OpenInFile(InDataFile);
RowNumber = 0;
while (RowNumber < 3)
{//start outter loop
InDataFile >> NoOfAssignments >> TotalPtsAllowed;
index = 0;
TotalScore = 0;
Assignments[index] = NoOfAssignments;
cout << Assignments[index] << endl;
//cout << Assignments[0] << Assignments[1] << Assignments[2] << endl;
while (index < NoOfAssignments)
{//start (inner loop)
InDataFile >> Grades;
index++;
TotalScore = TotalScore + Grades;
}//end while (inner loop)
InDataFile >> Weight;
cout << TotalScore << " " << TotalPtsAllowed << endl;
Average = TotalScore / TotalPtsAllowed;
//Average = (TotalScore / TotalPtsAllowed) * Weight;
TotalGPA = TotalGPA + Average;
RowNumber++;
}//end while (outer loop)
cout << "Total GPA is:: " << TotalGPA << endl;
GradeStructure.NoOfHomeWorkGrades = Assignments[0];
GradeStructure.NoOfProjectGrades = Assignments[1];
GradeStructure.NoOfExamGrades = Assignments[2];
cout << GradeStructure.NoOfHomeWorkGrades << endl;
cout << GradeStructure.NoOfProjectGrades << endl;
cout << GradeStructure.NoOfExamGrades << endl;
return 0;
}//end ReadFile
//-----------WriteData
//-----------write the data to user specified file
int WriteData (char & LeterGrade, string& Quote)
{
//char LetterGrade;
//string Quote;
ofstream OutDataFile;
OpenOutFile(OutDataFile);
OutDataFile << "NAME: A.C. Millard" << endl;
OutDataFile << "Your Letter Grade is ::" << LeterGrade << endl;
OutDataFile << Quote << endl;
return 0;
}//end WrtieData
//----------GetComment
//----------Create witty comment based on the letter grade
string GetComment (char & LetterGrade)
{
//char LetterGrade;
//LetterGrade=GetLetterGrade();
string Quote;
//cout << LetterGrade << endl;
switch (LetterGrade)
{
case 'A' : Quote = "The Fonz says...Exactamundo!" ;
break ;
case 'B' : Quote = "The Fonz says..Aaaaeeeyyy!" ;
break ;
case 'C' : Quote = "The Fonz says he was wrrroooogh about you." ;
break ;
case 'D' : Quote = "The Fonz says...Step into my office." ;
break ;
case 'F' : Quote = "The Fonz says Whoa!" ;
break ;
}
cout << Quote << endl;
WriteData(LetterGrade, Quote);
return Quote;
}//end GetComment
//---------GetLetterGrade
//---------Get the letter grade based on the average
void GetLetterGrade(double & TotalGPA)
{
//cout << TotalGPA << endl;
//double TotalGPA;
char LetterGrade;
if (TotalGPA > 3.50)
LetterGrade = 'A' ;
else
{
if (TotalGPA >= 2.40 && TotalGPA < 3.50)
LetterGrade = 'B' ;
if (TotalGPA >= 1.40 && TotalGPA < 2.40)
LetterGrade = 'C' ;
if (TotalGPA >= 0.040 && TotalGPA < 1.40)
LetterGrade = 'D' ;
if (TotalGPA < .40)
LetterGrade = 'F' ;
}//end else
cout << "Your Letter Grade is:: " << LetterGrade << endl;
GetComment(LetterGrade);
//return LetterGrade;
}//end GetLetterGrade
//----------CalculateGrade
//----------perform Grade Calcualtions
int CalculateGrade()
{
int Weight;
int Grade;
int TotalScore;
int TotalPtsAllowed;
Grade=(TotalScore / TotalPtsAllowed) * Weight;
return Grade;
}
Jul 12, 2013 at 1:20am UTC
After a quick read it seems to me that you reset 'index' to 0 every time the outer loop starts (line 138), so 'Assignments[1/2]' are never initialized.
Jul 12, 2013 at 1:33am UTC
I was thinking that too but when I move "index=0" out of the loop, it messes up my read of "Grades" and everything goes haywire plus it reads correctly through the loop.
Jul 12, 2013 at 2:43am UTC
Can't you use 'rowNumber' to assign to 'Assignments'?
Topic archived. No new replies allowed.