The recent assignment I was given in my programming class had me utilize a structure. Structures are not covered for another 10 chapters in our textbooks, however I did read up on them as much as I could to try to figure this thing out. I am still unsure on how to use them. Here is the question and then the code that I came up with to answer it. My code is riddled with errors and I really need help trying to figure out what I am doing wrong.

1. Write a program that includes the following four steps:

Step 1.Create a structure with the name StudentRecord containing a Name stored as an array of 20 characters, an ID stored as an integer, and a GPA stored as a floating-point number. Create (instantiate) a variable from StudentRecord called TESCStudent.
Step 2. Assign the following values to the variable by assuming Name is SuperProgrammer, with an ID of 1234, and a GPA of 4.0.
Step 3. The program should print out on the screen that SuperProgrammer with an Identification number of 1234 has a 4.0 GPA. (Of course, your program needs to use the structure you defined in step 1.)
Step 4. Generalize the program so that you can input the student's name, ID, and GPA when the program is run. Please do not forget to include prompt statements in your program. Print out the user entered Name, ID, and GPA.

Make sure your program includes all four steps, not just steps 1 and 4.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

struct StudentRecord
{
       char name[ 20 ]; 
       int iD;
       float gPA;
};

class GradeBook
{
       
       GradeBook( char word[], int number, float grade )
       {
            setStudentName( word );
            setStudentID( number );
            setStudentGPA( grade );
       }
       
       void setStudentName( char* word )
       {
            StudentRecord name = word;
       }
       
       char getStudentName()
       {
            return StudentRecord name;
       }
       
       void setStudentID( int number)
       {
            StudentRecord iD = number;
       }
       
       int getStudentID()
       {
            return StudentRecord iD;
       }
       
       void setStudentGPA( float grade )
       {
            StudentRecord gPA = grade;
       }
       
       float getStudentGPA()
       {
            return StudentRecord gPA;
       }
       
       void displayMessage()
       {
            cout << "Student: " << getStudentName() << "  ID: " << getStudentID() << "  GPA: " << getStudentGPA() << endl;
       }
       void newStudent()
       {
            cout << "Please enter a new student's name ( 20 character limit with no spaces ):/n" << endl;
            cin >> name;
            cout << "Please enter " << getStudentName() << "'s ID:/n" << endl;
            cin >> iD;
            cout << "Please enter " << getStudentName() << "'s GPA:/n" << endl;
            
            displayMessage();
       }
};

int main()
{
    GradeBook TESCStudent( SuperProgrammer, 1234, 4.0 );
    
    TESCSudent.displayMessage();
    TESCStudent.newStudent();
    
    system( "pause" );
    
    return 0;
}

The fact that you have so many errors suggests to me that you need to start afresh, and create your program one bit at a time slowly. Don't write 100 lines of code without trying to compile it, write a few lines at a time, or one small function at a time, then compile/test - make sure the output is what you expect, and carry on. doing this takes 5 seconds, and will save you alot longer of wading through compiler errors.


When you go back to rewrite your code, keep the following things in mind:

Firstly, the struct and class keywords both do exactly the same thing in C++, except that the default access specifier for a struct is public. If you know how to use a class, then you know how to use a struct aswell.

Secondly, I strongly suggest you replace your usage of char[] and char* with string. using a raw character array to move string data around your program in C++ is an obsolete technique which will just cause you all kinds of problems.

finally, the default floating point type in C and C++ is double - you should prefer this to float.

Edited 6 Years Ago by Bench: n/a

hmm it does looks like you were lost :)

its not so hard. looks like you haven't failed in creating the structure.
the big bug is that the class "GradeBook" doesn;t knows any variable called StudentRecord.
After declaring a struct it acts almost like a variable: you can write:
StudentRecord myRecord;
after declaring myRecord as a StudentRecord you can set myRecord's members values like this:
myRecord.iD = value;
Tell me if you have any more problems

I took advice and used it but I still cant seem to get it to work. I am getting errors like "insufficient contextual information to determine type. Here is what I have now with the corrections

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

struct StudentRecord
{
       char name[ 20 ]; 
       int iD;
       float gPA;
};

class GradeBook
{
       StudentRecord myrecord();
       
       GradeBook( char* word, int number, float grade )
       {
            setStudentName( word );
            setStudentID( number );
            setStudentGPA( grade );
       }
       
       void setStudentName( char* word )
       {
            myrecord.name = word;
       }
       
       char getStudentName()
       {
            return myrecord.name;
       }
       
       void setStudentID( int number)
       {
            myrecord.id = number;
       }
       
       int getStudentID()
       {
            return myrecord.iD;
       }
       
       void setStudentGPA( float grade )
       {
            myrecord.gPA = grade;
       }
       
       float getStudentGPA()
       {
            return myrecord.gPA;
       }
       
       void displayMessage()
       {
            cout << "Student: " << getStudentName() << "  ID: " << getStudentID() << "  GPA: " << getStudentGPA() << endl;
       }
       void newStudent()
       {
            cout << "Please enter a new student's name ( 20 character limit with no spaces ):/n" << endl;
            cin >> name;
            cout << "Please enter " << getStudentName() << "'s ID:/n" << endl;
            cin >> iD;
            cout << "Please enter " << getStudentName() << "'s GPA:/n" << endl;
            
            displayMessage();
       }
};

int main()
{
    GradeBook TESCStudent( SuperProgrammer, 1234, 4.0 );
    
    TESCSudent.displayMessage();
    TESCStudent.newStudent();
    
    system( "pause" );
    
    return 0;
}

You didn't take any of his advice except for inserting the include and the using statement. You need to change lines 11,20,27 to reflect string variables instead of the char array and the two char * values.

You need to also change 13 and 47 to doubles.

This article has been dead for over six months. Start a new discussion instead.