Printing class information

I'm doing this assignment for my class. I have gotten the program to ork but I am having an issue.
When the program prints itself. It prints the information that is given in the array and then prints the random values for the constants.
For example if i input the value for 1 student and the constant is 30. It will print the value for the first student as it is supposed to, but then it will also print the next 29 students with random information.

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
#include <cstdlib>
#include <iostream>
#include "student.h"
#include <string>
using namespace std;

const int MAX_STUDENT=30;
typedef Student studentArrayType[MAX_STUDENT];
void printMeFirst();



int main(int argc, char** argv) 

{
    
    printMeFirst();
    
    Student s1;
    Person teacher;
    studentArrayType studentArray;
    
    string grade;
    string dummy; 
    
    
    teacher.firstName = "Ron"; 
    teacher.lastName = "Sha";
    
    cout << "CS116: " << teacher.firstName << " " << teacher.lastName << endl;



     for (int i = 0; i < MAX_STUDENT; i++)
    {
        cout << "Enter First name (enter Q if no more inputs): ";
        cin >> studentArray[i].name.firstName;
        if (studentArray[i].name.firstName == "Q")
            break;
    
        cout << "Enter Last name (enter Q if no more inputs): ";
        cin >> studentArray[i].name.lastName;

        cout << "Enter bird year: ";
        cin >> studentArray[i].birthYear;
    
        cout << "Enter birth month: ";
        cin >> studentArray[i].birthMonth;

        cout << "Enter birth day: ";
        cin >> studentArray[i].birthDay;

        cout << "Enter percent: ";
        cin  >> studentArray[i].courseGrade.percent;

    
    }    
        // try to omit this line when compiling, see the difference
        // if you enter multiple words as inputs
        // get rid of cin buffer
        //getline(cin, dummy);
        cin.clear();
        cin.ignore(30, '\n');

        
        //fix print letter for number
       

    calculateGrade(studentArray, MAX_STUDENT); 

    cout << "Student Info: \n\n";
    printStudent(studentArray, MAX_STUDENT);
    
    return 0;
}


void calculateGrade(Student studentArray[], int size)
{
    // loop the entire student array and assign leter grade
    // as below: (last element of the array is either the end
    // of the array, or the student's first name is "Q"
    //
    // Letter grade based on grade percent: 
    //   A: 89 and above
    //   B: 79 and above and less than 89
    //   C: 69 and above and less than 79
    //   D: 59 and above and less than 69
    //   F: below 59     
    
    // your code below
    
    for (int i = 0; i < size; i++) 
    
    {
        if (studentArray[i].courseGrade.percent >= 89)
        {
            studentArray[i].courseGrade.grade = "A";
        }
    else if (studentArray[i].courseGrade.percent >= 79 && studentArray[i].courseGrade.percent < 89)
        {
            studentArray[i].courseGrade.grade = "B";
        }
    else if (studentArray[i].courseGrade.percent >= 69 && studentArray[i].courseGrade.percent < 79)
        {
            studentArray[i].courseGrade.grade = "C";
        }
    else if (studentArray[i].courseGrade.percent >= 59 && studentArray[i].courseGrade.percent < 69)
        {
            studentArray[i].courseGrade.grade = "D";
        }
    else if (studentArray[i].courseGrade.percent < 59)
        {
            studentArray[i].courseGrade.grade = "F";
        }
}
}
            
    
/*
 * Function:  printStudent
 *   print out all the student's information
 * 
 * @studentArray:  the array contains all the student's info
 * @size:  size of the studentArray
 * 
 * Output:
 *     print all all the student's info
 * Return: none
 * 
 */

void printStudent(Student studentArray[], int size)
{
    /*
     * print out all the student's infor:
     *    First name, last name, birthday (month/day/year), 
     *    course percent and course letter grade
     * 
     * Last element of the array is the end of the array or student's
     * first name is "Q"
     */
for (int i=0; i < size; i++)

{
        cout << studentArray[i].name.firstName << " is my first name." << endl;
        cout << studentArray[i].name.lastName << " is my last name." << endl;
        cout << "I was born on " << studentArray[i].birthDay << "//" << studentArray[i].birthMonth << "//" << studentArray[i].birthYear << endl;
        cout << "I got a " << studentArray[i].courseGrade.percent << " percent" << endl;
        cout << "I got an " << studentArray[i].courseGrade.grade << endl;
        cout << endl << endl;
        
}

}

//find variable to track how many students are in the array

//The function below will print out the progeammers name, class info, time, and date.
void printMeFirst()
{
    cout << "Program written by: " << "Hamza Sheikh" << endl;
    cout << "Course info: " << "Lab1: Students" << endl << "CS-116: Thursday class" << endl << endl;
    time_t now = time(0);

    char* t= ctime(&now);
    cout << "Date: " << t << endl << endl;
}

THE ARRAY FILES:
#ifndef STUDENT_H
#define STUDENT_H
#include<string>

using namespace std;

struct Person {
    string lastName, firstName;
};

struct GradeRec {
    float percent;
    string grade;
};

struct Student {
    Person name;
    int birthYear, birthMonth, birthDay;
    GradeRec courseGrade;
    
};


void calculateGrade(Student studentArray[], int size);
void printStudent(Student studentArray[], int size);
Last edited on
At lines 69
 
    calculateGrade(studentArray, MAX_STUDENT);

and line 72
 
    printStudent(studentArray, MAX_STUDENT);

the value MAX_STUDENT should not be used. Instead use an integer variable which stores the actual number of students which have been input.

That means you need to declare a new variable, call it something like student_count or num_students, a name to express its purpose. Set it to an initial value of 0 at the start of the program.

Then, inside the for loop (lines 34 to 57) add 1 to that variable each time a student record has been entered properly. Then use this count of the actual number of students when calling the other functions.

p.s. this question would be more suited to the Beginners forum, since it doesn't ask about anything specific to UNIX/Linux.

See also duplicate thread:
http://www.cplusplus.com/forum/general/198401/
http://www.cplusplus.com/forum/beginner/198400/

PLEASE don't make multiple posts for the same question.

See how it has created three times as much work for others.
Last edited on
Topic archived. No new replies allowed.