Why am I getting garbage values

The program out puts almost correctly, but does not give me the grade,instead gives me garbage values.
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
  #include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

struct StudentType
{
string studentFname;
string studentLname;
int testScore;
char grade;
};

void openFile(ifstream &inFile);
int readData(ifstream &inFile, StudentType grades[20]);
char letterGrade();
void outputData(StudentType grades[], int size);


int main()
{
StudentType grades[20];
int size = 20;

ifstream inFile;
inFile.open("Ch9_Ex2Data.txt");

size = readData(inFile, grades);
letterGrade();
outputData(grades, size);
    
 return 0;
}


void openFile(ifstream &inFile)
{
inFile.open("Ch9_Ex2Data.txt");
if(!inFile)
{
cout << "File did not open! Program terminating!!";
   exit(0);
}
}

int readData(ifstream &inFile, StudentType grades[])
{
int size = 0;
while(!inFile.eof())
{
inFile >> grades[size].studentFname
       >>grades[size].studentLname 
       >> grades[size].testScore;
        size++;
}
return size;
}
char letterGrade()
{
int testScore =0;
char grade = 0;
int size= 0;
for (int i=0; i < size; i++  )
    if (testScore >= 90)
	{
       grade = 'A';
    }
	else if (testScore >= 80)
	{
       grade = 'B';  
    } 
	else if (testScore >= 70) 
    {
       grade = 'C';  
    } 
    else if (testScore >= 60) 
    {
       grade = 'D';  
    }
    else 
    {
       grade = 'F';  
    } 

    return grade;
}

void outputData(StudentType grades[], int size)
{
	
for(int i = 0; i < size; i++)
    cout  << left << grades[i].studentLname<<", "
    << grades[i].studentFname 
    <<" "<< grades[i].testScore 
    <<  grades[i].grade << endl;
}

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
char letterGrade()
{
int testScore =0;
char grade = 0;
int size= 0;
for (int i=0; i < size; i++  )
    if (testScore >= 90)
	{
       grade = 'A';
    }
	else if (testScore >= 80)
	{
       grade = 'B';  
    } 
	else if (testScore >= 70) 
    {
       grade = 'C';  
    } 
    else if (testScore >= 60) 
    {
       grade = 'D';  
    }
    else 
    {
       grade = 'F';  
    } 

    return grade;
}

Have you any clue what you here doing? E.g,, what happens with the loop if 'size' was set to 0?
I tried running this many different ways in attempts to solve the garbage being printed. I am trying to run through the input file, get the testScore, and have the loop determine what the grade is, in the end returning grade
Would you not use your grades[] array for accessing the values of this inside your letterGrade() function?
Last edited on
I still get the garbage values. He is what I have using the array. I' thinking I need to be asking for input of testScore for the letterGrade function? but I am not exactly sure how to, and if that is ever correct thinking
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

struct StudentType
{
	string studentFname;
	string studentLname;
	int testScore;
	char grade;
};

void openFile(ifstream &inFile);
int readData(ifstream &inFile, StudentType grades[20]);
char letterGrade(StudentType grades [20]);
void outputData(StudentType grades[20], int size);


int main()
{
    StudentType grades[20];
    int size = 20;

    ifstream inFile;
	inFile.open("Ch9_Ex2Data.txt");

	size = readData(inFile, grades);
	letterGrade(grades);
    outputData(grades, size);
    
    return 0;
}


void openFile(ifstream &inFile)
{
	inFile.open("Ch9_Ex2Data.txt");
	if(!inFile)
	{
		cout << "File did not open! Program terminating!!";
		exit(0);
	}
}

int readData(ifstream &inFile, StudentType grades[])
{
int size = 0;
while(!inFile.eof())
{
    inFile >> grades[size].studentFname
           >>grades[size].studentLname 
           >> grades[size].testScore;
		    size++;
	}
return size;
}
char letterGrade(StudentType grades [20])
{
int testScore=0;
char grade=0;
int size=0;
for (int i=0; i < size; i++  )
    if (testScore >= 90)
	{
       grade = 'A';
    }
	else if (testScore >= 80)
	{
       grade = 'B';  
    } 
	else if (testScore >= 70) 
    {
       grade = 'C';  
    } 
    else if (testScore >= 60) 
    {
       grade = 'D';  
    }
    else 
    {
       grade = 'F';  
    } 

    return grade;
}

