Question: Code runs properly, but user input for # of students is ignored. The user should be able to enter any positive number of students, the

Code runs properly, but user input for # of students is ignored.

The user should be able to enter any positive number of students, the range being 1 to 50. No matter what I input, its ignored (ex. Enter 1, prompted for 50; Enter 1, prompted for 50;and so on).

I know it should be a simple fix but I don't see where I screwed up. How do I fix this?

(Note: C++, in Visual Studio 2017)

#include

#include

using namespace std;

struct Phone

{

int area_code;

int prefix;

int ph;

};

struct StudentPhoneRecord

{

string name;

Phone home, emergency, doctor;

};

//functions

void readNumStudents(StudentPhoneRecord a[]);

void readNameNumbers(StudentPhoneRecord a[]);

void displayOutput(StudentPhoneRecord a[]);

int main()

{

StudentPhoneRecord a[50];

readNumStudents(a);

readNameNumbers(a);

displayOutput(a);

cout << endl << endl;

system("pause");

return 0;

}

void readNumStudents(StudentPhoneRecord a[])

{

int i, num;

//get number of students

cout << "Enter number of students(must be a positive number less than or equal to 50) = ? ";

cin >> num;

}

void readNameNumbers(StudentPhoneRecord a[])

{

string dummy;

for (int i = 0; i<50; i++)

{

cout << "Please enter the information for student " << i + 1 << endl;

cout << "" << endl;

cout << "Name: ";

cin.ignore();

getline(cin, a[i].name);

cout << "" << endl;

cout << " Home Phone :" << endl;

cout << " Area Code : ";

cin >> a[i].home.area_code;

cout << " Prefix: ";

cin >> a[i].home.prefix;

cout << " Suffix: ";

cin >> a[i].home.ph;

cout << " Emergency Phone :" << endl;

cout << " Area Code : ";

cin >> a[i].emergency.area_code;

cout << " Prefix: ";

cin >> a[i].emergency.prefix;

cout << " Suffix: ";

cin >> a[i].emergency.ph;

cout << " Doctor Phone :" << endl;

cout << " Area Code : ";

cin >> a[i].doctor.area_code;

cout << " Prefix: ";

cin >> a[i].doctor.prefix;

cout << " Suffix: ";

cin >> a[i].doctor.ph;

cout << "" << endl;

}

}

void displayOutput(StudentPhoneRecord a[])

{

for (int i = 0; i < 50; i++)

{

cout << "Students Phone report is:" << endl;

cout << a[i].name << endl;

cout << "Home:\t\t" << a[i].home.area_code << "-" << a[i].home.prefix << "-" << a[i].home.ph << endl;

cout << "Emergency:\t" << a[i].emergency.area_code << "-" << a[i].emergency.prefix << "-" << a[i].emergency.ph << endl;

cout << "Doctor:\t\t" << a[i].doctor.area_code << "-" << a[i].doctor.prefix << "-" << a[i].doctor.ph << endl;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!