The problem is that you never increment the value of
i
.
i
will always be the same in your
do ... while
loop, so the loop will be infinite. At the end of the code in your
do ... while
loop, add
i++;
.
Also, the size of your array with student names is 3. And if the given number of students is more than 3, your program won't be able to store more than 3 students in the array. So you'll need to check whether the given number of students is greater than 3. And instead of hard-coding that number, you can write a
#define
statement for it to make it a constant (you can do the same for the length). Why? Because if you use the same number at multiple places, you just have to change the value of your constant, instead of all occurrences of the number.
And as Wes Aday said, you'll need to use
cin.ignore()
to ignore the terminating newline character, which is still on the stream.
So, you can change your code into this:
Add this line of code before your
Menu_Account
class:
#define MAX_STUDENTS 3
#define MAX_LENGTH 20
Then, in your
Menu_Account
class, replace
char name[3][20];
with:
char name[MAX_STUDENTS][MAX_LENGTH];
And then, update your
get_name
method:
void Menu_Account::get_name(int student)
{
if (student > MAX_STUDENTS)
{
cout<<"Number too high, there are max. "<<MAX_STUDENTS<<" accepted."<<endl;
return;
}
cout<<"\n\t\t\tEnter Student Name."<<endl;
i=0;
do
{ cout<<"Enter Student "<<i+1<<" Name:";
cin.getline(name[i],20);
cin.ignore();
i++;
}while(i<student);
cout<<"Enter Name is."<<endl;
for ( i = 0; i < student; i++)
{
cout<<name[i]<<endl;
}
}