Solved

hi...some help with c++..

Posted on 2001-06-12
9
276 Views
Last Modified: 2010-04-02
hi...
     i just started working with c++..i need to make some changes to existing code in my compnay but i am not able to understande some things in the code i will really really appreciate if you people could help me out..thanks

i want to know :
1)   what does #define boolean short signifies...what is it is used for in the programme.
2) in programme we are using customerName = inputData("customerName", true);
why are we pasing true as perameter ...what is its use ...i think we can do without it ..but i can't remove it..so i would appreciate ..if you could explain to me its functionality ..what is it used for in the prograame it is used on more then one places....in some places with false vale.
******* code*********

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <ctype.h>
#include <cstring.h>

#define stockfile "stock.dat"      // describing files
#define orderfile "orders.dat"
#define boolean short              // to express boolean
#define true 1
#define false 0


struct Order
{
      string customerName;
      string customerAddress;
      string customerTelephone;
      int numberofChairs;
      int numberofTables;
};

struct Stock
{
      int numberofChairs;
      int numberofTables;
};


Stock stockLeft();
int submitedOrder(Order database[]);
void displayMenuoption();
Order addOrder(int chairBalance, int tableBalance);
void displayStock(int chairBalance, int tableBalance);
void displayCurrentOrder(Order order, int chairPrice, int tablePrice);
void displayAllOrders(Order database[], int currentOrder);
string modify();
void exit(Stock stock, Order database[], int currentOrder);
string inputData(string fieldRequired, boolean alphaField);
int getStock(int stock, string type);
string getString();
int getIntValue(string value);
int getChoice();
void getPause();
boolean orderConfirmed(Order order, int chairPrice, int tablePrice, int &submitted);

int main()
{
  const int MAX = 9;
  Order database[MAX+1];
  Order torder;
  Stock stock;
  string customerName;
  const int chairPrice = 25;
  const int tablePrice = 65;
  int currentOrder = 0;
  int &orderSubmitted = 1;                  // for validation
  int choice = 0;

  for (int i = 0; i < MAX; i++)
  database[i].customerName = "";
  stock = stockLeft();
  currentOrder = submitedOrder(database);
  cout<<currentOrder;
  while(choice != 7)
  {
            clrscr();
      cout <<"\n\t\t" << "***********************************"
           <<"\n\t\t\t" << currentOrder
           << "orders/order submited";
      displayMenuoption();
      choice = getChoice();
            switch(choice)
            {
                  case 1:          displayStock(stock.numberofChairs, stock.numberofTables);
                                       break;
            //  create new order
                  case 2:       if (!orderSubmitted)
                     {
                                          cout << "\n****Valid order exits...\n****";
                                          getPause();
                                          break;
                                       }
                                       if (currentOrder == MAX)
                     {
                                        cout << "\n****DataBase excepts only 10 orders****\n";
                                        getPause();
                                        break;
                                    }
                     clrscr();
                     customerName = inputData("customerName", true);
                                       orderSubmitted = 0;
                                       torder = addOrder(stock.numberofChairs, stock.numberofTables);
                                       torder.customerName = customerName;
                                       database[currentOrder] = torder;
                                       break;
            // Modify order
                  case 3:  if(database[currentOrder].customerName != "")
                     {
                                          clrscr();
                                          displayCurrentOrder(database[currentOrder],chairPrice,tablePrice);
                                           getPause();
                                          database[currentOrder].numberofTables = getStock(stock.numberofTables, "table");
                                          database[currentOrder].numberofChairs = getStock(stock.numberofChairs, "chair");
                                       }
                                    else {
                                          cout << " \n*****No current order exits*****\n";
                                          getPause();
                                    }
                                    break;
            // display current order details
            case 4:  if(database[currentOrder].customerName != "")  {
                                          clrscr();
                                          displayCurrentOrder(database[currentOrder], chairPrice, tablePrice);
                                          getPause();
                                    }
                                    else {
                                           cout << "\n****Please create an order first.****\n";
                                           getPause();
                                         }
                                    break;
            // display all orders
                     case 5:  displayAllOrders(database, currentOrder);
                                    break;
            // submit current order
                     case 6:  if (database[currentOrder].customerName != "")
                            {
                                          clrscr();
                                          if (orderConfirmed(database[currentOrder], chairPrice,tablePrice,
                                                                         orderSubmitted))
                                {
                                            stock.numberofTables -= database[currentOrder].numberofTables;
                                                stock.numberofChairs -= database[currentOrder].numberofChairs;
                                                currentOrder++;
                                              }

                                        }
                                    else {
                                           cout << "\n*****No current order exits*****\n";
                                           getPause();
                                    }
                                    break;

            case 7:              exit(stock, database, currentOrder);
                                    break;

                  default :      cout << "\n****Invalid Entry****\n";
                                     getPause();
                                     break;
             }
  }
  return 0;
}


