Hi!
I have been learning the C++ programming language thanks to this website and I am creating my own examples or exercises just to learn, so the code may be kind of lame, but I'm having a problem with the
getline() function. I designed a program where the user can insert information about students names and their grades.
Here is the source code:
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
|
#include <iostream>
#include <string>
using namespace std;
class StuDB{
string *student_name;
int **student_grade,am_students,am_grades;
public:
StuDB (int,int);
~StuDB();
void info_in()
{
for(int a=0;a<am_students;a++)
{
cout<<"Insert Student "<<a+1<<" name-> ";
getline (cin,student_name[a]);
cout<<student_name[a]<<" grades: "<<endl;
for(int b=0;b<am_grades;b++)
{
cout<<"Grade "<<b+1<<"-> ";
cin>>student_grade[a][b];
}
}
}
void info_show()
{
for(int c=0;c<am_students;c++)
{
cout<<"\n"<<student_name[c]<<" grades: ";
for(int d=0;d<am_grades;d++)
{
cout<<student_grade[c][d]<<" . ";
}
}
}
};
StuDB::StuDB(int u_s,int u_g)
{
am_students = u_s;
am_grades = u_g;
student_name = new string[u_s];
student_grade = new int*[u_s];
for(int i=0;i<u_s;i++)
{student_grade[i]=new int[u_g];}
}
StuDB::~StuDB()
{
delete[] student_name;
delete[] student_grade;
}
int main()
{
int user_students,user_grades;
cout<<"\t-Student DataBase-"<<endl;
cout<<"How many students are being registered>";
cin>>user_students;
cout<<"How many grades per student>";
cin>>user_grades;
StuDB NewLand_school (user_students,user_grades);
cout<<"\n\t***Insert Information***\n"<<endl;
NewLand_school.info_in();
cout<<"\n\t***Report***\n";
NewLand_school.info_show();
cout<<"\n";
system("pause");
return 0;
}
|
I know that
cin>> stops reading as soon it founds a space character so I used the
getline() function to retrieve both first and last name.
The problem is that when I run the program, I can't insert the student name.
The output should be something like this:
-Student Database-
How many students are being registered>2
How many grades per student>4
***Insert Information***
Insert Student 1 name-> Manuel Perez
Manuel Perez grades:
Grade 1-> 95
Grade 2-> 85
....... etc.
|
But instead, it shows:
-Student Database-
How many students are being registered>2
How many grades per student>4
***Insert Information***
Insert Student 1 name-> grades:
Grade 1->
|
I had the same problem on other similar exercises and i searched the internet for help but nothing.
Any help would be appreciated!
By the way, the last chapter I read was
Classes(I)
And sorry for writing too much lol.