how to scan an array?

To end my project I need to know how to scan an array(the array contains lines of strings entered by the user.)

please notice the function editstudent().
what happens is when the user chooses to execute that function, it stops and just goes back to the previous selection screen which is the students(). It doesn't go through the function changename(). I think there's something wrong with that line. Please help. Anyway thanks to Pindrought for helping me with my previous problems. Props to that guy.


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


void deletestudent()
{
int delstudent=0;
cout<<"Enter student name to be deleted: ";
cin>>delstudent;
int accu1=delstudent-1;
null=student[accu1];
}

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)
{
if (student[x]==" ")
{
continue;
}
else
{
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==3)
{
deletestudent();
}
if (choicehere==4)
{
outputstudents();
}
if (choicehere==5)
{
break;
}
}
}

void EnrolledSubjects()
{
while(true)
{
int eschoice;
cout<<"Enrolled Subjects "<<endl;
cout<<"1-Add Subject to Student"<<endl;
cout<<"2-Delete Subject from student"<<endl;
cout<<"3-View Subjects Enrolled by the Students"<<endl;
cout<<"4-Back"<<endl;
cout<<">> ";
cin>>eschoice;
if (eschoice==4)
{
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();
}
if (choice==2)
{
EnrolledSubjects();
}
}


int main()
{
while (true)
{
menu();
}
return 0;
}







Last edited on
Code tags, please.
1
2
3
4
5
6
7
string student[20];
y = 0;
while ( y <= student[studentctr].length() )
{
  student[y] ...
  ++y;
}

This has two errors:

1. <=, if array has N items, then N is not a valid index. The condition is still true, when y==N

2. Array has at most 20 names. You look at the length of one name, rather than the number of names.
That's the reason why it skips the function changename() and directly executes the student()?
Do your names contain whitespace?
nope. just first names.
Yo dawg

I believe the issue is with

1
2
3
4
5
6
while (y<=student[studentctr].length())
{
if (oldstudentname==student[y])
{
changename(y);
}



Try changing it to
1
2
3
4
5
6
while (y<=studentctr)
{
if (oldstudentname==student[y])
{
changename(y);
}


Think about what you're checking in that while loop. The way you had it, the # of names it would check is equal to the length of the last name entered which doesn't really make any sense. It makes more sense to check for the studentctr you had since the studentctr holds the total # of names entered.

Think about it like this.
If I enter John for the first name.
Susan for the second name.
And J for the third name.


So

John
Susan
J


With your while loop, it would look at the last entered name's length, so if I wanted to try to change J's name, the loop would start but your while would only run if y was <=1 since J is a length of 1 character.
It is worse than that. With three students, the studentctr is 3. student[3] has default initialized empty string. That <= would still allow comparing to John, which is a different error.

PS. Please explain:
1
2
3
4
5
6
7
8
9
void deletestudent()
{
int delstudent=0;
cout<<"Enter student name to be deleted: ";
cin>>delstudent; // As user, I would type Slartibartfast (although the code expects 42)

int accu1=delstudent-1; // ???
null=student[accu1]; // ???
}



I would strongly recommend replacing the array 'student' with a STL container like std::vector.
Last edited on
wtf. I really need a break. twas supposed to be a string. been dealing with this for like 7 hours already.
It's late, everyone can write voodoo code when they're tired haha.

Also, I can tell you're trying so i'm going to throw you a bone and show you how your deletestudent should be. Comments included for mass amounts of understandingness
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
void deletestudent()
{
	int studentdeleted=-1; //We will initialize it to -1 since we know that the array position can never be -1
							//This way we can verify that a student was actually deleted or not before subtracting from student count
	cout<<"Enter student name to be deleted: ";
	string StudentName;
	cin >> StudentName;
	int counter=0;
	while (counter<=studentctr) 
	{
		if (student[counter] == StudentName) //If we found our match
		{
			studentdeleted = counter; //The student deleted will be what our counter currently is
			break; //Break will exit the while loop once we have found our student position 
		}
		counter+=1;
	}

	//Now we need to verify that we did find a student position
	if (studentdeleted == -1) //If we didn't find one, display an error
	{
		cout << "Student not found." << endl;
	} 
	else //If we did find the student...
	{
		if (studentctr == studentdeleted) //If our student was the last student in the list
		{
			studentctr-=1; //subtract 1 from student ctr
			student[studentdeleted]=""; //Clear student name
		} 
		else //If our student wasn't the last student in the list
		{
			for (int i=studentdeleted; i<studentctr; i++) //Then we'll move all of the students after that student up 1 position
			{
				student[i] = student[i+1];
			}
			//After moving them all down a position, we can subtract 1 from student ctr
			studentctr-=1;
		}
		cout << "Student " << StudentName << " deleted from the list." << endl;
	}
}
Topic archived. No new replies allowed.