void displayMenuoption()
{
   cout << "\n\t\t***********************************";
   cout << "\n\t\tWelcome to No-Frill Furniture Store";
   cout << "\n\t\t***********************************";
   cout << "\n\t\t\t Main Menu";
   cout << "\n\t\t1. Display Stock";
   cout << "\n\t\t2. Create Order";
   cout << "\n\t\t3. Modify Order";
   cout << "\n\t\t4. Display Order";
   cout << "\n\t\t5. Display All Orders";
   cout << "\n\t\t6. Submit Order";
   cout << "\n\t\t***********************************";
}

int getChoice()
{
   cout << "\n\n\t\tPlease enter your option here:[press 7 to exit] ";
   string option;
   cin >> option;
   option.substr(0,1);
   int choice =  atoi(option.c_str());
   return choice;
}

Order addOrder(int chairBalance, int tableBalance)
{
   const boolean alphaField = 1;
      Order order;
      order.customerAddress = inputData("Address", alphaField);
      order.customerTelephone = inputData("Telephone number", !alphaField);
      order.numberofTables = getStock(tableBalance, "table");
      order.numberofChairs = getStock(chairBalance, "chair");
      clrscr();
      return order;
}


Stock stockLeft()
{
      ifstream stockFile;
      Stock stock;

      stockFile.open(stockfile);
      if (stockFile.fail())
            cout << "\n****Cannot open file.****\n";
      else {
            stockFile >> stock.numberofChairs >> stock.numberofTables;
            stockFile.close();
      }
      return stock;
}

int submitedOrder(Order database[])
{
      int i = 0;
      char line[80];

      ifstream orderFile;
      orderFile.open(orderfile);
      if (orderFile.fail())
            cout << "\n****Cannot open file.****\n";
      else {
            while (!orderFile.eof()) {
                  orderFile.getline(line, 80);
                  database[i].customerName = line;
                  orderFile.getline(line, 80);
                  database[i].customerAddress = line;
                  orderFile.getline(line, 80);
                  database[i].customerTelephone = line;
                  orderFile >> database[i].numberofChairs
                                     >> database[i].numberofTables;
               orderFile.get();
                  i++;
            }
      }
   i--;
      orderFile.close();
      return i;
}

void displayStock(int chairBalance, int tableBalance)
{
  cout << "\nAvailable numberofTables : " << tableBalance;
  cout << "\nAvailable numberofChairs : " << chairBalance << "\n";
  getPause();
}


string inputData(string fieldRequired, boolean alphaField)
{
        boolean formatOK;
      string field;
             cout << "\nInput your " << fieldRequired << " : ";
            field = getString();
      return field;
}


int getStock(int stock, string type)
{
      string quantity;
      int order;
      do
      {
            cout << "\nPlace " << type << " order : ";
            cin >> quantity;
            order = getIntValue(quantity);
            if(order <= 0)
                  cout << "\n\nInvalid entry.\n";
            else if(order > stock)
                  cout << "\n\nIn excess of Stock avaliable.\n";
      }while(order <= 0 || order > stock );
      return order;
}

void displayCurrentOrder(Order order, int chairPrice, int tablePrice)
{
      int numberofChairsCost;
      int numberofTablesCost;
      numberofChairsCost = (chairPrice * order.numberofChairs);
      numberofTablesCost = (tablePrice * order.numberofTables);
      cout << "\n\t***********************************************";
      cout << "\n\t\tYour Order \n";
      cout << "\n\t***********************************************";
      cout << "\n\tClient customerName : " << order.customerName;
      cout << "\n\n\tOrder:\t\tDetails"
              << "\n\t**********************************************"
              << "\n\tTable Order : "  << order.numberofTables  << " Rate : $" << tablePrice <<"\t"
              << numberofTablesCost
              << "\n\tChair Order : "  << order.numberofChairs  << " Rate : $" << chairPrice <<"\t"
              << numberofChairsCost << "\n"
              << "\t*************************************************";


      cout << "\n\tTotal Cost : \t" << "$" << (numberofTablesCost + numberofChairsCost);
}

void displayAllOrders(Order database[], int currentOrder)
{
      for (int i = 0; i < currentOrder; i++)
        {
        clrscr();
        cout << "\n\n\t\t*********order\t" << (i+1) << "************"
             << "\n\t\tcustomerName:\t" << database[i].customerName
             << "\n\t\tnumberofChairs:\t" << database[i].numberofChairs
             << "\n\t\tnumberofTables:\t" << database[i].numberofTables;
        getPause();
        }
}

