Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of objects

Status
Not open for further replies.

danyes

Programmer
Sep 21, 2002
1
US
I was wondering if anyone could help me out with something I am trying to get a program to work where I have an array of employee objects, and through the program you can go make a selection to add an employee and then it asks you for certain data to put into the object. My problem is is that it seems to be working fine but when I try to display the objects data the data is in the wrong place. like instead of name next to name it is next to address, it skips the first data member name any help would be great thanks

//************************************************
/
//
//employeeMenu.cpp
//class implementation file for the employeeMenu class
//************************************************

#include <stdio.h>
#include <iostream.h>
#include <ios.h>
#include <iomanip.h>
#include <fstream.h>
#include <conio.h>
#include &quot;employeeMenu.h&quot;
#include &quot;employee.h&quot;
#include &quot;student.h&quot;

const int MAX = 5;
int E;
employee Emp[MAX];

employeeMenu::employeeMenu()
{
choiceText[0] = &quot; Display student information&quot;;
choiceText[1] = &quot; Add Employee&quot;;
choiceText[2] = &quot; Display Employee&quot;;
choiceText[3] = &quot; Quit&quot;;

numChoices = 4;
}



void employeeMenu::run()
{
bool dontQuit = true;
while (dontQuit)
{
displayMenu();
cin >> choice;
switch (choice)
{
case 1:
pStudentInfo();
break;

case 2:
cin.ignore (80, '\n');
addEmployeeInfo();
break;

case 3:
displayEmployeeInfo();
break;

case 4:
dontQuit = false;
break;

default:
cout << &quot;Invalid selection please try again\n&quot;;
}
}
}

void employeeMenu::displayMenu()
{
clrscr();
cout << &quot;-----------------------------------------------------------\n&quot;;
cout << &quot; Please choose from the following selections \n&quot;;
cout << &quot;-----------------------------------------------------------\n&quot;;
for ( int i = 0; i < numChoices; i++)
{
cout << &quot; &quot; << (i+1) << &quot;: &quot; << choiceText[ i ] << &quot;\n&quot;;
}
cout << &quot;<&quot;;
}

void employeeMenu::pStudentInfo()
{
clrscr();
Student student(4);
cout << &quot;-----------------------------------------------------------\n&quot;;
cout << &quot; Student Info \n&quot;;
cout << &quot;-----------------------------------------------------------\n&quot;;
student.showInfo();
cout << &quot;\n\nPress any key to continue...&quot;;
getch();
clrscr();
}

void employeeMenu::addEmployeeInfo()
{
int E;
string name1;
string address1;
string phoneNum1;
char n [80];
char a [80];
char p [80];
/*string n;
string a;
string p;*/

cout << &quot;Which employee would you like to add (1-&quot; << MAX << &quot;): &quot;;
cin >> E;
clrscr();

cout << &quot;Please input employees name (Last, First): &quot;;
cin.getline (n, 80, '\n');
name1 = n;
cout << &quot;\nPlease input employees address (address, city, state, zip): &quot;;
cin.getline (a, 80, '\n');
address1 = a;
cout << &quot;\nPlease input employee phone number (xxx-xxx-xxxx): &quot;;
cin.getline (p, 80, '\n');
phoneNum1 = p;
cin.ignore (80, '\n');
Emp[E-1].setName(name1);
Emp[E-1].setAddress(address1);
Emp[E-1].setPhoneNum(phoneNum1);

}

void employeeMenu::displayEmployeeInfo()
{
int E;
cout << &quot;Which employee would you like to see info for (1-&quot; << MAX << &quot;): &quot;;
cin >> E;
Emp[E-1].showInfo();
}



 
First thing I notice (have't read it completely yet..) is that after your cin>>choice,you should put a cin.get(); to take away your &quot;enter&quot; key from the buffer. You might wanna start with that,that could already remove some unexpected behaviour of you program. (because when you don't remove the enter, the next time a cin happens, that cin will automatically get the enter sign and the program will continue..) Greetz,
muppeteer.gif

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top