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
}
|