void exit(Stock stock, Order database[], int currentOrder)
{
      ofstream stockFile, orderFile;

      stockFile.open(stockfile);
      if (stockFile.fail()) {
            cout << "\nError Occured whle creating file.\n";
            }
      else
            stockFile << stock.numberofChairs << "\n" << stock.numberofTables << "\n";
      stockFile.close();
      orderFile.open(orderfile);
      if (orderFile.fail()) {
            cout << "\nCannot create orders file.\n";
            cout << "Orders not written to file.\n";
      }
      else {
            for (int i = 0; i <  currentOrder; i++) {
                  orderFile << database[i].customerName << "\n"
                                     << database[i].customerAddress << "\n"
                                     << database[i].customerTelephone << "\n"
                                     << database[i].numberofChairs << "\n"
                                     << database[i].numberofTables << "\n";
            }
            orderFile.close();
      cout << "\nOrder file updated\n";
      }
   cout << "\nThank you for using the Software.\n"
        <<  "have a good day!";
      getch();
}

boolean orderConfirmed(Order order, int chairPrice, int tablePrice, int &submitted)
{
      string option;
      submitted = true;
      displayCurrentOrder(order, chairPrice,tablePrice);
      cout << "\n\n\nDo you want to submit this order ? [ Y / N ]";
      cin >> option;
      option = option.substr(0,1);
      if (option == 'Y' || option == 'y')
            return true;
      return false;
}

int getIntValue(string value)
{
      value = value.substr(0,5);
      int number = atoi(value.c_str());
      return number;
}

// Builds up string and Allows a null entry to be detected
string getString()
{
      string str = "";
      char temp;
      while((temp = getche()) != '\r')
            str += temp;
      return str;
}

void getPause()
{
   cout << "\n\n\n****press any key to continue**** : ";
      getch();
}
 
i want to know :
1)   what does #define boolean short signifies...what is it is used for in the programme.
2) in programme we are using customerName = inputData("customerName", true);
why are we pasing true as perameter ...what is its use ...i think we can do without it ..but i can't remove it..so i would appreciate ..if you could explain to me its functionality ..what is it used for in the prograame it is used on more then one places....in some places with false vale.
thank you for your help in advance.
cheers
0
Comment
Question by:sati001
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 2

Accepted Solution

by:
MadYugoslav earned 100 total points
ID: 6180809
1. #define boolean short
   In this place #define is same as typedef. On every place where You use boolean it will be the type short. In the other words You use short as boolean type.

2. As I see in function inputData second parameter (alphaField) is not used. Which mean for now it is equal behaviour of function with parameter value true and false.
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6180820
#define expressionA expressionB

the preprocessor replaces all expressionA with expressionB, so in your case all 'boolean' will be replaced with 'short'. (they use short-values to express boolean, thats all)
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6180843
I think that already sad that.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Expert Comment

by:missionImpossible
ID: 6180858
right , just an explorer update problem.
0
 

Author Comment

by:sati001
ID: 6181878
thanks ...but see the thing is do i need to do that ...i tried to remove those lines...the programme runs fine..but when i create a project,easywin with the same code....it shows all sorts of errors basically saing boolean not defined.....(i am using borland 5)..WHY??????

about inputData second parameter (alphaField) if it is not used ....i am in BIG trouble :) ..why to give second perameters...how could it be used.....with this function i am just tring to understand logic of previous coder.....why he put another perameter if not to use..i am not basically a C++ programmer..just know very very basic and tring to learn  a bit with this small assignment  
thanks for the help... i appreciate..
cheers
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6182401
Just a small point - in C++ there is a bool data type.  I think that using it would have been better than creating a pretend Boolean which is actually a short int.

In answer to your second question, the 'true' is not doing anything at all.  Just look at the code:

string inputData(string fieldRequired, boolean alphaField)
{
      boolean formatOK;
     string field;
          cout << "\nInput your " << fieldRequired << " : ";
          field = getString();
     return field;
}


alphaField is _not_ being used anywhere.  My guess would be that it used to do something, but the previous programmer decided that it was redundant but was too lazy to remove the second parameter.  You will also notice that the formatOK boolean is redundant.

Good luck with the work :)
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6182424
> [if alphaField is not used] i am in BIG trouble :)

Why?

Also, is your company really called "No Frills Furniture Store", and your software really exits with "Thank you for using the software.  Have a good Day!"?  Bad luck :)
0
 

Author Comment

by:sati001
ID: 6184153
no this software is not used ...
told you it is a SMALL ASSIGNMENT..it is not used..as you can tell this programme is not good enought to be used..but i need to make changes to prove something ..to my employer..WHAT ?? don't have a clue and more over the programme is not that difficult so i thought what the hack.....i think it will take me another 10 year to make a programme in c++..;-)
till then i am happy making web sites ..
cheers
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6184872
I think that there are answers about Your question.
Is there anything specific that You will ask or You will end this question ?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

786 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