Skip to content

Instantly share code, notes, and snippets.

@mahata
Created September 22, 2011 04:54
Show Gist options
  • Save mahata/1234053 to your computer and use it in GitHub Desktop.
Save mahata/1234053 to your computer and use it in GitHub Desktop.
C++ Primer 7.8
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array or upon encountering a blank line for the student
// name. The function returns the actual number of array elements
// filled.
int getinfo(student pa[], int n);
// display1() takes a student structure as an argument
// and displays its contents
void display1(student st);
// display2() takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student * ps);
// display3() takes the address of the first element of an array
// of student structures and the number of array elements as
// arguments and displays the contents of the structures
void display3(const student pa[], int n);
int main()
{
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;
}
int getinfo(student pa[], int n)
{
int i = 0;
for (; i < n; i++) {
cout << endl;
cout << "Please enter the student's name: ";
cin.getline(pa[i].fullname, SLEN);
if ('\0' == pa[i].fullname) {
cout << "Error: no information." << endl;
return i;
}
cout << "Please enter the student's hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Please enter the student's OOP (Object Oriented Programming) level: ";
cin >> pa[i].ooplevel;
if (cin.fail()) {
cout << "Error: bad information (you have to input integer here)." << endl;
// return i;
cin.clear();
while (cin.get() != '\n') { continue; }
continue;
}
while (cin.get() != '\n') { continue; }
}
return i;
}
void display1(student st)
{
cout << endl;
cout << "name @ display1: " << st.fullname << endl;
cout << "hobby @ display1: " << st.hobby << endl;
cout << "OOP level @ display1: " << st.ooplevel << endl;
}
void display2(const student * ps)
{
cout << endl;
cout << "name @ display2: " << ps->fullname << endl;
cout << "hobby @ display2: : " << ps->hobby << endl;
cout << "OOP level @ display2: " << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
cout << endl;
for (int i = 0; i < n; i ++) {
cout << "name @ display3: " << pa[i].fullname << endl;
cout << "hobby @ display3: " << pa[i].hobby << endl;
cout << "OOP level @ display3: " << pa[i].ooplevel << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment