Define array of structure within a structure in C++

78 views Asked by At

I am trying to create a structure Student which contains q substructures named Course. Each Course is a structure with a credit and point int values.

How do I set up my Student structure to have an integer number of Course structures within it? Thanks

struct Student{
    int q;
    Course course[q];
};

struct Course{
    int credit;
    int point;
};

I tried this but VSC is telling me it is wrong.

Edit error:

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;


struct Course{
    string name;
    int credit;
    int point;
};

typedef struct Student{
    vector<Course> courses;
};



int main() {

    Student student;

    system("cls");
    int input;
    int input2;
    string strinput;
    cout<<"--------------------------------------------------------------------------"<<endl;
    cout<<"                    GPA & CGPA Calculator (Developed by Ohid)             "<<endl;
    cout<<"--------------------------------------------------------------------------\n"<<endl;
    cout<<"Set up the student!"<<endl;
    sub:
    cout<<"Add a course"<<endl;
    cout<<"Course name: ";
    cin>>strinput;
    cout<<"Course points: ";
    cin>>input;
    cout<<"Course credits ";
    cin>>input2;
    student.courses.push_back({strinput,input,input2});
    cout<<"Add another course (1/0)?: ";
    cin>>input;
    switch(input)
    {
        case 1:
            goto sub;
            break; 
        case 2:
            break;
    }
    
    int numCourses = student.courses.size();

    cout<<"--------------------------------------------------------------------------\n"<<endl;
    cout<<"Student created with "<<student.q<<" courses!"<<endl;

    cout<<"--------------------------------------------------------------------------\n"<<endl;
    cout<<"            MENU:"<<endl;
    cout<<"                   1. Calculate GPA (Grade Point Average)"<<endl;
    cout<<"                   2. Calculate CGPA (Cummulative Grade Point Average)"<<endl;
    cout<<"                   3. Method that is applied here for calclating GPA & CGPA"<<endl;
    cout<<"                   4. Exit Application"<<endl;
    cout<<"--------------------------------------------------------------------------"<<endl;
    sub:
    cout<<"Enter your choice: ";
    cin>>input;

    switch(input)
    {
        case 1:
            cout<<"You chose calculate GPA";
            calculateGPA();
            break;
        case 2:
            cout<<"You chose calculate CGPA";
            calculateCGPA();
            break;
        case 3:
            cout<<"You chose method";
            method();
            break;
        case 4:
            cout<<"You chose to exit";
            exit(EXIT_SUCCESS);
            break;
        default:
            cout<<"You are stupid";
            goto sub;
            break;
    }
}



void calculateGPA()
{

}

void calculateCGPA()
{

}

void method()
{


}

Error text! 'Course': undeclared identifiercpp(C2065) 'std::vector': 'Course' is not a valid template type argument for parameter '_Ty'cpp(C2923) 'std::vector': too few template argumentscpp(C2976) class std::vector

Fixed std etc.

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <string>




struct Course{
    std::string name;
    int credit;
    int point;
};

typedef struct Student{
    std::vector<Course> courses;
};



int main() {

    Student student;

    system("cls");
    int input;
    int input2;
    std::string strinput;
    std::cout<<"--------------------------------------------------------------------------"<<std::endl;
    std::cout<<"                    GPA & CGPA Calculator (Developed by Ohid)             "<<std::endl;
    std::cout<<"--------------------------------------------------------------------------\n"<<std::endl;
    std::cout<<"Set up the student!"<<std::endl;
    sub:
    std::cout<<"Add a course"<<std::endl;
    std::cout<<"Course name: ";
    std::cin>>strinput;
    std::cout<<"Course points: ";
    std::cin>>input;
    std::cout<<"Course credits ";
    std::cin>>input2;
    student.courses.push_back({strinput,input,input2});
    std::cout<<"Add another course (1/0)?: ";
    std::cin>>input;
    switch(input)
    {
        case 1:
            goto sub;
            break; 
        case 2:
            break;
    }
    
    int numCourses = student.courses.size();

    std::cout<<"--------------------------------------------------------------------------\n"<<std::endl;
    std::cout<<"Student created with "<<numCourses<<" courses!"<<std::endl;

    std::cout<<"--------------------------------------------------------------------------\n"<<std::endl;
    std::cout<<"            MENU:"<<std::endl;
    std::cout<<"                   1. Calculate GPA (Grade Point Average)"<<std::endl;
    std::cout<<"                   2. Calculate CGPA (Cummulative Grade Point Average)"<<std::endl;
    std::cout<<"                   3. Method that is applied here for calclating GPA & CGPA"<<std::endl;
    std::cout<<"                   4. Exit Application"<<std::endl;
    std::cout<<"--------------------------------------------------------------------------"<<std::endl;
    sub:
    std::cout<<"Enter your choice: ";
    std::cin>>input;

    switch(input)
    {
        case 1:
            std::cout<<"You chose calculate GPA";
            calculateGPA();
            break;
        case 2:
            std::cout<<"You chose calculate CGPA";
            calculateCGPA();
            break;
        case 3:
            std::cout<<"You chose method";
            method();
            break;
        case 4:
            std::cout<<"You chose to exit";
            exit(EXIT_SUCCESS);
            break;
        default:
            std::cout<<"You are stupid";
            goto sub;
            break;
    }
}



void calculateGPA()
{

}

void calculateCGPA()
{

}

void method()
{


}
0

There are 0 answers