Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   print method (http://www.programmingforums.org/showthread.php?t=8601)

cwl157 Feb 26th, 2006 3:25 PM

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;
}


DaWei Feb 26th, 2006 4:49 PM

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.

nnxion Feb 26th, 2006 5:19 PM

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).

cwl157 Feb 26th, 2006 5:29 PM

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?

DaWei Feb 27th, 2006 7:14 AM

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.


All times are GMT -5. The time now is 4:37 PM.

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