back function

Hello guys,Im having a bit of a problem here. You see I need to create a menu where I can choose different options with different functions. Like here:

Subject registration system:
1-Students
2-Enrolled subjects
3-Exit.

I need to choose option number 1 to access students menu.
and here it what will show up if open students menu.

Students
1 – Add Student
2 – Edit Student
3 – Delete Student
4 – List Students
5 – Back
...


So my problem is I cant choose item number 5 cause the compiler says that the function was not declared. heres my code. Please reply. thanks


#include<iostream>
using namespace std;
string student[20];
int studentctr;


void changename()
{
cout<<"Enter new student name: ";
string newstudentname;
cin>>newstudentname;
student[studentctr]=newstudentname;
cout<<"Student was successfully edited."<<endl;
}


void editstudent()
{
int y;
string oldstudentname;
cout<<"Enter name of the student name to be edited: ";
cin>>oldstudentname;
while (y<=student[studentctr].length())
{
if (oldstudentname==student[y])
{
changename();
}
y++;
}
}

void outputstudents()
{
cout<<"Enrolled students: "<<endl<<endl;
int x;
while (x<=studentctr)
{
cout<<student[x]<<endl;
x++;
}
cout<<endl;
}

void addstudent()
{
string student1;
cout<<"Enter name of the student: ";
cin>>student1;
student[studentctr]=student1;
studentctr++;
cout<<"New student was successfully added."<<endl<<endl;
}

void students()
{
while(true)
{
int choicehere;
cout<<"Students"<<endl;
cout<<"1-Add student"<<endl;
cout<<"2-Edit student"<<endl;
cout<<"3-Delete student"<<endl;
cout<<"4-List students"<<endl;
cout<<"5-Back"<<endl;
cout<<">> ";
cin>>choicehere;
if (choicehere==1)
{
addstudent();
}
if (choicehere==2)
{
editstudent();
}
if (choicehere==4)
{
outputstudents();
}
if (choicehere==5)
{
continue;
}
}
}

void menu()
{
int choice;
cout<<"Subject Registration System"<<endl;
cout<<"1-Students"<<endl;
cout<<"2-Enrolled Students"<<endl;
cout<<"3-Exit"<<endl;
cout<<">> ";
cin>>choice;

if (choice==1)
{
students();
}
}


main()
{
menu();
system("pause");
}
Last edited on
sup my nigga hugmepls

Guessing this is what you wanted. Here's what I did. For choice 5, you were choosing to continue. All that continue does it skip the rest of that while loop and go back to the beginning and run through it again. If you want to exit the while loop, you need to use break; . Another option alternatively, since exiting the function would also exit the while loop, you could use return;

With this change, your program ends when the user hits 5. This isn't good. So what I did was I put the main function inside of a while loop. By doing this, once your program returns to the main function, it'll run the menu() function again. I also removed the system("pause") after menu() because there's no point to have it there. Please respond if this works so I know.


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
#include<iostream>
using namespace std;
string student[20];
int studentctr;


void changename()
{
cout<<"Enter new student name: ";
string newstudentname;
cin>>newstudentname;
student[studentctr]=newstudentname;
cout<<"Student was successfully edited."<<endl;
}


void editstudent()
{
int y;
string oldstudentname;
cout<<"Enter name of the student name to be edited: ";
cin>>oldstudentname;
while (y<=student[studentctr].length())
{
if (oldstudentname==student[y])
{
changename();
}
y++;
}
}

void outputstudents()
{
cout<<"Enrolled students: "<<endl<<endl;
int x;
while (x<=studentctr)
{
cout<<student[x]<<endl;
x++;
}
cout<<endl;
}

void addstudent()
{
string student1;
cout<<"Enter name of the student: ";
cin>>student1;
student[studentctr]=student1;
studentctr++;
cout<<"New student was successfully added."<<endl<<endl;
}

void students()
{
while(true)
{
int choicehere;
cout<<"Students"<<endl;
cout<<"1-Add student"<<endl;
cout<<"2-Edit student"<<endl;
cout<<"3-Delete student"<<endl;
cout<<"4-List students"<<endl;
cout<<"5-Back"<<endl;
cout<<">> ";
cin>>choicehere;
if (choicehere==1)
{
addstudent();
}
if (choicehere==2)
{
editstudent();
}
if (choicehere==4)
{
outputstudents();
}
if (choicehere==5)
{
break;
}
}
}

