I need help in understanding an assignment

Posted on 2004-11-15
Last Modified: 2008-01-09
I am working on an assignment and my instructor says that I do not have a main method.  I do not understand what is meant by this.  I would appreciate any feedback. I am sure that is a basic program and it is as follows:

"you are the owner of a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand, and the cost of each one.  Write a program that initializes the random-access file "hardware.dat" to one hundred empty records, lets you input the data concerning each tool that you no longer have and lets you update any information in the file."  Then information is given regarding the record numbers, tool name, quantity and cost.  

Here is the code that I have in my .cpp file.  

#ifndef HardwareData_h
#define HardwareData_h

#include <iostream>

using std::string;

class HardwareData {


      HardwareData (int = 0, string = "", string = "", double = 0.0);

      void setRecordNumber( int );
      int getRecordNumber() const;

      void setToolName(string);
      string getToolName() const;

      void setQuantity(string);
      string getQuantity() const;

      void setCost (double );
      double getCost() const;

      int RecordNumber;
      char ToolName [20];
      char Quantity [4];
      double Cost;



Thank you.

Question by:klrqueen13
    LVL 22

    Accepted Solution

    You have to actually write the code that implements all those nice functions.

    for example:

    void SetRecordNumber( int r ) {   RecordNumber = r; }

    Author Comment

    Do you mean in say a hardware.h file?  I have been working on this for hours and hours..and am still lost.
    LVL 23

    Expert Comment

    > I am working on an assignment and my instructor says that I do not have a main method.  
    > I do not understand what is meant by this.

    Computer programs must have an initial entry point where the program begins execution.
    By convention, the c (and c++) runtime library defines that starting point as a function called
    main().   By including a function called main() in your program, you are instructing the C runtime
    that the program starts 'here'.

    LVL 10

    Expert Comment

    Go thru this link for starting to write your program. It provides an explanation of the main() method etc.

    Regarding your cpp file implementation,

    In C++, U should declare the class in a .h (header) file and the implementation (define the class) in a .cpp (iplementation) file.
    These links would help

    LVL 39

    Expert Comment

    >> Here is the code that I have in my .cpp file

    - Move all your code to a file called 'hardwaredata.h'.

    - Change

             #include <iostream>


             #include <string>

       in hardwaredata.h

    - Add the following include statements to the (now empty) cpp file

        #include "hardwaredata.h"

        #include <iostream>
        #include <string>

        using namespace std;

    - Add an implementation for all your class member functions, e. g.

         HardwareData::HardwareData (int RecNum,
                                                      string strToolName,
                                                      string strQuantity ,
                                                       double dCost)
              :  RecordNumber(RecNum)
              , Cost(dCost)
               if (strToolName.length() < sizeof(ToolName))
                    strcpy(ToolName, strToolName.cstr());
                      cout << "Tool-Name " << strToolName << ", string is too long " << endl;
               if (strQuantity.length() < sizeof(Quantity))
                    strcpy(Quantity, strQuantity.cstr());
                      cout << "Quantity " << strQuantity << ", string is too long " << endl;

         void HardwareData::setRecordNumber( int RecNum )

         int HardwareData::getRecordNumber() const

         void HardwareData::setToolName(string strToolName)

         string HardwareData::getToolName() const

         void HardwareData::setQuantity(string strQuantity)

         string HardwareData::getQuantity() const

         void HardwareData::setCost (double dCost)
         double HardwareData::getCost() const

    - Finally, you need a main() function where you can test your class.

      Add the function below all other implementations.

       int main(int nArgs, char* pszArgs[])
             HardwareData hd1;

             HardwareData pHd2 = new HardwareData(2, "Keyboard", "12", 12.99);  
             double cost = pHd2->getCost();

             delete pHd2;

             return 0;  

    TODO:  replace ....  by  implementation code.

    Regards, Alex


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C++ Help from Top Experts

    Solve your toughest problems, fast.
    C++ experts are online now and ready to help you.

    Suggested Solutions

    Title # Comments Views Activity
    Finding a good hash function 4 109
    ADO Memory leak with DELPHI 2007 37 140
    how to understand recursion 12 192
    C++ question 3 26
    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now