edit: I figured out
Code:
int getinfo(student pa[], int n)
{
int i;
for(i = 0; i < n; i++)
{
cout << "Enter student's full name: ";
cin.get(pa[i].fullname, SLEN).get();
if(strlen(pa[i].fullname) == 0)
break;
cout << "Enter student's hobby: ";
cin.get(pa[i].hobby, SLEN).get();
cout << "Enter student's OOP level: ";
(cin >> pa[i].ooplevel).get();
}
return i;
}
I need to break the for inside getinfo() if the user inputs a blank line for his full name. But I don't know how to do it. I tried with cin.get() and cin.getline() without success.
Code:
#include <iostream>
#include <cstring>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student *pa, int n);
void display1(student st);
void display2(const student *ps);
void display3(const student *pa, int n);
int getinfo(student pa[], int n)
{
int i;
for(i = 0; i < n; i++)
{
cout << "Enter student's full name: ";
cin >> pa[i].fullname;
cout << "Enter student's hobby: ";
cin >> pa[i].hobby;
cout << "Enter student's OOP level: ";
cin >> pa[i].ooplevel;
}
return i;
}
void display1(student st)
{
cout << "Student full name: " << st.fullname << endl;
cout << "hobby: " << st.hobby << endl;
cout << "OOP level: " << st.ooplevel << endl;
}
void display2(const student *ps)
{
cout << "Student full name: " << ps->fullname << endl;
cout << "hobby: " << ps->hobby << endl;
cout << "OOP level: " << ps->ooplevel << endl;
}
void display3(const student *pa, int n)
{
for(int i = 0; i < n; i++)
{
cout << "Student full name: " << pa[i].fullname << endl;
cout << "hobby: " << pa[i].hobby << endl;
cout << "OOP level: " << pa[i].ooplevel << endl;
}
}
int main(void)
{
cout << "Enter class size: ";
int class_size;
cin >> class_size;
while(cin.get() != '\n')
continue;
student *ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for(int i = 0; i < entered; i++)
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete [] ptr_stu;
cout << "Done\n";
return 0;
}