void menu()
{
int choice;
cout<<"Subject Registration System"<<endl;
cout<<"1-Students"<<endl;
cout<<"2-Enrolled Students"<<endl;
cout<<"3-Exit"<<endl;
cout<<">> ";
cin>>choice;

if (choice==1)
{
students();
}
}


int main()
{
	while (true)
	{
		menu();
	}
  return 0; //End of main function
} 
Last edited on
It helped. thanks a lot man. If you have time please check the edit students. seems like theres something wrong with that function.
Youre the guy from yesterday. I owe you two now.
Yo dawg sorry about that wait I was writing on another post you know what i'm saying

so check this out i changed a few things i got it working but u gotta understand what you were doing wrong or else you never gonna get better

First off I changed the way the void changename function worked. If you notice in the parentheses, there is now a parameter called int StudentPosition. Whatever integer variable you pass in the parentheses will be usable by that function when it is called. This is necessary so you know which element in your student string array you need to be editing. You were basing it off the studentctr before which wont work because that doesnt tell the function which name you're editing. I also changed your edit function, as well as the outputstudents function. In outputstudents, you have int x; You should have int x=0; You are trying to compare x to something before you set it to anything. Keep in mind, that this will result in unexpected behavior and will essentially break your program. You need to declare what your variable is set to before comparing it to ANYTHING.

You did the same thing in the edit function. You had int y; You need int y=0; because of the fact that y needs to start at 0, not undefined which is what it is when you have int y; Also notice in the edit function now it calls changename(y). This means that it is sending the value that y has into that function for when it is called. The reason i'm doing this is because y will hold the position in the string student array that needs to be changed. Example, if we are changing the second student (which will be at array position 1 since position0=student 1), then y will be = to 1 when that function is called. Going into the changename function, StudentPosition will be set to 1 when it starts, and then that function will be carried out.

Hopefully this makes sense to you

Edit: Also changed int studentctr; to int studentctr=0;

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
#include<iostream>
using namespace std;
string student[20];
int studentctr=0;


void changename(int StudentPosition)
{
cout<<"Enter new student name: ";
string newstudentname;
cin>>newstudentname;
student[StudentPosition]=newstudentname;
cout<<"Student was successfully edited."<<endl;
}


void editstudent()
{
int y=0;
string oldstudentname;
cout<<"Enter name of the student name to be edited: ";
cin>>oldstudentname;
while (y<=student[studentctr].length())
{
if (oldstudentname==student[y])
{
changename(y);
}
y++;
}
}

void outputstudents()
{
cout<<"Enrolled students: "<<endl<<endl;
int x=0;
while (x<=studentctr)
{
cout<<student[x]<<endl;
x++;
}
cout<<endl;
}

void addstudent()
{
string student1;
cout<<"Enter name of the student: ";
cin>>student1;
student[studentctr]=student1;
studentctr++;
cout<<"New student was successfully added."<<endl<<endl;
}

void students()
{
while(true)
{
int choicehere;
cout<<"Students"<<endl;
cout<<"1-Add student"<<endl;
cout<<"2-Edit student"<<endl;
cout<<"3-Delete student"<<endl;
cout<<"4-List students"<<endl;
cout<<"5-Back"<<endl;
cout<<">> ";
cin>>choicehere;
if (choicehere==1)
{
addstudent();
}
if (choicehere==2)
{
editstudent();
}
if (choicehere==4)
{
outputstudents();
}
if (choicehere==5)
{
break;
}
}
}

void menu()
{
int choice;
cout<<"Subject Registration System"<<endl;
cout<<"1-Students"<<endl;
cout<<"2-Enrolled Students"<<endl;
cout<<"3-Exit"<<endl;
cout<<">> ";
cin>>choice;

if (choice==1)
{
students();
}
}


int main()
{
	while (true)
	{
		menu();
	}
  return 0; //End of main function
} 
Last edited on
So that's why the function wont work. Thanks a lot man. I'll review this over and over and try some other problems. Again, Thanks.
Topic archived. No new replies allowed.