Write a no (zero) argument constructor which initializes the member variables

May 22, 2016 at 2:28am
HI guys, I Wrote a no (zero) argument constructor which initializes the following member variables from structure student to.
• name to an empty string
• units to zero
• residency status to resident
• tuittion fee to a flat fee

The instructions indicate the main function will prompt the user to enter the "maximum" number of students.The input function will accept that maximum number or less. The user will indicate that there are no more students by pressing [Enter] in response to the prompt, "Enter the name of student ___" The problem right now is that the program hangs if the user does not enter a value. I'm a beginner in programming and really confused with this problem. Any help with be appreciated.

#include <iostream>
#include <cstring>
using namespace std;

enum res { resident, nonresident };

struct tuitioncost {
float instate;
int outstate;

};

struct student
{
char name[50];
int units;
char answer;
res status;
tuitioncost cost;

student() //constructor
{
strcpy(name, "\0");
int units = 0;
res status = resident;
cost.instate = 0;

}
};



void input(struct student e[], int);
void output(student e[], int num);
int main()
{


int max;
cout << "Enter max number of students ";
cin >> max;
student *emp;
emp = new student[max];
input(emp, max);
output(emp, max);

delete[] emp;


return 0;
}


void input(student e[], int num)
{

for (int i = 0; i < num; i++)
{
cout << "Enter the name of student #: " << i + 1 << endl;
cin.ignore();
cin.getline(e[i].name, 80);
cout << "Enter the enrolled units: ";
cin >> e[i].units;
cout << " Enter Resident STATUS (R/r or N/n) : ";
cin >> e[i].answer;

if (e[i].answer == 'R' || e[i].answer == 'r')
{
e[i].status = resident;
cout << "Enter the flat tuition fee: : ";
cin >> e[i].cost.instate;
}
else if (e[i].answer == 'N' || e[i].answer == 'n')
{
e[i].status = nonresident;
cout << "Enter the fee per unit : ";
cin >> e[i].cost.outstate;
}



}





}

void output(student e[], int num)
{
cout << "\nHere is the students fees" << endl;
for (int i = 0; i < num; i++)
{

cout << "Name: " << e[i].name << endl;
cout << "units: " << e[i].units << endl;
if (e[i].status == resident)
{
cout << "Resident" << endl;
cout << "tuition: $" << e[i].cost.instate << endl;

}
else if (e[i].status == nonresident)
{
cout << "nonresident" << endl;
cout << "tuition: $" << e[i].cost.outstate * e[i].units << endl;

}


}




}

Last edited on May 22, 2016 at 2:39am
May 22, 2016 at 1:26pm
I'm seeing a lot of low-level C code going on here. Do yourself a favour; use C++. All the low-level fiddling around here is obscuring the logic and making it harder for you to write good code that solves your problems.

Don't use C-strings. Use proper C++ strings. No strcpy. No char arrays. In fact, no arrays at all. Arrays are for advanced users. As a beginner, use a vector.

Don't be handling memory yourself with new and delete. Let C++ handle it for you.

Your student structure seems odd. answer has no place being in the student structure. It should be more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct student
{
  string name;
  int units;
  res status; 
  tuitioncost cost; 

  student() //constructor
  {
    units = 0;
    status = resident();
    cost.instate = 0;
   }
};



As for hanging if you enter a blank student name, are you sure? I see no reason why it would.
May 28, 2016 at 4:09pm
Moschops,

thanks for making things more clear.
Topic archived. No new replies allowed.