Solved

Concerned about the size of source code

Posted on 2004-04-01
45
373 Views
Last Modified: 2010-04-01
Hi
I am learning C++ and I have to design a source code for a spare parts company.
The requirements are:
Gather the spare part code entered by the buyer.
Get the number of the particular parts required.
Produce an invoice showing the part , Quantity ordered and Price.
Show the total price of the invoice.
Update the parts on hand for each part ordered.
Then finally advise if any parts need to be reordered.
While the user is entering the parts required my program it has to state if an incorrect part number or number of parts requested are entered by the user.

My problem is that I have written the code (not complete as yet) and I have 4 pages of code.  Does this seem excessive?


Thanks
Kaz.
0
Comment
Question by:KazIT
  • 20
  • 9
  • 8
  • +3
45 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10731016
I dont think that anyone can comment on that without looking at your code
Each one has his/her own method of catering to the requirements
So, it may be poosible that u can shorten your code OR may be perfectly OK

Can u Post your code so that we can help

Amit
0
 

Author Comment

by:KazIT
ID: 10731076
Like I said previously it's nowhere near finished as yet.  But here goes.

//PREPORCESSOR DIRECTIVES
#include <iostream>            //FOR CIN AND COUT
#include <iomanip>            //SETW
#include <cctype>            //FOR TOUPPER
#include <string>            //FOR STRING CLASS
using namespace std;      //REQUIRED WHEN USING IOSTREAM

//FUNCTION DECLARATIONS (void = return type not used again in the program)

void Welcome (void);
{
//THIS FUNCTION DISPLAYS A WELCOME MESSAGE THAT INCLUDES
//YOUR CUSTOMER  NUMBER AND NAME
      cout << "Welcome to 12345 John Brown Spare Parts.\n";
            << "We supply parts for your vehicle. " <<endl;
return 0;
}//END Welcome

void ReadChar(char &Char);

