-
Drving me loopy!!!
Hi I am developing a program that prints a table of student made up deatials.
The program below works ok, but when I go to enter a new detail, it lets me do it, but then I go to end the program to display the results and all I get is the last entry I did. I want the program to print all the details the user inputs, not just the most recent one.
I think the problem is my while loop in main, can anyone see where I am going worng?
Code:
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <string>
using std::string;
using std::getline;
// create directory class
class Directory
{
public:
// constructor - sets course name
Directory ( string name )
{
setListName ( name );
}
// display name of list
void setListName ( string name )
{
listName = name;
}
string getListName()
{
return listName;
}
void displayMessage()
{
cout << "\tSchool list for: " << getListName();
}
// student name
string setStudentName ( string stuName )
{
cin.ignore(); // prevents this function being skipped
cout << "\n\nEnter student name: ";
getline ( cin, stuName );
studentName = stuName;
return stuName;
}
string getStudentName()
{
return studentName;
}
// student date of birth
int setDob ( int bd )
{
cout << "\nEnter year of birth: ";
cin >> bd;
dob = bd;
return bd;
}
int getDob()
{
return dob;
}
// student gender
char setGender ( char gen )
{
cout << "\nEnter gender, m / f: ";
cin >> gen;
gender = gen;
return gen;
}
char getGender()
{
return gender;
}
private:
string listName; // college name
string studentName; // student name
int dob; // student year of birth
char gender; // student gender
};
// main function - driver //////////////////////////////////////////////////////
//
int main ( void )
{
Directory dir ("MILTON COLLEGE");
dir.displayMessage();
int answer;
cout << "\n\nWill you use the list: -1 to quit: ";
cin >> answer;
// quit program if no data needed
if ( answer == -1 )
{
return 0;
}
// loop to input data
while ( answer != -1 )
{
dir.setStudentName("");
dir.setDob(0);
dir.setGender('a');
cout << "\nWill you add another name: -1 to show result: ";
cin >> answer;
}
// display headings
cout << "\nSTUDENT NAME" << "\t\t" << "D O B" << "\t\t" << "GENDER" << "\n\n";
// display result of class
cout << dir.getStudentName() << "\t\t" << dir.getDob() << "\t\t"
<< dir.getGender() << endl;
cin.get(); // freeze console window
cin.ignore();
return 0; // indicate program ended sucsessfuly
}
-
You only have one dir. You need an instance of dir for each student.