Simple print out problem?

Hey all,

I've been working on this program for a while. I'm pretty new to C++ and to say the least it isn't my favorite.

Basically it is just a program that enters a college student/staff/faculty person's info and then returns all the info when prompted depending on what number you enter. In this instance it not meant to accept more than 8 people.

The program has a header file that includes a class and strings blah blah. For some reason if I enter a person's info and then enter "4" the information I entered will not display!!! (feelings of anger!). Can anyone assist me?


#ifndef Headerfile_h
#define Headerfile_h
#include<iostream>
using namespace std;

class witperson
{
protected:
string name;
string address;
string number;
string role;

public:
string getName(void) {return name;}
string getAddress(void) {return address;}
string getNumber(void) {return number;}
string getRole(void) {return role;}

void setName (string Name);
void setAddress (string Address);
void setNumber (string Number);
void setRole (string Role);


void print_info()
{
cout << "Name: " <<getName()<<endl;
cout << "Address: " <<getAddress()<<endl;
cout << "W-Number: " <<getNumber()<<endl;
cout << "Role: " <<getRole()<<endl;
}
};
#endif

..........

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#include "Headerfile.h"


int main(void)
{
witperson person[8];

for (int index = 0; index < 8; index++)
{
int s;
cout << "Enter 1 to add a student, 2 to add a faculty member, 3 to enter a staff member, 4 to display the present list, or 0 to quit: ";
cin >> s;
cout << endl;

if (s == 1){

cout << "Enter name: ";
cin>> person[index].getName();

cout << "Enter address: ";
cin>> person[index].getAddress();

cout << "Enter W-number: ";
cin>> person[index].getNumber();

person[index].getRole() = "Student";
}

else if (s == 2) {

cout << "Enter name: ";

cin>> person[index].getName();

cout << "Enter address: ";
cin>> person[index].getAddress();

cout << "Enter W-number: ";
cin>> person[index].getNumber();

person[index].getRole() = "Faculty";
}

else if (s == 3) {

cout << "Enter Name: ";
cin.ignore();
cin>> person[index].getName();

cout << "Enter Address: ";
cin>> person[index].getAddress();

cout << "Enter W-number: ";
cin>>person[index].getNumber();

person[index].getRole() = " Staff";
}

else if (s == 4)
{
for (int i=0; i < index; i++) {
cout << "Entry #" << i + 1 << ":" << endl;
person[index].print_info();
cout << endl;
}

}

else if (s == 0) {return 0;}
else {cout << "Not Valid" << endl;}
}


return 0;
}
I think the for loop needs to be
for(int i =0; i < 8; i++)
Wait nevermind... the problem is you put
person[index]. print_info();
it should be
person[i].print_info();
Thanks, but no dice. when you enter the 4th option it will print:

name: (blank)
address: (blank)
role: (blank)

still none of the information appears where it should be (the blank space). Changing the for loop to 8 will just print this out 8 times.

***edit. I've tried- person[i].print_info(); -too. still nothing...
Last edited on
Oh... Your problem is when enter data you use get functions instead of set functions... so when you enter data it never actually does anything
Also on your set functions I think your paramaters need to be & paramaters
Fixed it :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class witperson
{
public:
	string name;
	string address;
	string number;
	string role;

	string getName(void) { return name; }
	string getAddress(void) { return address; }
	string getNumber(void) { return number; }
	string getRole(void) { return role; }

	void print_info()
	{
		cout << "Name: " << getName() << endl;
		cout << "Address: " << getAddress() << endl;
		cout << "W-Number: " << getNumber() << endl;
		cout << "Role: " << getRole() << endl;
	}
};


int main(void)
{
	witperson person[8];

	for (int index = 0; index < 8; index++)
	{
		int s;
		cout << "Enter 1 to add a student, 2 to add a faculty member, 3 to enter a staff member, 4 to display the present list, or 0 to quit: ";
		cin >> s;
		cout << endl;

		if (s == 1){

			cout << "Enter name: ";
			cin >> person[index].name;

			cout << "Enter address: ";
			cin >> person[index].address;

			cout << "Enter W-number: ";
			cin >> person[index].number;

			person[index].role = "Student";
		}

		else if (s == 2) {

			cout << "Enter name: ";

			cin >> person[index].name;

			cout << "Enter address: ";
			cin >> person[index].address;

			cout << "Enter W-number: ";
			cin >> person[index].number;

			person[index].role = "Faculty";
		}

		else if (s == 3) {

			cout << "Enter Name: ";
			cin.ignore();
			cin >> person[index].name;

			cout << "Enter Address: ";
			cin >> person[index].address;

			cout << "Enter W-number: ";
			cin >> person[index].number;

			person[index].role = " Staff";
		}

		else if (s == 4)
		{
			for (int i = 0; i < index; i++)
			{
				cout << "Entry #" << i + 1 << ":" << endl;
				person[i].print_info();
				cout << endl;
			}

		}

		else if (s == 0) { return 0; }
		else { cout << "Not Valid" << endl; }
	}


	return 0;
}
Also you may want to add index -- to the end of the else if (s == 4)
That works great! But the only problem is that part of problem is to make certain data private and public . So the strings are meant to be private data.
Topic archived. No new replies allowed.