int main ()
{
      //DEFINE VARIABLES
      int AxlesStock =0;            //AXLES IN STOCK
      int BearingsStock =0;                     //BEARINGS IN STOCK
      int BrakeStock =0;            //BRAKES IN STOCK
      int CustomerId =0;            //CUSTOMER ID
      int Code =0;            //CODE
      string Part = " ";            //PART
      int QuantityOnHand =0;      //QUANTITY ON HAND
      double Price = 0.0;            //PRICE OF ITEM
      int reorderLevel =0;                     //REORDER LEVEL
      int numberRequired =0;      //NUMBER OF PARTS REQUIRED
      char Answer = ' ';

//THIS OUTPUT ASKS IF THERE ARE ANY CUSTOMERS AND CHECKS FOR INCORRECT INPUT.
      cout << "Are there any customers? (Y/N) => ";
      ReadChar(Answer);            //READ ANSWER
      Answer = toupper (Answer);      //CONVERT TO UPPERCASE

// IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
while ((Answer != 'N') && (Answer != 'Y'))
{
            cout << "Invalid Entry. Try Again.\n";
                  << "Are there any customers? (Y/N) => ";
            ReadChar(Answer);
            Answer = toupper (Answer);
}

if (Answer == 'Y')
{
      cout << "Please enter your Customer ID =>";
      cin >> CustomerId;
}

else if (Answer == 'N')
{
      cout << "\nThankyou and Goodbye." <<endl;
}


void DisplayStock (int AxleStock, int BearingsStock, int BrakeStock);
{

//THIS FUNCTION DISPLAYS A LIST OF THE ITEMS AVAILABLE, THEIR PRICES
//AND THE NUMBERS OF EACH ITEM THAT ARE AVAILABLE FOR SALE

//DEFINE AND INITIALISE PART 1 OBJECTS AND VARIABLES
int Code1 = 1;                  //CODE PART 1
string Part1 = "Rear Axle";            //PART REAR AXLE
double Price1 = 128.00;            //PRICE PART 1
int QuantityOnHand1 = 5;            //QUANTITY ON HAND PART 1
int reorderLevel1 = 2;                  //REORDER LEVEL PART 1

//DEFINE AND INITIALISE PART 2 OBJECTS AND VARIABLES
int Code2 = 2;                  //CODE PART 2
string Part2 = "Bearings";            //PART BEARINGS
double Price2 = 25.50;            //PRICE PART 2
int QuantityOnHand2 = 20;            //QUANTITY ON HAND PART 2
int reorderLevel2 = 10;            //REORDER LEVEL PART 2

//DEFINE AND INITIALISE PART 3 OBJECTS AND VARIABLES
int Code3 = 3;                  //CODE PART 3
string Part3 = "Brake Pads";            //PART BRAKE PADS
double Price3 = 30.00;            //PRICE PART 3
int QuantityOnHand3 = 12;            //QUANTITY ON HAND PART 3
int reorderLevel3 = 6;                  //REORDER LEVEL PART 3

cout << "\nOur current stock is: " ;

//DISPLAY HEADER
cout << "\nCode\tPart\tQuantity On Hand\tPrice"<< endl;

//DISPLAY CURRENT STOCK
cout << Code1 << "\t" << Part1 << "\t\t" << QuantityOnHand1 << "\t\t"
      << "$" << Price1 << endl;
cout << Code2 << "\t" << Part2 << "\t\t" << QuantityOnHand2 << "\t\t"
      << "$" << Price2 << endl;
cout << Code3 << "\t" << Part3 << "\t\t" << QuantityOnHand3 << "\t\t"
      << "$" << Price3 << endl;

//RETURN
return 0;
}//END Display Stock

void GetOrderQuantities(int& AxleQty, int& BearingsQty, int& BrakeQty,
            int AxleStock, int BearingsStock, int BrakeStock);
{
//THIS FUNCTION OBTAINS FROM THE USER THE ITEM CODE AND
//THE QUANTITY OF EACH ITEM THAT ARE REQUIRED BY THE USER

cout << "Enter the code for the part you want to buy => ";
cin >> Code;

int GetIntegerResponse(      // return response entered by user
      int Lower =1,                  //Lower bound on valid responses
      int Upper =3)                  //Upper bound on valid responses
{
int
Response;

//Get response from user; if not in the range Lower..Upper print
//error msg and loop until Response in this range.
do
{
      ReadInteger(Response);
      if (Response < Lower || Response > Upper)
      {
      cerr << "n\t\tERROR: Number must be between " << Lower << " and "
                  << Upper << "==> ";
      }
}while (Response < Lower || Response > Upper);

//return the user's selection
return Response;
}
 void ReadInteger(int &Integer)      // function definition
 {
cout << "How many of this part would you like => ";
cin >> numberRequired;

      {
       cin >> mumberRequired;
       while (cin.fail())
       {
             cin.clear();                  // clear input stream flags
             Clear();                        // clear to end of line
             cout << "ERROR:  You must enter number required ==> ";
             cin << numberRequired;            // try again
       }
       Clear ();                              // clear to end of line
 }
 }
void Clear(void)                        // clear standard input to end of line
{
      char Char = ' ';
      while (Char != '\n')
      {
            cin.get(Char);
      }
}
      char prompt;                        //prompt for more parts
      do {

cout << "Would you like to buy more parts? (y/N)";
cin >> prompt;

}while (toupper(prompt) == 'Y');      // while answer Y loop otherwise end
return 0;
}//END


void UpdatePartsStock(int& AxleStock, int& BearingsStock,
int&  BrakeStock,  int AxleQty, int BearingsQty, int BrakeQty);
{
//THIS FUNCTION UPDATES THE QUANTITY OF EACH ELEMENT THAT REMAINS IN STOCK
//AFTER THE CUSTOMER IS SERVED

float TotalCostParts(int AxleQty, int BearingsQty, int BrakeQty);

      //DECLARE LOCAL VARIABLES
      int AxleQty,
      BearingsQty,
      BrakeQty;


      if (Code == 1)
      {
            AxleQty = AxlesStock - numberRequired;
      }
      if (Code == 2)
      {
            BearingsQty = BearingsStock - numberRequired;
      }
      if (Code == 3)
      {
            BrakeQty = BrakeStock - numberRequired;
      }
      else
      {
            cout << "\nInvalid Code......Try again.";
      }


//THIS FUNCTION CALCULATES AND RETURNS THE TOTAL COST OF THE ITEMS
//THAT HAVE BEEN ORDERED

void DisplayInvoice ( int CustomerId, int AxleQty, int BearingsQty,
                               int BrakeQty);
{
//THIS FUNCTION DISPLAYS THE INVOICE DETAILS
int sum =0;
float totalCostOfParts =0;



}//END Display Invoice
void Reorder (int AxlesStock, int BearingsStock, int BrakeStock);
{

cout << "Invoice for Customer Number:" << CustomerId << endl;

cout << "Part\tQuantity\tCost" <<endl;
cout << "----\t--------\t----" <<endl:
//DISPLAY ITEMS ON INVOICE
cout << Code <<
(totalCostOfParts = (sum/Price));
cout << " Total Cost of parts: $" << totalCostOfParts <<endl;


//THIS FUNCTION DETERMINES WHICH ITEMS SHOULD BE REORDERED
if (QuantityOnHand < reorderLevel)
{
      cout << "The following parts should be reordered: " <<endl;
}
else
{
      cout << "No parts to be reordered at present. " << endl;
}
return 0;
}//END main()

0
 
LVL 16

Expert Comment

by:nonubik
ID: 10731127
KazIT, why are you concerned about the size of the source code?
0
 

Author Comment

by:KazIT
ID: 10731159
nonubik,
It's the first time I have attempted something of this nature it seems a little overwhelming to me but I wondered if the code can be shortened here or there or if in fact it needs to be? Other than whatever errors I have within it.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10731176
I feel the code can be more organised which would definitely reduce its length
Also, I feel that u are not really using the features C++ provides
U should use the class feature to have your data more organised

For example
A rough design can be something like this
(I am assuming that u have only 3 Spares Parts as your program reflects)

// This class can represent a sparePart.
// It would also contain methods which would operate on the spare part (increase/decrease qtyOnHand etc )
class SparePart {
       int code ;
       int price ;
       int qtyOnHand ;
} ;

// This class can represent the SpareParts u currently have. This can be implemented by having a array of SparePart type
This class would have members functions which would intern call member functions of SparePart with the particular array object .
class Stock {      
}


int main ( ) {
     Stock stk ;
      // User Interaction piece
}

HTH

Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10731182
Also, some areas where u can shorten more
instead of using multiple if-elseif statements , use switch-case as u are comparing just integer values

Amit
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10731232
Hi KazIT,
Sounds like you're trying to implement a full-scale logistics application. Good luck :-)

OK, here are a couple of ideas:
>int AxlesStock =0;          //AXLES IN STOCK
>int BearingsStock =0;                    //BEARINGS IN STOCK
[...]

Handle this in a more generic manner. Think OO:

* Stock articles have common attributes, so define a StockArticle class which can be instantiated as rear axle, type 012 roller bearing, and such. Define methods for querying available items, costs, ...

* In case you want to group your articles into axles, bearings, etc., define a StockCategory, which could be nested for a hierarchy of groups, if you want to.

* Invoices consist of one or more invoice items. Invoices could be queried for total sum. Each invoice is attached to exactly one customer.

* Logistic business rules (reorder). Good one. You can either use an automatic reorder mechanism (like checking the stock after each order and having an individual threshold value for an item), or you have a "daily reorder check job", which goes over the entire stock and checks.



Read a good book or two. I recommend Bruce Eckel's "Thinking in ..." books (http://www.bruceeckel.com), which are available as soft copy, too:

"Thinking in C++":
http://64.78.49.204/TICPP-2nd-ed-Vol-one.zip
http://64.78.49.204/TICPP-2nd-ed-Vol-two.zip

"Thinking in Patterns":
http://64.78.49.204/TIPatterns-0.9.zip


Cheers,
Stefan
0
 

Author Comment

by:KazIT
ID: 10731242
I have a few guidelines on this program.  I can use functions, selection and repetition statements but I'm not required to use arrays.
I will take your advice on board Amit and use the switch case you mentioned.
Thanks.
0
 

Author Comment

by:KazIT
ID: 10731249
Thanks for the ideas Stefan.  I need all the good luck offers I can get. :)

Kaz
0
 
LVL 22

Expert Comment

by:grg99
ID: 10731904
I would write the program in such a way as there are no variables named "brake hose quantity", or any other name.

When you think about it, in a real program, you'd like to be able to add a new item without having to write any new code.

Therefore all the part names and quantities should be READ IN FROM A FILE.

The progran shoul djust have arrays of part names and quantities.
Each one can be handled in the same way.
That way you can handle ONE or a MILLION different parts, all without changing a line of code.

Hope this helps.

0
 
LVL 12

Expert Comment

by:stefan73
ID: 10733319
KazIT,
> I'm not required to use arrays

Arrays are a feature to avoid in C++, anyway. Use vectors instead.

Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10733337
grg99,
> Therefore all the part names and quantities should be READ IN FROM A FILE.
Hmmm... I hear a faint voice saying "database" in my head :-)

Stefan
0
 
LVL 22

Expert Comment

by:grg99
ID: 10733514
Yes, a dfatabase is obviously the way to go, but this looks more like a programming assignment in an intro programming class, rather than one in a database class (I may be wrong).

If the goal was to learn how to interact with a proper database, I suspect the instructor would have given the strong hint:  "Dont roll your own, use these interfaces to database system XXXX".

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10733923
I analyzed your code and in the moment it is a mess or let's say some code ideas where two of three statements will not compile because of typos, syntax errors or lack of definitions. I couldn't think that this program can be accomplished with reasonable efforts.


Some Comments.

