How to make a repeating menu

I want to have the printMenu function repeat until user enters 5 which exits the program. I tried placing printMenu() after every option with wrong results.
Is there another way I could achieve this?
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

//This function prints the menu options and prompts the user to enter a choice.
void printMenu(){
     cout <<"******* ASSIGNMENT 2 MENU*******" <<endl;
     cout <<endl;
     cout <<"1.   Course Information"<<endl;
     cout <<"2.   Course Summary Grade Sheet"<<endl;
     cout <<"3.   Student Grade Sheet"<<endl;
     cout <<"4.   Student Information"<<endl;
     cout <<"5.   Exit Menu"<<endl;
     cout <<endl;
     cout <<"Please enter your choice (1-5): ";

}

int searchCourses(Course courses[], int numCourses, string courseCode){
    int i;
    i=0;
	while (i<numCourses) {
        if(courses[i].courseCode == courseCode)
           return i;
        i=i+1;
    }
    return -1;
}

int searchGrades(GradeSheet grades[], int numEntries, string courseCode){
    int i;
    i=0;
	while (i<numEntries) {
        if(grades[i].gCode == courseCode)
           return i;
        i=i+1;
    }
    return -1;
}

void option1(Course courses[], int numCourses, string courseCode){
    int found;
    found=0;
    found=searchCourses(courses, numCourses, courseCode);
        if(found==-1){
            cout<<courseCode<<" was not found."<<endl;
            cout<<endl;
        }else
        if(found>=0){
            printCourse(courses[found]);
        }
}

void option2(GradeSheet grades[], int numEntries, string courseCode){
    GradeSheet g;
    int found;
    found=0;
    found=searchGrades(grades, numEntries, courseCode);
        if(found==-1){
            cout<<"No student was enrolled for "<<courseCode<<endl;
            cout<<endl;
        }else
        if(found>=0){
            printGrade(grades[found]);
        }
}


void option3(Student students[], int numStudents, GradeSheet grades[], int numEntries, int id){
    GradeSheet g;
    int found;
    int i;
    found=0;

    found=searchStudent(students, numStudents, id);
        if(found==-1){
            cout<<endl;
            cout<<"Student "<<id<<" was not found."<<endl;
            cout<<endl;
        }else
        cout<<endl;
        for(i=0; i<numEntries; i=i+1){
            if(grades[i].gId==grades[found].gId){
                printOption3(grades[i]);
            }
        }
        cout<<endl;

    }

int main(){
    Student students[100];
    int numStudents;
    numStudents=loadStudents(students);

    Course courses[100];
    int numCourses;
    numCourses=loadCourses(courses);

    GradeSheet grades[100];
    int numEntries;
    numEntries=loadGradeSheet(grades);

    int choice;
    int id;
    printMenu();
    string courseCode;
    cin>>choice;
    if(choice==1){
        cout<<"Enter a course code: ";
        cin>>courseCode;
        option1(courses, numCourses, courseCode);
    }else
    if(choice==2){
        cout<<"Enter a course code: ";
        cin>>courseCode;
        option2(grades, numEntries, courseCode);
    }
    if(choice==3){
        cout<<"Enter student id: ";
        cin>>id;
        option3(students, numStudents, grades, numEntries, id);
    }
    if(choice==5){
    return 0;
    }
}
  

repeat until

Have you looked at loops? ( for, while, do while )

They do repeat.
1
2
3
4
5
6
printMenu();
cin>>choice;

while (choice!=5)
{// run code
}
Last edited on
Topic archived. No new replies allowed.