Student records problem?

Howdy all, my assignment is to define a structure for student grade records.
define a structure for student grade records.
2. declare an array of records and allocate on the Heap.
3. populate from the parallel arrays.
4. define a function to compute average and
populate average field.
5. define a function to display name and average.
6. demonstrate your functions and program

I am getting an error on line 10 saying i need to return an int? But it is a void function so it should work. Also any help with how to get my average to work would be awesome!

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
 #include <iostream>
#include <iomanip>
#include "studentrec.h"
using namespace std;

studentrec populateRecord();
void computeavg ( studentrec& );
void showRecord( const studentrec& );

void main()
{
    studentrec p = populateRecord();    

    computeavg( p );

    showRecord( p );
    
    return 0;//seeing it this fixed it, it didnt. 
}

studentrec populateRecord()
{
    studentrec t;           

    cout<<"Enter Name: ";
    getline( cin, t.name );
    cout<<"Enter scores: ";
    cin >> t.scores;
    cout<<endl;

    return t;            

} 

//void computeavg(studentrec& p) 
//{
//   // p.avg = t.scores/4;
//
//}

void showRecord( const studentrec& p )
{
    cout<<fixed<<setprecision(2);
   
    cout<<"Name: "<<p.name<<endl
        <<"Scores: "<<setw(8)<<p.scores<<endl
        <<"Average:  "<<setw(8)<<p.avg<<endl
        <<endl;

}//end showRecord 


Structure:
1
2
3
4
5
6
7
8
9
10
11
#include <string>
using namespace std;
   
        struct studentrec       //user defined data type
        {
            string name;    //fields(structure members)
            double scores;
            double avg;
            
        };
            
Last edited on
Where is your structure?
Last edited on
Also, your main function has a "return 0;" (line 18) at then end. Take that out, should fix your compile error

Why don't you try creating a variable in your structure that acts as an accumulator of grades, then divides by the amount of scores input? This could be easier.
Last edited on
Added it in OP. I forgot to add it in, my apologies.


P.S. i tried taking out the return and i still get
1
2
3
4
5
6
7
8
9
10

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/studentrecords
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp
main.cpp:10: error: '::main' must return 'int'
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
Last edited on
Have you tried changing main to an int function, and leaving the return?
That fixed it! i also commented out the computeavg() i think that was giving it other errors. I dont know how i didnt think to try that.

What about my average? If i create a variable in my structure like
 
double total;


How do i assign the entered numbers to add up into that variable?
I would definitely use a double for an average.

You would want to make something that can accumulate numbers:
1
2
3

   structname.total += structname.testScore; //Use something like this to store all scores 
                                                 //into a single variable. Then divide 

Last edited on
Like this?

1
2
3
4
5
6
void computeavg(studentrec& p) 
{
    studentrec.total += studentrec.scores;
    p.avg = studentrec.total / 4;

}

Yeah, that should work. Did it?
Apparently not.

1
2
3
4
5
6
7
8
9
10
11
12
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/studentrecords
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp
main.cpp: In function 'studentrec populateRecord()':
main.cpp:30: error: expected unqualified-id before '.' token
main.cpp: In function 'void computeavg(studentrec&)':
main.cpp:38: error: expected primary-expression before '.' token
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


Also i just found this but when i input my scores i can only input one score.

1
2
3
4
5
6
7
8
9
10
11
12
studentrec populateRecord()
{
    studentrec t;           

    cout<<"Enter Name: ";
    getline( cin, t.name );
    cout<<"Enter scores: ";
    cin >> t.scores;
    cout<<endl;
    
    return t;            


would i have to either loop that or should i make it into an array? Or is there a simpler way?
It would be best to make an array of structures so that you can store a name and score for each student,
1
2
3
4
5
6
struct Planets
{
	string planetName;
	int distanceFromSun;
	double surfaceGravityFactor;
} planets_t[8];


That will create an array to store those structure variables, for each planet. Then you can modify your accumulator to traverse through each index and sum up the score for each student
1
2
3
4
5
6
int i = 0;
While ( i < sizeofarray)
{
     structname.average += structname.testScore[i];
     i++;
}
Okay, i am no good at decrypting examples that do not pertain to my problem. But i gave it a shot and heres what i got:

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 <iomanip>
#include "studentrec.h"
using namespace std;

studentrec populateRecord();
void computeavg ( studentrec& );
void showRecord( const studentrec& );

int main()
{
    studentrec p = populateRecord();    

    //computeavg( p );

    showRecord( p );
    
    return 0;
}

studentrec populateRecord()
{
    studentrec t;           

    cout<<"Enter Name: ";
    getline( cin, t.name );
    cout<<"Enter scores: ";
    cin >> t.score_t[4];
    cout<<endl;
    
    return t;            

} 

void computeavg(studentrec& p) 
{
    int i = 0;
while ( i < 4)
{
     p.avg += p.score_t[i];
     i++;
}
    
}

void showRecord( const studentrec& p )
{
    cout<<fixed<<setprecision(2);
   
    cout<<"Name: "<<p.name<<endl
        <<"Scores: "<<setw(8)<<p.scores_t[4]<<endl
        <<"Average:  "<<setw(8)<<p.avg<<endl
        <<endl;

}

//header

include <string>
using namespace std;
   
        struct studentrec       //user defined data type
        {
           
            string name;    //fields(structure members)
           
            double avg;
            int score_t[4];
            
        }
            


Not sure if i declared that correctly in my header, or used it correctly in my code for that matter. I appreciate your patience, i just took 3 tests today so i am a bit frazzled still.
You are close! But do this:
1
2
3
4
5
6

struct name
{
     string name;
     int score_t;
}students[4]; 


This will make it so that you can store data for 4 students (potentially), or 4 scores.

No worries, if you have questions PM me.
Got it all to work! thanks! I hope to one day be a wizard like you. :D
Topic archived. No new replies allowed.