Need Help - Concerning Structs, Functions, and Arrays

This is a simple homework assignment. I should know how to do this already because I passed intro to C++. But I guess I don't have the programmer's mindset because I just can't get my head wrapped around this.

The Problem is I need to make a program that asks for 20 different records. Each record holds a first and last name, and a test score. The program is then supposed to output all the data entered, list a letter grade for each test score, then show the highest score.

Simple, well I can't figure it out. The damn book is no help. The program must be done using a struct to hold the data and 4 functions to perform all the tasks. As well as an array of sorts to hold the records of 20 different students.

Here's what I've done so far. I stopped here because FirstName and LastName is not being saved properly. I can get Grade and TestScore to save properly but not the name and I have no clue why.

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.h>
#include<conio.h>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

struct studentType
{
    string FName;
    string LName;
    int testScore;
    char grade;
};

int main()
{
    void studentData();
    void getGrade();
    void highestScore();
    void printData();
    
    studentData();
    
    getGrade();
    
    highestScore();
    
    printData();

getch();
}

void studentData()
{
    studentType student;
    cout<<"Hey"<<endl;
    cin>>student.FName>>student.LName;
    cin>>student.testScore;
}

void getGrade()
{
    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';
}

void highestScore()
{
    cout<<"Hi!"<<endl;
}

void printData()
{
//Last name, First Name, Score, Grade
    studentType student;
    cout<<student.LName<<", "<<student.FName<<" : "<<
        student.testScore<<" : "<<student.grade;
}


-summary-
I would like some help and tips on how to complete the assignment. ASAP if possible. I really just want to know why the heck first and last name isnt being saved and carried over. When I run the program, I enter the data but when the data is output back onto the screen, only TestScore and Grade are being shown. As if it's not saving the name entry.

Gah!
Your problem is that you've got a local declaration of "studentType student" in every function - and as they're LOCAL declarations the data never survive function termination.

My guess is that every time you REdeclare this variable the compiler is allocating the same section of memory to this REdeclared variable "student". This would mean that the two strings "FName" and "LName" are initialized to a null string everytime a variable of type studentType is created (i.e. within each function), but the integer "testScore" just happens to contain whatever happened to be in that memory location at the time the LOCAL variable is declared. In this case, of course, it just happens to be whatever was stuck there by the function studentData().

You can see how this works if you change the variable declarations in the function printData() - you STILL get the same test score you entered earlier because the memory allocations are the same - run the following and see - ask yourself where the value of anotherStudent.testScore in printData() came from and why it just happens to be the same as the value of student.testScore given in studentData();

Note: I changed iostream.h to iostream as the former is deprecated

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
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

struct studentType
{
    string FName;
    string LName;
    int testScore;
    char grade;
};

int main()
{
    void studentData();
    void getGrade();
    void highestScore();
    void printData();

    studentData();

    getGrade();

    highestScore();

    printData();

getch();
}

void studentData()
{
    studentType student;
    cout<<"Hey"<<endl;
    cin>>student.FName>>student.LName;
    cin>>student.testScore;
}

void getGrade()
{
    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';
}

void highestScore()
{
    cout<<"Hi!"<<endl;
}

void printData()
{
    // Just to prove the point ----
    // The following is a TOTALLY DIFFERENT VARIABLE but still
    // gives the same test score entered in studentData() !!!!!
    studentType anotherStudent;
    cout<<anotherStudent.LName<<", "<<anotherStudent.FName<<" : "<<
        anotherStudent.testScore<<" : "<<anotherStudent.grade;
}


What this all boils down to;

1. the test score ISN'T being saved as you think -- it's just a fluke that it happens to remain in the same memory location AFTER studentData() terminates so everytime you create a LOCAL variable using "studentType student" the value student.testScore just happens to initialize to the value of that same memory location. The name data is lost because the strings initialize to NULL values.

2. you need to pass your parameters back to your calling function. From what I'm seeing above you don't have a good grasp of this so try reading up on parameter passing, return statements and the like in your text. Your program will work if you try using something like;

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
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

struct studentType
{
    string FName;
    string LName;
    int testScore;
    char grade;
};

studentType studentData();
void printData(studentType s);
studentType getGrade(studentType s);

int main()
{

    // declare a variable of type studentType and use studentData() to 
    // input the data and return it to the variable "student"
    studentType student = studentData();

    // pass student to getGrade() and return it with the grade now calculated
    student = getGrade(student);

    // pass student to printData() to display values on screen
    printData(student);

getch();
}

studentType studentData()
{
    studentType student;
    cout<<"\n\nEnter student details (first name, last name, test score): ";
    cin>>student.FName>>student.LName;
    cin>>student.testScore;
    // return the entered data to the calling function by using "return"
    return student;
}

void printData(studentType s)
{
    cout << "\n\nstudent data : ";
    cout << s.LName << ", "
         << s.FName << " : "
         << s.testScore <<" : "
         << s.grade;
}

studentType getGrade(studentType s)
{

    if (s.testScore >= 90)
        s.grade = 'A';
    else if (s.testScore >= 80)
        s.grade = 'B';
    else if (s.testScore >= 70)
        s.grade = 'C';
    else if (s.testScore >= 60)
        s.grade = 'D';
    else
        s.grade = 'F';
    return s;                  // returns the values in s to the calling function
}


Of course, you'll still need to complete the rest of the assignment, but the use of an array of 20 x studentType and us of loops should do the trick.
Last edited on
Topic archived. No new replies allowed.