![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 343
Rep Power: 4
![]() |
print method
Alright, i have a program that allows users to add an employee, display employees, add a car, and display cars. These are broken into classes a car class and an employee class. The functionality of employee works fine. However, in car class when i display the cars to the screen the last line is a zero. It is this way if you add 1 car or 4 cars. I can not figure out why because i use the same method to display the cars as i do employees. Heres the code:
#include <iostream>
#include <string.h>
using namespace std;
//routines for adding a class, displaying a class, 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;
}
}
//return name
string getName()
{
return name;
}
//return emp_id
string getEmp_id()
{
return emp_id;
}
}; //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;
}
}
//return make_model
string getMake_model()
{
return make_model;
}
// return carNum
string getCarNum()
{
return carNum;
}
}; //end of car class
int main()
{
//employee *emp;
car *cars;
//emp = new employee[100];
cars = new car[100];
//emp->addEmployee(emp);
//emp->addEmployee(emp);
//emp->addEmployee(emp);
//emp->displayEmployees(emp);
//emp->addEmployee(emp);
//emp->addEmployee(emp);
//emp->displayEmployees(emp);
cars->addCar(cars);
cars->displayCar(cars);
//cars->addCar(cars);
//cars->addCar(cars);
//cars->addCar(cars);
//cars->displayCar(cars);
return 0;
} |
|
|
|
|
|
#2 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I have only glanced at your code. This is usually wrong:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Don't have much time right now, but you should include <string>.
<string.h> can be replaced by <cstring>, I'm not sure you even need it (didn't look that far).
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 343
Rep Power: 4
![]() |
yea i changed the for loop to for(int i = 0; i < loc; i++) but now my question is why did: for(int i = 0; i <=loc; i++) work for displaying employees and not for cars? Isnt it pretty much the same thing?
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Sometimes a bug doesn't bite -- until you least expect it. If you printed one too many employees, it might have just had no effect. Could have been a totally 'empty' piece of information that terminated the process. One can't rely on that; when the customer's present, it'll always make your machine run off into the weeds and upchuck. Murphy's law.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|