void outputData(StudentType grades[20], int size)
{
	
for(int i = 0; i < size; i++)
    cout  << left << grades[i].studentLname<<", "
    << grades[i].studentFname 
    <<" "<< grades[i].testScore 
    <<  grades[i].grade << endl;
}

Last edited on
You have to set the 'grade' values of all your 'StudentType' objects of your 'grades[]' array.

Therefore you need access to it at your letterGrade() code. So it's best to pass a reference of this into your letterGrade() function. Assuming, you will grading at each invocation of your letterGrade() function a single student, your letterGrade() function looks like this:
1
2
3
4
5
6
7
8
void letterGrade( StudentType & student )
{
    if (student.testScore >= 90) student.grade = 'A';
    else if (student.testScore >= 80) student.grade = 'B';
    else if (student.testScore >= 70) student.grade = 'C';
    else if (student.testScore >= 60) student.grade = 'D';
    else student.grade = 'F';
}

Then you have to loop int your main() function through all StudentType objects of grades[]:
1
2
3
4
5
6
7
8
9
int main()
{
    ....
    for (int i = 0;  i < size;  ++i)
    {
        letterGrade( grades[i] );
    }
    ...
}
The other way is, like your try, passing the whole array to the letterGrade() function. But then it should better named to letterGrades. Also, you need to pass the size of grades[] because plain arrays will be passed as pointers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void letterGrades(StudentType grades[], int size)
{
    for (int i = 0;  i < size;  ++i)
    {
        char grade;
        int testScore= grades[i].testScore;

        if (testScore >= 90) grade = 'A';
	else if (testScore >= 80) grade = 'B';  
	else if (testScore >= 70) grade = 'C';  
        else if (testScore >= 60) grade = 'D'; 
        else grade = 'F';

        grades[i].grade = grade;
    } 
}
Last edited on
Still outputting garbage values. I am obviously missing something. Here is part of what the output looks like.


Tomek, Chelsea 95ª
Clodfelter, Angela 95ª
Nields, Allison 95
Norman, Lance 88
AWAVI‰×AUATL%¶ UH-¾ SA‰ýI‰öL)åHƒìHÁýèöÿÿH…ít 1Û„L‰úL‰öD‰ïAÿÜHƒÃH9ÝuêHƒÄ[]A\A]A^A_Ðf.„óÃHƒìHƒÄÃCh9_Ex2Data.txtFile did not open! Program terminating!!, ;´&öÿÿ`÷ÿÿ(p÷ÿÿÐzøÿÿ€åùÿÿ¨MúÿÿÈ*ûÿÿè(üÿÿXýÿÿ(¡ýÿÿH¶ýÿÿ@Èýÿÿ`Úýÿÿ
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

struct Student
{
    string Fname;
    string Lname;
    int    score;
    char   grade;
};

void openFile(ifstream& inFile);
int  readData(ifstream& inFile, Student grades[]);
char letterGrade(int score);
void outputData(const Student grades[], int size);

const char* const InputFile = "Ch9_Ex2Data.txt";
const int MaxSize = 100;

int main()
{
    Student grades[MaxSize];

    ifstream inFile;
    openFile(inFile);

    int size = readData(inFile, grades);
    outputData(grades, size);
}

void openFile(ifstream& inFile)
{
    inFile.open(InputFile);
    if (!inFile)
    {
        cout << "Error: Cannot open input file.\n";
        exit(0);
    }
}

int readData(ifstream& inFile, Student grades[])
{
    int size = 0;
    while (size < MaxSize &&
           inFile >> grades[size].Fname
                  >> grades[size].Lname 
                  >> grades[size].score)
    {
        grades[size].grade = letterGrade(grades[size].score);
        ++size;
    }
    return size;
}

char letterGrade(int score)
{
    char grade;
    if      (score >= 90) grade = 'A';
    else if (score >= 80) grade = 'B';
    else if (score >= 70) grade = 'C';
    else if (score >= 60) grade = 'D';
    else                  grade = 'F';
    return grade;
}

void outputData(const Student grades[], int size)
{
    for (int i = 0; i < size; ++i)
        cout << left
             << setw(10) << grades[i].Lname << ' '
             << setw(10) << grades[i].Fname << ' '
             << right
             << setw(4) << grades[i].score << ' '
             << setw(4) << grades[i].grade << '\n';
}

Thank you !
Topic archived. No new replies allowed.