![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#31 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 329
Rep Power: 4
![]() |
Quote:
//cs351//Carl Layton
//cs351
#include <iostream>
#include <string.h>
using namespace std;
//routines for adding an employee, displaying an employee, displaying id number
//displaying name
class employee
{
private:
char name[15];
char emp_id[4];
int loc;
public:
//constructor
employee()
{
loc = 0;
}
//add an employee
void addEmployee(employee emp[])
{
cout << "Please enter the employee name: ";
cin >> emp[loc].name;
cout << "Please enter the employee id: ";
cin >> emp[loc].emp_id;
cout << endl;
loc++;
}
//display all employees
void displayEmployees(employee emp[])
{
for(int i =0; i < loc; i++)
{
cout << emp[i].name << " "
<< emp[i].emp_id
<< endl;
}
}
// find employee
bool findEmp(employee emp[], char empId[4])
{
bool match = false;
for(int i = 0; i < loc; i++)
{
if(strcmp(emp[i].emp_id, empId) == 0)
{
match = true;
break;
}
}
return match;
}
//return name from employee id
string getName(employee emp[], char empId[4])
{
char name[15];
for(int i = 0; i < loc; i++)
{
if(strcmp(emp[i].emp_id, empId) == 0)
{
return emp[i].name;
}
}
return "";
}
}; //end of employee class
//routines for adding displaying cars, get for make_model and carNum
class car
{
private:
char make_model[15];
char carNum[4];
char cType[15];
int hp;
int loc;
public:
//constructor
car()
{
loc = 0;
}
//add a car
void addCar(car cars[])
{
cout << "Enter the make_model number: ";
cin >> cars[loc].make_model;
cout << "Enter car number: ";
cin >> cars[loc].carNum;
cout << "Enter C type: ";
cin >> cars[loc].cType;
cout << "Enter HP number: ";
cin >> cars[loc].hp;
cout << endl;
loc++;
}
//display cars
void displayCar(car cars[])
{
for(int i =0; i < loc; i++)
{
cout << cars[i].make_model << " "
<< cars[i].carNum << " "
<< cars[i].cType << " "
<< cars[i].hp
<< endl;
}
}
// find car
bool findCar(car cars[], char carnum[4])
{
bool match = false;
for(int i = 0; i < loc; i++)
{
if(strcmp(cars[i].carNum, carnum) == 0)
{
match = true;
break;
}
}
return match;
}
// return true if car has been purchased false otherwise
bool alreadyPurchased(car cars[], char carnum[4])
{
bool purch = false;
for(int i = 0; i < loc; i++)
{
if(strcmp(cars[i].carNum, carnum) == 0)
{
purch = true;
break;
}
}
return purch;
}
//get make_model from car number
string getMake_model(car cars[], char carnum[4])
{
for(int i = 0; i < loc; i++)
{
if(strcmp(cars[i].carNum, carnum) == 0)
{
return cars[i].make_model;
}
}
return "";
}
}; //end of car class
//add an offer, display offers, get oDate, get buyer, get price
class offer
{
private:
int offerNum;
char oDate[8];
char buyer[14];
int price;
int loc;
char car1[4]; // car number
char emp1[4]; // employee number
public:
//constructor
offer()
{
loc = 0;
}
//add offer
void addOffer(offer off[], employee emp[], car cars[])
{
cout << "Enter offer number: ";
cin >> off[loc].offerNum;
cout << "Enter offer date: ";
cin >> off[loc].oDate;
cout << "Enter buyer: ";
cin >> off[loc].buyer;
cout << "Enter price: ";
cin >> off[loc].price;
cout << "Enter existing car number: ";
cin >> off[loc].car1;
while(!cars->findCar(cars, off[loc].car1))
{
cout << "Not an existing car number! \n"
<< "Please enter an existing car number: ";
cin >> off[loc].car1;
}
cout << "Enter existing employee id: ";
cin >> off[loc].emp1;
while(!emp->findEmp(emp, off[loc].emp1))
{
cout << "Not an existing employee id! \n"
<< "Please Enter an existing employee id: ";
cin >> off[loc].emp1;
}
loc++;
}
//display offers
void displayOffer(offer off[], employee emp[], car cars[])
{
for(int i = 0; i < loc; i++)
{
cout << off[i].offerNum << " "
<< off[i].oDate << " "
<< off[i].buyer << " "
<< off[i].price << " "
<< off[i].car1 << " "
<< cars->getMake_model(cars, off[i].car1) << " "
<< emp->getName(emp, off[i].emp1) << " "
<< endl;
}
}
//get oDate from offer number
string getOdate(offer off[], int offnum)
{
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
return off[i].oDate;
}
}
return "";
}
//get buyer from offer number
string getBuyer(offer off[], int offnum)
{
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
return off[i].buyer;
}
}
return "";
}
//get price from offer number
int getPrice(offer off[], int offnum)
{
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
return off[i].price;
}
}
return 0;
}
// find offer number
bool findOffer(offer off[], int offnum)
{
bool match = false;
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
match = true;
break;
}
}
return match;
}
}; // end offer class
//add a purchase, display a purchase, get pDate set pDate
class purchase
{
private:
char id[4]; // employee id number
char car1[4]; // car number
int off1; // offer number
char pDate[8];
int loc;
public:
purchase()
{
loc = 0;
}
// add a purchase
void addPurchase(purchase purch[], employee emp[], car cars[], offer off[])
{
int i = 0;
cout << "Enter pDate: ";
cin >> purch[loc].pDate;
cout << "Enter an existing car number: ";
cin >> purch[loc].car1;
while(!cars->findCar(cars, purch[loc].car1))
{
cout << "Not an existing car number! \n"
<< "Please enter an existing car number: ";
cin >> purch[loc].car1;
}
while(cars->alreadyPurchased(cars, purch[loc].car1))
{
cout << "Already been purchased \n"
<< "Please enter a different existing car number: ";
cin >> purch[loc].car1;
}
// while(i < loc)
//{
// if(strcmp(purch[loc].car1, purch[i].car1) == 0)
// {
// cout << "Already been purchased \n"
// << "Please enter a different existing car number: ";
// cin >> purch[loc].car1;
// }
// while(!cars->findCar(cars, purch[loc].car1))
// {
// cout << "Not an existing car number! \n"
// << "Please enter an existing car number: ";
// cin >> purch[loc].car1;
// }
//i++;
// }
cout << "Enter an existing employee id: ";
cin >> purch[loc].id;
while(!emp->findEmp(emp, purch[loc].id))
{
cout << "Not an existing employee id! \n"
<<"Please enter an existing employee id: ";
cin >> purch[loc].id;
}
cout << "Enter an existing offer number: ";
cin >> purch[loc].off1;
while(!off->findOffer(off, purch[loc].off1))
{
cout << "Not an existing offer number! \n"
<< "Please enter an existing offer number: ";
cin >> purch[loc].off1;
}
loc++;
}
// display purchases
void displayPurchases(purchase purch[], employee emp[], car cars[], offer off[])
{
for(int i = 0; i < loc; i++)
{
cout << purch[i].pDate << " "
<< purch[i].car1 << " "
<< cars->getMake_model(cars, purch[i].car1) << " "
<< emp->getName(emp, purch[i].id) << " "
<< purch[i].off1 << " "
<< off->getOdate(off, purch[i].off1) << " "
<< off->getBuyer(off, purch[i].off1) << " "
<< off->getPrice(off, purch[i].off1) << " "
<< endl;
}
}
}; // end of purchase class
int main()
{
employee *emp;
emp = new employee[100];
car *cars;
cars = new car[100];
offer *off;
off = new offer[100];
purchase *purch;
purch = new purchase[100];
emp->addEmployee(emp);
emp->displayEmployees(emp);
cars->addCar(cars);
cars->addCar(cars);
cars->displayCar(cars);
cars->addCar(cars);
cars->addCar(cars);
cars->addCar(cars);
cars->displayCar(cars);
off->addOffer(off, emp, cars);
off->displayOffer(off, emp, cars);
return 0;
}Last edited by cwl157; Mar 6th, 2006 at 11:45 AM. |
|
|
|
|
|
|
#32 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 329
Rep Power: 4
![]() |
alright now the program is complete with menu in main and everything. The only problem is still that one method for checking if a car has been purchased or not yet. I have an attempt but it does not work right. Here is the code with the updated main and everything.
#include <iostream>
#include <string.h>
using namespace std;
//routines for adding an employee, displaying an employee, displaying id number
//displaying name
class employee
{
private:
char name[15];
char emp_id[4];
int loc;
public:
//constructor
employee()
{
loc = 0;
}
//add an employee
void addEmployee(employee emp[])
{
cout << "Please enter the employee name: ";
cin >> emp[loc].name;
cout << "Please enter the employee id: ";
cin >> emp[loc].emp_id;
cout << endl;
loc++;
}
//display all employees
void displayEmployees(employee emp[])
{
for(int i =0; i < loc; i++)
{
cout << emp[i].name << " "
<< emp[i].emp_id
<< endl;
}
}
// find employee
bool findEmp(employee emp[], char empId[4])
{
bool match = false;
for(int i = 0; i < loc; i++)
{
if(strcmp(emp[i].emp_id, empId) == 0)
{
match = true;
break;
}
}
return match;
}
//return name from employee id
string getName(employee emp[], char empId[4])
{
char name[15];
for(int i = 0; i < loc; i++)
{
if(strcmp(emp[i].emp_id, empId) == 0)
{
return emp[i].name;
}
}
return "";
}
}; //end of employee class
//routines for adding displaying cars, get for make_model and carNum
class car
{
private:
char make_model[15];
char carNum[4];
char cType[15];
int hp;
int loc;
public:
//constructor
car()
{
loc = 0;
}
//add a car
void addCar(car cars[])
{
cout << "Enter the make_model number: ";
cin >> cars[loc].make_model;
cout << "Enter car number: ";
cin >> cars[loc].carNum;
cout << "Enter C type: ";
cin >> cars[loc].cType;
cout << "Enter HP number: ";
cin >> cars[loc].hp;
cout << endl;
loc++;
}
//display cars
void displayCar(car cars[])
{
for(int i =0; i < loc; i++)
{
cout << cars[i].make_model << " "
<< cars[i].carNum << " "
<< cars[i].cType << " "
<< cars[i].hp
<< endl;
}
}
// find car
bool findCar(car cars[], char carnum[4])
{
bool match = false;
for(int i = 0; i < loc; i++)
{
if(strcmp(cars[i].carNum, carnum) == 0)
{
match = true;
break;
}
}
return match;
}
// return true if car has been purchased false otherwise
bool alreadyPurchased(car cars[], char carnum[4])
{
bool purch = true;
for(int i = 0; i < loc; i++)
{
if(strcmp(cars[i].carNum, carnum) != 0)
{
purch = false;
break;
}
}
return purch;
}
//get make_model from car number
string getMake_model(car cars[], char carnum[4])
{
for(int i = 0; i < loc; i++)
{
if(strcmp(cars[i].carNum, carnum) == 0)
{
return cars[i].make_model;
}
}
return "";
}
}; //end of car class
//add an offer, display offers, get oDate, get buyer, get price
class offer
{
private:
int offerNum;
char oDate[8];
char buyer[14];
int price;
int loc;
char car1[4]; // car number
char emp1[4]; // employee number
public:
//constructor
offer()
{
loc = 0;
}
//add offer
void addOffer(offer off[], employee emp[], car cars[])
{
cout << "Enter offer number: ";
cin >> off[loc].offerNum;
cout << "Enter offer date: ";
cin >> off[loc].oDate;
cout << "Enter buyer: ";
cin >> off[loc].buyer;
cout << "Enter price: ";
cin >> off[loc].price;
cout << "Enter existing car number: ";
cin >> off[loc].car1;
while(!cars->findCar(cars, off[loc].car1))
{
cout << "Not an existing car number! \n"
<< "Please enter an existing car number: ";
cin >> off[loc].car1;
}
cout << "Enter existing employee id: ";
cin >> off[loc].emp1;
while(!emp->findEmp(emp, off[loc].emp1))
{
cout << "Not an existing employee id! \n"
<< "Please Enter an existing employee id: ";
cin >> off[loc].emp1;
}
loc++;
}
//display offers
void displayOffer(offer off[], employee emp[], car cars[])
{
for(int i = 0; i < loc; i++)
{
cout << off[i].offerNum << " "
<< off[i].oDate << " "
<< off[i].buyer << " "
<< off[i].price << " "
<< off[i].car1 << " "
<< cars->getMake_model(cars, off[i].car1) << " "
<< emp->getName(emp, off[i].emp1) << " "
<< endl;
}
}
//get oDate from offer number
string getOdate(offer off[], int offnum)
{
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
return off[i].oDate;
}
}
return "";
}
//get buyer from offer number
string getBuyer(offer off[], int offnum)
{
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
return off[i].buyer;
}
}
return "";
}
//get price from offer number
int getPrice(offer off[], int offnum)
{
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
return off[i].price;
}
}
return 0;
}
// find offer number
bool findOffer(offer off[], int offnum)
{
bool match = false;
for(int i = 0; i < loc; i++)
{
if(off[i].offerNum == offnum)
{
match = true;
break;
}
}
return match;
}
}; // end offer class
//add a purchase, display a purchase, get pDate set pDate
class purchase
{
private:
char id[4]; // employee id number
char car1[4]; // car number
int off1; // offer number
char pDate[8];
int loc;
public:
purchase()
{
loc = 0;
}
// add a purchase
void addPurchase(purchase purch[], employee emp[], car cars[], offer off[])
{
int i = 0;
cout << "Enter pDate: ";
cin >> purch[loc].pDate;
cout << "Enter an existing car number: ";
cin >> purch[loc].car1;
while(!cars->findCar(cars, purch[loc].car1))
{
cout << "Not an existing car number! \n"
<< "Please enter an existing car number: ";
cin >> purch[loc].car1;
}
while(cars->alreadyPurchased(cars, purch[loc].car1))
{
cout << "Already been purchased \n"
<< "Please enter a different existing car number: ";
cin >> purch[loc].car1;
}
// while(i < loc)
//{
// if(strcmp(purch[loc].car1, purch[i].car1) == 0)
// {
// cout << "Already been purchased \n"
// << "Please enter a different existing car number: ";
// cin >> purch[loc].car1;
// }
// while(!cars->findCar(cars, purch[loc].car1))
// {
// cout << "Not an existing car number! \n"
// << "Please enter an existing car number: ";
// cin >> purch[loc].car1;
// }
//i++;
// }
cout << "Enter an existing employee id: ";
cin >> purch[loc].id;
while(!emp->findEmp(emp, purch[loc].id))
{
cout << "Not an existing employee id! \n"
<<"Please enter an existing employee id: ";
cin >> purch[loc].id;
}
cout << "Enter an existing offer number: ";
cin >> purch[loc].off1;
while(!off->findOffer(off, purch[loc].off1))
{
cout << "Not an existing offer number! \n"
<< "Please enter an existing offer number: ";
cin >> purch[loc].off1;
}
loc++;
}
// display purchases
void displayPurchases(purchase purch[], employee emp[], car cars[], offer off[])
{
for(int i = 0; i < loc; i++)
{
cout << purch[i].pDate << " "
<< purch[i].car1 << " "
<< cars->getMake_model(cars, purch[i].car1) << " "
<< emp->getName(emp, purch[i].id) << " "
<< purch[i].off1 << " "
<< off->getOdate(off, purch[i].off1) << " "
<< off->getBuyer(off, purch[i].off1) << " "
<< off->getPrice(off, purch[i].off1) << " "
<< endl;
}
}
}; // end of purchase class
int main()
{
employee *emp;
emp = new employee[100];
car *cars;
cars = new car[100];
offer *off;
off = new offer[100];
purchase *purch;
purch = new purchase[100];
int option;
while(option != 9)
{
cout << endl;
cout << "1. Add an employee \n"
<< "2. Display employees \n"
<< "3. Add a car \n"
<< "4. Display cars \n"
<< "5. Add an offer \n"
<< "6. Display offers \n"
<< "7. Add a purchase \n"
<< "8. Display purchases \n"
<< "9. Quit \n"
<< "Please make a selection: ";
cin >> option;
cout << endl;
if(option == 1)
emp->addEmployee(emp);
if(option == 2)
emp->displayEmployees(emp);
if(option == 3)
cars->addCar(cars);
if(option == 4)
cars->displayCar(cars);
if(option == 5)
off->addOffer(off, emp, cars);
if(option == 6)
off->displayOffer(off, emp, cars);
if(option == 7)
purch->addPurchase(purch, emp, cars, off);
if(option == 8)
purch->displayPurchases(purch, emp, cars, off);
}
return 0;
} |
|
|
|
|
|
#33 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
I would think that the "alreadyPurchased" method belongs in the your purchase class, seing as you would need to check through the purchased array. Something like:
// return true if car has been purchased false otherwise
bool alreadyPurchased(car purch[], char carnum[4])
{
for(int i = 0; i < loc; i++)
{
if(strcmp(purch[i].car1, carnum) == 0)
{
return true;
}
}
return false;
} |
|
|
|
|
|
#34 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 329
Rep Power: 4
![]() |
ok thats close however the variable is wrong. car purch[] does not have a variable car1. This variable would be carNum in the car class. Do i want to make a get method for this variable and get it because its private in the car class? Or i am supposed to pass it an array of purchases from the purchase class? I think the method will work with a couple things changed to make the variables line up. I do not know how to do that though. I made it this
In car class: //return carNum
string getCarNum()
{
return carNum;
}In purchase class // return true if car has been purchased false otherwise
bool alreadyPurchased(car purch[], char carnum[4])
{
for(int i = 0; i < loc; i++)
{
if(strcmp(purch->getCarNum(), carnum) == 0)
{
return true;
}
}
return false;
}When compiling i get this error Quote:
|
|
|
|
|
|
|
#35 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
Yes that was a typo, all of your other methods in purch take a purchase array as the first object, why wouldn't this one? It should be
bool alreadyPurchased(purchase purch[], char carnum[4]) I forgot to change it as it was about that time that I realised you had also used purch for the boolean local variable, so I had to remove that. Also the fact that the parameter was called purch should have clued you in that it was of type purchase, not car. So in short, yes it was my mistake, but you should try to understand what is in the code before copying it. |
|
|
|
|
|
#36 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 329
Rep Power: 4
![]() |
yea i know. I realized that and got it. Now the only problem that remains is if they enter a car number that has already been purchased it does in fact correctly say it has been purchased. However, if they enter a car number that has already been purchased and then the next car they enter can be a car number that does not exist and it does not give them an error. To solve this problem would i just also put the findCar while loop after the alreadyPurchased while loop or would i put the findCar while loop inside the alreadyPurchased while loop or how would i do it so it will check to make sure the number exists? Heres the snippet of code im dealing with now.
cout << "Enter an existing car number: ";
cin >> purch[loc].car1;
while(!cars->findCar(cars, purch[loc].car1))
{
cout << "Not an existing car number! \n"
<< "Please enter an existing car number: ";
cin >> purch[loc].car1;
}
while(alreadyPurchased(purch, purch[loc].car1))
{
cout << "Already been purchased \n"
<< "Please enter a different existing car number: ";
cin >> purch[loc].car1;
} |
|
|
|
|
|
#37 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
I'd probably use a while true loop
something like (pseudo code): while(true)
{
get car
check car
if not a car
{
display message
continue;
}
check purchased
if is purchased
{
display message
continue
}
// All is well, so quit out of the loop
break;
}Edit: you might also want to put a way out of the loop if there are no cars left to be purchased, otherwise the user will be stuck in the loop forever. |
|
|
|
|
|
#38 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 329
Rep Power: 4
![]() |
ive tested it a couple times and it has worked ok so far. I have to go somewhere now i will test it more later tonight thanks for all the help.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|