nazdar. toto je priklad z knižky Mistrovstvi v C++ strana 275. je daný main a prototypy troch rôznych display funkcií, ktoré treba dokódiť. všetko mi beží až na detail vo funkcii getinfo ktorá má za úlohu načítavať dáta do štruktúr. (to ide). je tam taká úloha, že ked nevyplním meno tak má prerušiť načítavanie. skusil som to cez strcmp (tie zakomentovane 2 riadky) ale nefunguje to, program zamrzne. neviete ako na to?
Kód:
#include <cstdlib>
#include <iostream>
#include <cctype>
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); //parameter je struktura student
void display2(const student *ps); //parameter je adresa strukturi student
void display3(const student pa[], int n); //adresa prveho prvku strukturi, pocet prvkov pola
int main(int argc, char** argv)
{
cout << "zadaj velkost triedy: ";
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 << "\nHOTOVO\n";
return 0;
}
int getinfo(student pa[], int n)
{
int i=0, pocet = n;
while(i<pocet)
{
cout << "zadaj meno: ";
cin.getline(pa[i].fullname,SLEN);
cout << "zadaj hobby: ";
cin.getline(pa[i].hobby,SLEN);
cout << "zadaj level oop: ";
cin >> pa[i].ooplevel;
//if(strcmp(pa[i].fullname,'\0')==0)
// break;
i++;
cin.get();
cin.clear();
cout << "-----------------------------\n";
}
return i;
}
void display1(student st)
{
cout << "\nVYPIS STRUKTURI DISPLAY1\n";
cout << st.fullname << "\n";
cout << st.hobby << "\n";
cout << st.ooplevel << "\n";
cout << "-----------------------------";
}
void display2(const student *ps)
{
cout << "\nVYPIS STRUKTURI DISPLAY2\n";
cout << ps->fullname << "\n";
cout << ps->hobby << "\n";
cout << ps->ooplevel << "\n";
cout << "-----------------------------";
}
void display3(const student pa[], int n)
{
int i=0, pocet = n;
while(i<pocet)
{
cout << "\nVYPIS STRUKTURI DISPLAY3\n";
cout << pa[i].fullname << "\n";
cout << pa[i].hobby << "\n";
cout << pa[i].ooplevel << "\n";
cout << "-----------------------------";
i++;
}
}