1. For a beginner a top-down design is better than the bottum-up approach you have chosen. You should always try to have a compilable program as you haven't a chance to finish the prog if you had to handle a zillion of compile errors.

2. So as it is C++ you shouldn't begin with func Welcome() or main() but with a class. What class? As you want to run a store why not calling it 'Store'. Normally, we would create a new header file 'store.h' and enter a class declaration like this:

//---------------------------- store.h ------------------------
#ifndef STORE_H
#define STORE_H

class Store
{
private:

public:
   Store();
   ~Store();

   // That function does the job
   int run();
};

#endif // STORE_H
//---------------------------- store.h ------------------------

You see, we have no members at first approach except of a constructor, a destructor and a run() function (i'll explain below). Then we need an implementation of this.

//---------------------------- store.cpp ------------------------
#include "store.h"

// constructor
Store::Store()
{
}

// destructor
Store::~Store()
{
}

// That function does the job
int Store::run()
{
    // todo ...
    return 0;
}
//---------------------------- store.cpp ------------------------

With C++ the program logic will be moved from main() to a class member function. So, in main()

3. In order to get a program we need a main function


//---------------------------- main.cpp ------------------------
#include "store.h"

void main()
{
    Store store;
    return store.run();
}
//---------------------------- main.cpp ------------------------


4. Now add these 3 files to your project (or better start a new empty one), compile and build it. Voilà, it compiles and you got an executable!

5. Enhance your Store class adding Welcome and Dialog functions


class Store
{
private:

public:
   Store();
   ~Store();

   // That function does the job
   int run();

public:
   // welcome the customer
   void welcome();
   // ask for Yes/No using prompt and return true if "Y" and false if "N"
   bool askYesNo(const string& prompt);
   // ask for a number within a range
   int askNumber(const string& prompt, int from, int to);
   // ask for a string and show an error before if the previous answer had been wrong
   bool askString(const string& prompt, const string& exitCode, string& value,
                  bool showError);

};

6. Implement the new functionality

//---------------------------- store.cpp ------------------------
#include "store.h"

#include <iostream>
#include <string>

// constructor
Store::Store()
{
}

// destructor
Store::~Store()
{
}

// That function does the job
int Store::run()
{
    welcome();
    if (!askYesNo("Are there any customers?"))
        return 1;

    // todo
    return 0;

}
//---------------------------- store.cpp ------------------------



// welcome the customer
void Store::welcome()
{
     cout << "Welcome to 12345 John Brown Spare Parts.\n"
          << "We supply parts for your vehicle. " << endl << endl;
}

// ask for Yes/No using prompt and return true if "Y" and false if "N"
bool Store::askYesNo(const string& prompt)
{
    char c   = '\0';
    bool ret = false;
    while (true)
    {
        cout << prompt << " (Y/N) => ";
        cin >> c;
        c = toupper(c);

        if (c == 'Y')
        {
            ret = true;
            break;
        }
           
        if (c == 'N')
            break;

        cout << endl << "Invalid input. Please enter 'Y' or 'N'" << endl << endl;
    }
    return ret;
}

// ask for a number within a range
int askNumber(const string& prompt, int from, int to)
{
    int ret = 0;
    while (true)
    {
        cout << prompt << "(" << from << ":" << to << ") => ";
        cin >> ret;
        if (ret >= from && ret >= to)
            break;  

        cout << endl << "Invalid input. Please enter a number from " << from << " to " << to << endl << endl;
    }
    return ret;
}

// ask for a string and show an error before if the previous answer had be wrong
bool Store::askString(const string& prompt, const string& exitCode, string& value,
                 bool showError)
{
    bool ret = false;
    if (showError)
    {
        cout << endl << "[" << value << "], Invalid input. Please enter again or type '" << exitCode << "' to quit." <<endl << endl;
    }

    cout << prompt << "['" << exitCode << "' to quit] : ";
    cin >> value;

    if (value.length() == exitCode.length())
    {
        for (int i = 0; i < value.length(); i++)
        {
            if (toupper(value[i]) != toupper(exitCode[i]))
            {
                ret = true;
                break;
            }
        }
    }
    else
        ret = true;

    return ret;

}

//----------------------------------------- store.cpp ---------------------------------------


7. Then, create 2 new classes, say Customer and StorePart (following Stefan's suggestion). Use files customer.h, customer.cpp, storepart.h and storepart.cpp. Declare members customerId, customerName and partId, partName, price, quantity, reorderLevel, make them all private and create default constructors, full constructors (where values for all members get passed) and copy constructors. Also, get and set functions for all members. If all is declared you have to implement all these functions. Add the files to your project and try to compile and build.

8. I'll give you further help if you want to follow me on that way.


Regards, Alex
 
0
 

Author Comment

by:KazIT
ID: 10756165
Thankyou itsmeandnobodyelse I appreciate the time you spent on your response to me.  
grg99 you are right with the intro to programming not database.  I have been given some functions that I have to implement they are

void Welcome (void);
//THIS FUNCTION DISPLAYS A WELCOME MESSAGE THAT INCLUDES
//THE NUMBER AND NAME

void DisplayStock (int AxleStock, int BearingsStock, int BrakeStock);
//THIS FUNCTION DISPLAYS A LIST OF THE ITEMS AVAILABLE, THEIR PRICES
//AND THE NUMBERS OF EACH ITEM THAT ARE AVAILABLE FOR SALE

void GetOrderQuantities(int& AxleQty, int& BearingsQty, int& BrakeQty,
            int AxleStock, int BearingsStock, int BrakeStock);
//THIS FUNCTION OBTAINS FROM THE USER THE ITEM CODE AND
//THE QUANTITY OF EACH ITEM THAT ARE REQUIRED BY THE USER

void UpdatePartsStock(int& AxleStock, int& BearingsStock,
int&  BrakeStock,  int AxleQty, int BearingsQty, int BrakeQty);
//THIS FUNCTION UPDATES THE QUANTITY OF EACH ELEMENT THAT REMAINS IN STOCK
//AFTER THE CUSTOMER IS SERVED

float TotalCostParts(int AxleQty, int BearingsQty, int BrakeQty);
//THIS FUNCTION CALCULATES AND RETURNS THE TOTAL COST OF THE ITEMS
//THAT HAVE BEEN ORDERED

void DisplayInvoice ( int CustomerId, int AxleQty, int BearingsQty, int BrakeQty);
//THIS FUNCTION DISPLAYS THE INVOICE DETAILS

void Reorder (int AxlesStock, int BearingsStock, int BrakeStock);
//THIS FUNCTION DETERMINES WHICH ITEMS SHOULD BE REORDERED

As well as using these routines that they have given me on
int GetIntegerResponse(int Lower, Int Upper);
void ReadInteger(int &Integer);
Void Clear(void);

Amit
I have tried to implement a switch by this
      int AxleQty,
            BearingsQty,
            BrakeQty;

      switch (Code)
      {
      case 1:      AxleQty = AxlesStock - numberRequired;
            break;

      case 2:      BearingsQty = BearingsStock - numberRequired;
            break:

      case 3:      BrakeQty = BrakeStock - numberRequired;
            break;
            
      case 4:      cout << "\nInvalid Code......Try again.";
      }//END switch






0
 

Author Comment

by:KazIT
ID: 10763367
Point increased to 500
0
 

Author Comment

by:KazIT
ID: 10764536
Can anyone out there help me with this please?
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10764762
With implementing the functions?
0
 

Author Comment

by:KazIT
ID: 10764781
Yes.
I code is pretty much as I have first shown.  I  have made a few changes. I took out the if statements and used switch.
When I compile I get this error
error C2601: 'ReadChar' : local function definitions are illegal
would it be more helpful if I reposted the whole code with changes again?

Kaz.
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10764819
The compiler error should be because of an unmatched } in your code. Check out for the { } pairs.
And if you can't find it quick, just repost the code for more help
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10764861
>> Thankyou itsmeandnobodyelse I appreciate the time you spent on your response to me.  

Is that a 'Yes' or a 'No' to my offer 'I'll give you further help if you want to follow me on that way' ??

>> When I compile I get this error
>> error C2601: 'ReadChar' : local function definitions are illegal

I tried to compile your code above. However, i got hundreds of errors. The main reason for this is incorrect nesting of function bodys, i. e.  the curly brackets of your functions are not set correctly. I made some efforts to get your code compiled (but not linked). I can post it if you want. However, you would have to redo your last improvements. And my recommendation is to start a new project and do it the C++ way.

Regards, Alex








0
 

Author Comment

by:KazIT
ID: 10764877
Iam still working on parts of the code eg...I'm not sure how to get the Price column on the Invoice to total for all requested parts.

I know it is a mess but I have actually managed to get my errors from 20 odd down to 2 for the moment.

I am also unsure of how to get the Quantity on Hand to decrease for each item if parts are ordered.

//PREPORCESSOR DIRECTIVES
#include <iostream>            //FOR CIN AND COUT
#include <iomanip>            //SETW
#include <cctype>            //FOR TOUPPER
#include <string>            //FOR STRING CLASS

using namespace std;      //REQUIRED WHEN USING IOSTREAM

void Welcome (void)

{
//DEFINE VARIABLES
char Answer;
int AxlesStock =0;            //AXLES IN STOCK
int BearingsStock =0;      //BEARINGS IN STOCK
int BrakeStock =0;            //BRAKES IN STOCK
int CustomerId =0;            //CUSTOMER ID
int Code =0;                  //CODE (GLOBAL)
string Part = " ";            //PART (GLOBAL)
int QuantityOnHand =0;      //QUANTITY ON HAND (GLOBAL)
double Price = 0.0;            //PRICE OF ITEM (GLOBAL)
int reorderLevel =0;      //REORDER LEVEL
int numberRequired =0;      //NUMBER OF PARTS REQUIRED
string welcomeMessage ="Welcome to J3456 John Brown Spare Parts.";
string supplying = "We supply parts for your vehicle. ";

//THIS FUNCTION DISPLAYS A WELCOME MESSAGE THAT INCLUDES
//YOUR  NUMBER AND NAME
{
      cout << welcomeMessage <<endl;
      cout << supplying <<endl;
}
//END Welcome()
//-----------------------------------------------------------------------
void ReadChar(char &Char)
{
//THIS OUTPUT ASKS IF THERE ARE ANY CUSTOMERS AND CHECKS FOR INCORRECT INPUT.
      cout << "Are there any customers? (Y/N) => ";
      ReadChar(Answer);                  //READ ANSWER
      Answer = toupper (Answer);      //CONVERT TO UPPERCASE

// IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
while ((Answer != 'N') && (Answer != 'Y'));
{
            cout << "ERROR:  Valid responses are 'Y' or 'N'" <<endl;
                  << "Are there any customers? (Y/N) => ";
            ReadChar(Answer);
            Answer = toupper (Answer);

if (Answer == 'Y')
{
      cout << "Please enter your Customer ID =>";
      cin >> CustomerId;
}

else if (Answer == 'N')
{
      cout << "\nThankyou and Goodbye." <<endl;
}
}//END while

//----------------------------------------------------------------------
//DEFINE VARIABLES

void DisplayStock (int AxleStock, int BearingsStock, int BrakeStock)
{
//THIS FUNCTION DISPLAYS A LIST OF THE ITEMS AVAILABLE, THEIR PRICES
//AND THE NUMBERS OF EACH ITEM THAT ARE AVAILABLE FOR SALE

//DEFINE AND INITIALISE PART 1 OBJECTS AND VARIABLES
int Code1 = 1;                                    //CODE PART 1
string Part1 = "Rear Axle";                  //PART REAR AXLE
double Price1 = 128.00;                        //PRICE PART 1
int QuantityOnHand1 = 5;                  //QUANTITY ON HAND PART 1
int reorderLevel1 = 2;                        //REORDER LEVEL PART 1

//DEFINE AND INITIALISE PART 2 OBJECTS AND VARIABLES
int Code2 = 2;                                    //CODE PART 2
string Part2 = "Bearings";                  //PART BEARINGS
double Price2 = 25.50;                        //PRICE PART 2
int QuantityOnHand2 = 20;                  //QUANTITY ON HAND PART 2
int reorderLevel2 = 10;                        //REORDER LEVEL PART 2

//DEFINE AND INITIALISE PART 3 OBJECTS AND VARIABLES
int Code3 = 3;                                    //CODE PART 3
string Part3 = "Brake Pads";            //PART BRAKE PADS
double Price3 = 30.00;                        //PRICE PART 3
int QuantityOnHand3 = 12;                  //QUANTITY ON HAND PART 3
int reorderLevel3 = 6;                        //REORDER LEVEL PART 3

cout << "\nOur current stock is: " <<endl;

//DISPLAY HEADINGS
cout.precision(2);
cout << "\n\n\n\n\n"
       << setw(20) <<"Code"
       << setw(20) <<"Part"
       << setw(20) <<"Quantity On Hand"
       << setw(20) <<"Price" << endl;

//DISPLAY CURRENT STOCK
cout << setw(20) << Code1
       << setw(20) << Part1
       << setw(20) << QuantityOnHand1
       << setw(20) << Price1 << endl;

cout << setw(20) << Code2
       << setw(20) << Part2
       << setw(20) << QuantityOnHand2
       << setw(20) << Price2 << endl;

cout << setw(20) << Code3
       << setw(20) << Part3
       << setw(20) << QuantityOnHand3
       << setw(20) << Price3 << endl;
//RETURN

}//END Display Stock
//---------------------------------------------------------------------
void GetOrderQuantities(int& AxleQty, int& BearingsQty, int& BrakeQty,
            int AxleStock, int BearingsStock, int BrakeStock)
{
//THIS FUNCTION OBTAINS FROM THE USER THE ITEM CODE AND
//THE QUANTITY OF EACH ITEM THAT ARE REQUIRED BY THE USER

cout << "Enter the code for the part you want to buy => ";
cin >> Code;

int GetIntegerResponse(      // return response entered by user
      int Lower,                  //Lower bound on valid responses
      int Upper)                  //Upper bound on valid responses
{
int
Response;

//Get response from user; if not in the range Lower..Upper print
//error msg and loop until Response in this range.
do
{
      ReadInteger(Response);
      if (Response < Lower || Response > Upper)
      {
            cerr << "n\t\tERROR: Number must be between " << Lower << " and "
                  << Upper << "==> ";
      }
}while (Response < Lower || Response > Upper);

//return the user's selection
return Response;
}
 void ReadInteger(int &Integer)      // function definition
 {
cout << "How many of this part would you like => ";
cin >> Integer;

      {
       cin >> Integer;
       while (cin.fail())
       {
             cin.clear();                  // clear input stream flags
             Clear();                        // clear to end of line
             cout << "ERROR:  You must enter number required ==> ";
             cin << Integer;            // try again
       }
       Clear ();                              // clear to end of line
 }
 }
void Clear(void)                        // clear standard input to end of line
{
      char Char = ' ';
      while (Char != '\n')
      {
            cin.get(Char);
      }
}
      char prompt;                        //prompt for more parts
      do {

cout << "Would you like to buy more parts? (y/N)";
cin >> prompt;

}while (toupper(prompt) == 'Y');      // while answer Y loop otherwise end

}//END
//-----------------------------------------------------------------------

float TotalCostParts(int AxleQty, int BearingsQty, int BrakeQty)

//THIS FUNCTION CALCULATES AND RETURNS THE TOTAL COST OF THE ITEMS
//THAT HAVE BEEN ORDERED


//----------------------------------------------------------------------

void DisplayInvoice ( int CustomerId, int AxleQty, int BearingsQty,
                               int BrakeQty)
{
//THIS FUNCTION DISPLAYS THE INVOICE DETAILS

      string Part = " ";
      int Quantity = 0;
      float Cost = 0.0;

cout << "Invoice for Customer Number:" << CustomerId << endl;

cout << "Part\tQuantity\tCost" <<endl;
cout << "----\t--------\t----" <<endl;

cout << Part << Quantity << Cost <<endl<<endl;

cout << " Total Cost of parts: $" << totalCostOfParts <<endl;


}//END Display Invoice
//--------------------------------------------------------------------

void UpdatePartsStock(int& AxleStock, int& BearingsStock, int&
                                BrakeStock,
                                int AxleQty, int BearingsQty, int BrakeQty)
{
//THIS FUNCTION UPDATES THE QUANTITY OF EACH ELEMENT THAT REMAINS IN STOCK
//AFTER THE CUSTOMER IS SERVED


      //DECLARE LOCAL VARIABLES
      int AxleQty,
            BearingsQty,
            BrakeQty;

      switch (Code)
      {
      case 1:      AxleQty = AxlesStock - numberRequired;
            break;

      case 2:      BearingsQty = BearingsStock - numberRequired;
            break;

      case 3:      BrakeQty = BrakeStock - numberRequired;
            break;
            
      case 4:      cout << "\nInvalid Code......Try again.";
      }//END switch
//--------------------------------------------------------------------

//THIS FUNCTION DETERMINES WHICH ITEMS SHOULD BE REORDERED
void Reorder (int AxlesStock, int BearingsStock, int BrakeStock)

if (QuantityOnHand < reorderLevel)
{
      cout << "The following parts should be reordered: " <<endl;
}
else
{
      cout << "No parts to be reordered at present. " << endl;

      return 0;
}

//-----------------------------------------------------------------------
//END main()
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:nonubik
ID: 10764907
You deffinetly need to close some brackets. Like }
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10764911
It's like for every { you MUST have a }
0
 

Author Comment

by:KazIT
ID: 10764913
itsmeandnobodyelse
As I have mentioned in one of my previous comments I have certain functions that I have to use without using arrays.
I would appreciate any advice using these proceedures.
0
 

Author Comment

by:KazIT
ID: 10764981
nonubik
I do understand the opening and closing brackets.  I guess I'm just getting confused now it seems I close my eyes and all I see is brackets. I have gone through and tried to make sure I have closing for each opening.  The last compile I have done gives me 2 errors
 error C2601: 'ReadChar' : local function definitions are illegal
: fatal error C1075: end of file found before the left brace '{'

Kaz
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10765006
Kaz, from your last code posted, I see you must
}//END Welcome()

It's only
//END Welcome()
in your post. This should repair the 'ReadChar' error.
It' just 2 lines above void ReadChar(char &Char)
And if you think you have them all paired, just try to count them.
0
 

Author Comment

by:KazIT
ID: 10765230
Ok I have counted and actually printed out my source code to make it easier to count them and I have a closing for each opening.  Now when I compile I get
(19) : warning C4101: 'Answer' : unreferenced local variable
(45) : error C2065: 'Answer' : undeclared identifier
(59) : error C2065: 'CustomerId' : undeclared identifier
(132) : error C2065: 'Code' : undeclared identifier
(137) : error C2601: 'GetIntegerResponse' : local function definitions are illegal
(167) : error C2065: 'Clear' : undeclared identifier
(169) : error C2676: binary '<<' : 'class std::basic_istream<char,struct std::char_traits<char> >' does not define this operator or a conversion to a type ac
ceptable to the predefined operator
(176) : error C2373: 'Clear' : redefinition; different type modifiers
(219) : error C2065: 'totalCostOfParts' : undeclared identifier
(235) : error C2082: redefinition of formal parameter 'BearingsQty'
(236) : error C2082: redefinition of formal parameter 'BrakeQty'
(240) : error C2065: 'AxlesStock' : undeclared identifier
(240) : error C2065: 'numberRequired' : undeclared identifier
(257) : error C2065: 'QuantityOnHand' : undeclared identifier
(257) : error C2065: 'reorderLevel' : undeclared identifier
(265) : error C2562: 'Reorder' : 'void' function returning a value
(255) : see declaration of 'Reorder'

ass2q1 2nd try.obj - 16 error(s), 1 warning(s)
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10765311
you are using variables inside the ReadChar() function without decalring them (like 'Answer'). I presume you want to use the ones declared in Welcome() function. Try to make them global.

Like moving
//DEFINE VARIABLES
char Answer;
int AxlesStock =0;          //AXLES IN STOCK
int BearingsStock =0;     //BEARINGS IN STOCK
int BrakeStock =0;          //BRAKES IN STOCK
int CustomerId =0;          //CUSTOMER ID
int Code =0;               //CODE (GLOBAL)
string Part = " ";          //PART (GLOBAL)
int QuantityOnHand =0;     //QUANTITY ON HAND (GLOBAL)
double Price = 0.0;          //PRICE OF ITEM (GLOBAL)
int reorderLevel =0;     //REORDER LEVEL
int numberRequired =0;     //NUMBER OF PARTS REQUIRED
string welcomeMessage ="Welcome to J3456 John Brown Spare Parts.";
string supplying = "We supply parts for your vehicle. ";

before
void Welcome (void)
{

And you left a { without a } in ReadChar function. Try:

}//END while
} //nonubik comment: here another } MUST be
//----------------------------------------------------------------------
//DEFINE VARIABLES

void DisplayStock (int AxleStock, int BearingsStock, int BrakeStock)

0
 
LVL 16

Expert Comment

by:nonubik
ID: 10765325
And you didn't post the brackets count result :)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10765336
Ok, that compiles. However, it will not work as main() doesn't call any of the functions.

//PREPORCESSOR DIRECTIVES
#include <iostream>          //FOR CIN AND COUT
#include <iomanip>          //SETW
#include <cctype>          //FOR TOUPPER
#include <string>          //FOR STRING CLASS

using namespace std;     //REQUIRED WHEN USING IOSTREAM

//DEFINE VARIABLES
char Answer;
int AxlesStock =0;          //AXLES IN STOCK
int BearingsStock =0;     //BEARINGS IN STOCK
int BrakeStock =0;          //BRAKES IN STOCK
int CustomerId =0;          //CUSTOMER ID
int Code =0;               //CODE (GLOBAL)
string Part = " ";          //PART (GLOBAL)
int QuantityOnHand =0;     //QUANTITY ON HAND (GLOBAL)
int totalCostOfParts =0;     //TOTAL COSTS OF PARTS (GLOBAL)
double Price = 0.0;          //PRICE OF ITEM (GLOBAL)
int reorderLevel =0;     //REORDER LEVEL
int numberRequired =0;     //NUMBER OF PARTS REQUIRED
string welcomeMessage ="Welcome to J3456 John Brown Spare Parts.";
string supplying = "We supply parts for your vehicle. ";

//THIS FUNCTION DISPLAYS A WELCOME MESSAGE THAT INCLUDES
//YOUR  NUMBER AND NAME
void Welcome (void)

{
     cout << welcomeMessage <<endl;
     cout << supplying <<endl;
}
//END Welcome()
//-----------------------------------------------------------------------
void ReadChar(char &Char)
{
//THIS OUTPUT ASKS IF THERE ARE ANY CUSTOMERS AND CHECKS FOR INCORRECT INPUT.
     cout << "Are there any customers? (Y/N) => ";
     ReadChar(Answer);               //READ ANSWER
     Answer = toupper (Answer);     //CONVERT TO UPPERCASE

// IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
     while ((Answer != 'N') && (Answer != 'Y'));
     {
         cout << "ERROR:  Valid responses are 'Y' or 'N'" <<endl
             << "Are there any customers? (Y/N) => ";
         ReadChar(Answer);
         Answer = toupper (Answer);
         
         if (Answer == 'Y')
         {
             cout << "Please enter your Customer ID =>";
             cin >> CustomerId;
         }
         
         else if (Answer == 'N')
         {
             cout << "\nThankyou and Goodbye." <<endl;
         }
     }//END while
}

//----------------------------------------------------------------------
//DEFINE VARIABLES

void DisplayStock (int AxleStock, int BearingsStock, int BrakeStock)
{
//THIS FUNCTION DISPLAYS A LIST OF THE ITEMS AVAILABLE, THEIR PRICES
//AND THE NUMBERS OF EACH ITEM THAT ARE AVAILABLE FOR SALE

//DEFINE AND INITIALISE PART 1 OBJECTS AND VARIABLES
int Code1 = 1;                              //CODE PART 1
string Part1 = "Rear Axle";               //PART REAR AXLE
double Price1 = 128.00;                    //PRICE PART 1
int QuantityOnHand1 = 5;               //QUANTITY ON HAND PART 1
int reorderLevel1 = 2;                    //REORDER LEVEL PART 1

//DEFINE AND INITIALISE PART 2 OBJECTS AND VARIABLES
int Code2 = 2;                              //CODE PART 2
string Part2 = "Bearings";               //PART BEARINGS
double Price2 = 25.50;                    //PRICE PART 2
int QuantityOnHand2 = 20;               //QUANTITY ON HAND PART 2
int reorderLevel2 = 10;                    //REORDER LEVEL PART 2

//DEFINE AND INITIALISE PART 3 OBJECTS AND VARIABLES
int Code3 = 3;                              //CODE PART 3
string Part3 = "Brake Pads";          //PART BRAKE PADS
double Price3 = 30.00;                    //PRICE PART 3
int QuantityOnHand3 = 12;               //QUANTITY ON HAND PART 3
int reorderLevel3 = 6;                    //REORDER LEVEL PART 3

cout << "\nOur current stock is: " <<endl;

//DISPLAY HEADINGS
cout.precision(2);
cout << "\n\n\n\n\n"
      << setw(20) <<"Code"
      << setw(20) <<"Part"
      << setw(20) <<"Quantity On Hand"
      << setw(20) <<"Price" << endl;

//DISPLAY CURRENT STOCK
cout << setw(20) << Code1
      << setw(20) << Part1
      << setw(20) << QuantityOnHand1
      << setw(20) << Price1 << endl;

cout << setw(20) << Code2
      << setw(20) << Part2
      << setw(20) << QuantityOnHand2
      << setw(20) << Price2 << endl;

cout << setw(20) << Code3
      << setw(20) << Part3
      << setw(20) << QuantityOnHand3
      << setw(20) << Price3 << endl;
//RETURN

}//END Display Stock
//---------------------------------------------------------------------
void GetOrderQuantities(int& AxleQty, int& BearingsQty, int& BrakeQty,
                        int AxleStock, int BearingsStock, int BrakeStock)
{
    //THIS FUNCTION OBTAINS FROM THE USER THE ITEM CODE AND
    //THE QUANTITY OF EACH ITEM THAT ARE REQUIRED BY THE USER
   
    cout << "Enter the code for the part you want to buy => ";
    cin >> Code;
    char prompt;                    //prompt for more parts
    do {
       
        cout << "Would you like to buy more parts? (y/N)";
        cin >> prompt;
       
    }while (toupper(prompt) == 'Y');     // while answer Y loop otherwise end
   
//-----------------------------------------------------------------------
}

void ReadInteger(int &Integer);

int GetIntegerResponse(     // return response entered by user
                       int Lower,               //Lower bound on valid responses
                       int Upper)               //Upper bound on valid responses
{
    int
        Response;
   
    //Get response from user; if not in the range Lower..Upper print
    //error msg and loop until Response in this range.
    do
    {
        ReadInteger(Response);
        if (Response < Lower || Response > Upper)
        {
            cerr << "n\t\tERROR: Number must be between " << Lower << " and "
                << Upper << "==> ";
        }
    }while (Response < Lower || Response > Upper);
   
    //return the user's selection
    return Response;
}

void Clear(void);                    // clear standard input to end of line;

void ReadInteger(int &Integer)     // function definition
{
    cout << "How many of this part would you like => ";
    cin >> Integer;
   
    {
        cin >> Integer;
        while (cin.fail())
        {
            cin.clear();               // clear input stream flags
            Clear();                    // clear to end of line
            cout << "ERROR:  You must enter number required ==> ";
            cin >> Integer;          // try again
        }
        Clear ();                         // clear to end of line
    }
}

void Clear(void)                    // clear standard input to end of line
{
     char Char = ' ';
     while (Char != '\n')
     {
          cin.get(Char);
     }
}

float TotalCostParts(int AxleQty, int BearingsQty, int BrakeQty);

//THIS FUNCTION CALCULATES AND RETURNS THE TOTAL COST OF THE ITEMS
//THAT HAVE BEEN ORDERED


//----------------------------------------------------------------------

void DisplayInvoice ( int CustomerId, int AxleQty, int BearingsQty,
                          int BrakeQty)
{
    //THIS FUNCTION DISPLAYS THE INVOICE DETAILS

         string Part = " ";
         int Quantity = 0;
         float Cost = 0.0;

    cout << "Invoice for Customer Number:" << CustomerId << endl;

    cout << "Part\tQuantity\tCost" <<endl;
    cout << "----\t--------\t----" <<endl;

    cout << Part << Quantity << Cost <<endl<<endl;

    cout << " Total Cost of parts: $" << totalCostOfParts <<endl;


}//END Display Invoice
//--------------------------------------------------------------------

void UpdatePartsStock(int& AxleStock, int& BearingsStock, int&
                           BrakeStock,
                           int AxleQty, int BearingsQty, int BrakeQty)
{
//THIS FUNCTION UPDATES THE QUANTITY OF EACH ELEMENT THAT REMAINS IN STOCK
//AFTER THE CUSTOMER IS SERVED


     //DECLARE LOCAL VARIABLES
     // int AxleQty,
     //      BearingsQty,
     //     BrakeQty;

     switch (Code)
     {
     case 1:     AxleQty = AxlesStock - numberRequired;
          break;

     case 2:     BearingsQty = BearingsStock - numberRequired;
          break;

     case 3:     BrakeQty = BrakeStock - numberRequired;
          break;
         
     case 4:     cout << "\nInvalid Code......Try again.";
     }//END switch
}
//--------------------------------------------------------------------

//THIS FUNCTION DETERMINES WHICH ITEMS SHOULD BE REORDERED
void Reorder (int AxlesStock, int BearingsStock, int BrakeStock)
{
    if (QuantityOnHand < reorderLevel)
    {
        cout << "The following parts should be reordered: " <<endl;
    }
    else
    {
        cout << "No parts to be reordered at present. " << endl;
       
        return;
    }
}

void main()
{
}        
//-----------------------------------------------------------------------
//END main()


Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10765539
Some of the changes i made:

1.  I moved Welcome() below to the global variables, removing a '{'
2.  Added totalCostOfParts as it was used later.
3.  Added curly brackets.
4.  Moved ReadInteger() function below the body of GetOrderQuantities
5.  Added a prototype of ReadInteger() before GetIntegerResponse()
6.  In ReadInteger:  cin << Integer;  changed to  cin >> Integer;          // try again
7.  Moved Clear() function out of other function scope
8.  Had to move some curly brackets as two (or more) functions were nested.
9.  float TotalCostParts(..)?? Didn't find any implementation. Added a ';'.
10. UpdatePartsStock():  arguments have been defined locally. Made comments.
11. Reorder(): Brackets missing.
12. main() missing;

Regards, Alex
0
 

Author Comment

by:KazIT
ID: 10765559
Thankyou all for your time and considerable effort.  I have also managed to reduce the errors to 7.  Which I am still working on.  But I will implement the changes you have advised and see how I go.
Thankyou so much nonubik and Alex. :)

Will advise my outcome. ASAP.

Kaz.
0
 

Author Comment

by:KazIT
ID: 10765759
Alex,
I have implemented your advices.
With regards to the TotalCostParts() this has to add the price column on the invoice that is output and place the sum in to cout statement "Total Cost of parts"

When  I compile it as it is there are no errors.. When I execute it the console window comes up "press any key to continue"
That has to do with....
" Ok, that compiles. However, it will not work as main() doesn't call any of the functions"?
What do I need to use to call the functions?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10765850
>> What do I need to use to call the functions?

You have to call your functions from main():

void main()
{
     Welcome();
     ReadChar();
     ...
}

However, these function have all void type. Maybe you should change to bool type, so you could find out whether the function succeeded or not.

Regards, Alex


0
 

Author Comment

by:KazIT
ID: 10773498
Alex,
The void type functions are what I am required to use.  
When I try to implement void main() I get 14 errors saying
"local function definitions are illegal"
I obviously am not understanding what you mean.

Kaz
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10773684
>> "local function definitions are illegal"

That message you get, if you are nesting two functions or mismatching curly brackets like this:

   void main()
   {
         void Welcome() //  local functions are not supported
         {
         }
   }

Here the function body of Welcome() is within the function body of main(). That's invalid. You have to do this:

   void main()
   {
   }
   void Welcome() //  now it isn't local resp. to main()
   {
   }

However to be able to call Welcome() in main() you have either to move Welcome() implementation above main() - as i had done - or you have to define a prototype of that function:


   void Welcome();   // It's a prototype only

   void main()
   {
         Welcome();   // now it can be called here because the compiler knows type and arguments
   }
   void Welcome() //  now it isn't local resp. to main()
   {
   }

>> Maybe you should change to bool type,

With that i meant to change the type of function ReadChar() from void to bool:

bool ReadChar(char &Char)
{
//THIS OUTPUT ASKS IF THERE ARE ANY CUSTOMERS AND CHECKS FOR INCORRECT INPUT.
     cout << "Are there any customers? (Y/N) => ";
     cin >> Answer;               //READ ANSWER
     Answer = toupper (Answer);     //CONVERT TO UPPERCASE

// IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
     while ((Answer != 'N') && (Answer != 'Y'));
     {
         cout << "ERROR:  Valid responses are 'Y' or 'N'" <<endl
             << "Are there any customers? (Y/N) => ";
         cin >> Answer;
         Answer = toupper (Answer);
      }//END while

         if (Answer == 'Y')
         {
             cout << "Please enter your Customer ID =>";
             cin >> CustomerId;
             return true;          // return true for 'Yes'
         }
         
         else if (Answer == 'N')
         {
             cout << "\nThankyou and Goodbye." <<endl;
             return false;        // return false for 'NO'
         }
     }

Then, you can do this:


   void main()
   {
         Welcome();   // now it can be called here because the compiler knows type and arguments

         if (ReadChar())
         {
              // go on because there are customers
         }
         // no (more) customers
   }

Note, i changed ReadChar function

  - having no arguments (the argument Char hasn't be set before)
  - ReadChar(Answer) ==> cin >> Answer;  (endless loop always calling same function else)
  - moved if statements checking 'Y/N' below the loop (wouldn't be performed if correct answer at begin)

Regards, Alex

BTW, you must check all of your functions if they use the given arguments or if global variables are set correctly. All functions where the user could make an input should have bool type and there should be a possibility to enter 'quit' or 'Q' to quit the program.
0
 

Author Comment

by:KazIT
ID: 10773788
Can you also tell me why void main() is on the 3rd last line of the code?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10773979
>> Can you also tell me why void main() is on the 3rd last line of the code?

By moving main() to the end of code, i must not define prototypes of the functions that get called from main(). You may move main() to the top of your implementation and make prototypes of all functions that must be defined before or in a separate header file, e. g.  sparepart.h . Then, you have to include this header by

     #include "sparepart.h"

Regards, Alex

0
 

Author Comment

by:KazIT
ID: 10804525
Can you tell me what is meant by a message being hard coded as part of a function please?

Kaz
0
 

Author Comment

by:KazIT
ID: 10804649
Iam still having problems here. My latest efforts are
I have Listed Function Prototypes before the main function.
declared my variables in the main function
Called my functions in the main function
ended the Main function then commenced each separate function but I am still getting errors
'GetIntegerResponse' : local function definitions are illegal
error C2084: function 'void __cdecl ReadChar(char &)' already has a body

Can anyone offer assistance please?
Thanks

Kaz
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 10805107
error C2084: function 'void __cdecl ReadChar(char &)' already has a body
'GetIntegerResponse' : local function definitions are illegal

Look at yout ReadChar function:

void ReadChar(char &Char)
{
//THIS OUTPUT ASKS IF THERE ARE ANY CUSTOMERS AND CHECKS FOR INCORRECT INPUT.
     cout << "Are there any customers? (Y/N) => ";
     ReadChar(Answer);               //READ ANSWER
     Answer = toupper (Answer);     //CONVERT TO UPPERCASE

// IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
while ((Answer != 'N') && (Answer != 'Y'));
{
          cout << "ERROR:  Valid responses are 'Y' or 'N'" <<endl;
               << "Are there any customers? (Y/N) => ";
          ReadChar(Answer);
          Answer = toupper (Answer);

if (Answer == 'Y')
{
     cout << "Please enter your Customer ID =>";
     cin >> CustomerId;
}

else if (Answer == 'N')
{
     cout << "\nThankyou and Goodbye." <<endl;
}
}//END while

//----------------------------------------------------------------------
//DEFINE VARIABLES

You see, the last closing bracket is for END WHILE and not to close ReadChar function. Because of this you get next error 'local functions are invalid...'.

You should try to indent your code properly so you easily could see where there are missing brackets. Try the following: select the whole function body and press ALT + F8. Then Visual Studio will do the indentation for you.

Regards, Alex

0
 

Author Comment

by:KazIT
ID: 10812613
Alex,
Thanks for the tip on ALT + F8.
I have altered the code again because I think I was repeating myself.
Maybe it would be easier for me if I ask questions of each function as I go along because I am not sure what to include in the Main function.

when I ask "Are there any customers?"
this is the functions that I use
//THIS OUTPUT ASKS IF THERE ARE ANY CUSTOMERS AND CHECKS FOR INCORRECT INPUT.
     char Reply = ' ';
       cout << "Are there any customers? (Y/N) => ";
     ReadChar(Reply);               //READ USER RESPONSE
     Reply = toupper(Reply);     //CONVERT TO UPPERCASE

// IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
     while ((Reply != 'N') && (Reply != 'Y'));
     {
         cout << "ERROR:  Valid responses are 'Y' or 'N'" <<endl
             << "Are there any customers? (Y/N) => ";
         ReadChar(Reply);
         Reply = toupper (Reply);
       }//END while
         
         if (Reply == 'Y')
         {
             cout << "Please enter your Customer ID =>";
             cin >> CustomerId;
             }               
         else if (Reply == 'N')
         {
             cout << "\nThankyou and Goodbye." <<endl;
             }
}
                    int GetIntegerResponse(int Lower, int Upper)
                   {
                         int Response;
                         do
                         {
                               ReadInteger(Response);
                               if (Response < Lower || Response > Upper)
                               {
                                     cerr << "\n\t\tERROR: Number must be between"
                                           << Lower << "and" << Upper << "=> ";
                               }
                         }while (Response < Lower || Response > Upper);

                         return Response;
                   }
  Is this the correct way to ask this question?
0
 

Author Comment

by:KazIT
ID: 10820596
Iam going to close this question and come back when I'm further along in my program.  Thank you Alex for all your help and advice the points are yours.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10820635
Forgot to send some comment yesterday...

The first two lines of your function have been missing:

void ReadChar(char &Char)
{
 ....

>> Is this the correct way to ask this question?


The ReadChar function has two purposes. (A) it asks if the user wants to start. (B) it asks for the customer id.

Normally a function should do only one thing. So, it would be better to have two functions here, say

 bool AskForStart()
 {
    char Reply = ' ';
    cout << "Are there any customers? (Y/N) => ";
    cin >> Reply;               //READ USER RESPONSE
    Reply = toupper(Reply);     //CONVERT TO UPPERCASE
   
    // IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
    while ((Reply != 'N') && (Reply != 'Y'));
    {
        cout << "ERROR:  Valid responses are 'Y' or 'N'" <<endl
            << "Are there any customers? (Y/N) => ";
        cin >> Reply;               //READ USER RESPONSE
        Reply = toupper (Reply);
    }//END while
   
    return (Reply == 'Y');
 }

 bool AskForCustomerID(int & customerId)
 {
    cout << "Please Enter Your Customer ID (or -1 to quit) =>";
    cin >> customerId;
   
    return (customerId != -1);
 }

Then in main() you have

 void main()
 {
    Welcome();
    if (!AskForStart())
        return 1;           // exit
    if (!AskForCustomerID(CustomerId))
        return 2;           // exit
 }

Or, you omit the first question - as it is rhetorically only - and ask for the customer id only. In any case you should  give the user a chance to quit.

Note, that i passed the global variable 'CustomerId' to function AskForCustomerID. Functions shouldn't use global variables if it isn't necessary. That applies to main() also: You may move all global variables to main() making them local to main() function. Then, the compiler tells you where you didn't pass variables as arguments to a function. If it is too much arguments, you have to create a struct or class to hold all data. Then pass a pointer or reference of that struct to functions (or change functions to member functions).


>> int GetIntegerResponse(int Lower, int Upper)

That function is ok. However, the user should have a chance to quit.

Regards, Alex

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

19 Experts available now in Live!

Get 1:1 Help Now