Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 26th, 2006, 3:25 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
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;
}
cwl157 is offline   Reply With Quote
Old Feb 26th, 2006, 4:49 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I have only glanced at your code. This is usually wrong:
Quote:
for(int i =0; i <= loc; i++)
As it will loop for values from 0 through loc, which is normally one too many.
__________________
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
DaWei is offline   Reply With Quote
Old Feb 26th, 2006, 5:19 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 26th, 2006, 5:29 PM   #4
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
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?
cwl157 is offline   Reply With Quote
Old Feb 27th, 2006, 7:14 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC