0

It has two issues 1). program is not taking input it exits without getting value of variable option. 2).Also my base and derived class are not initializing they are displaying garbage value.

Here is complete code.

#include<iostream>
using namespace std;


class BeverageItem
{
 protected:
    string name;
    double price;


 public:
    void set_name(string n);
    string get_name();
    void set_price(double pr);
    double get_price();


};

void BeverageItem::set_name(string n)
{
   name=n;
}
string BeverageItem::get_name()
{
  return(name);
}

void BeverageItem::set_price(double pr)
{
   price=pr;
}
double BeverageItem::get_price()
{
   return(price);

}


class HotBeverage:public BeverageItem
{

private:
    int tea_bags;
    int whiteners;


public:
    //HotBeverage(int bg,int wht);
    void set_teabags(int t_bags);
    int get_teabags();
    int  getwhiteners();
    void  set_whiteners(int wht);
    double basePrice();

    double computeTax();
    double totalCost();
    void print();
};

double HotBeverage::computeTax()
{

    return (0.16*price);
}

double HotBeverage::totalCost()
{

    return(price+computeTax());
}

 double HotBeverage::basePrice()
 {
    double pr;

    if((tea_bags==1)&& (whiteners==1))
   {

        pr=20;

   }
   else if((tea_bags>1)&& (whiteners>1))
   {

    tea_bags=tea_bags-1;
    pr=20+(5*tea_bags);

   }
   set_price(pr);
   return(pr);

 }


  void HotBeverage::set_teabags(int t_bags)
  {

      tea_bags=t_bags;
  }

  int HotBeverage::get_teabags()
  {
    return(tea_bags);
  }
  int HotBeverage::HotBeverage::getwhiteners()
  {

      return(whiteners);
  }

  void  HotBeverage::set_whiteners(int wht)
  {
    whiteners=wht;
  }
   void HotBeverage::print()
 {
     cout<<"Name: "<<name<<endl;
     cout<<"Tax:"<<computeTax()<<endl;
     cout<<"Total Cost: "<<totalCost()<<endl;
 }





class ColdBeverage:public BeverageItem
{

  private:
      int drinkSize;


  public:
   //ColdBeverage(int drinkSize);
   void setDrinkSsize(int sz);
   int getDrinkSize();
    double basePrice();
    double computeTax();
    double totalCost();
    void print();

};

 void ColdBeverage::print()
 {
     cout<<"Name: "<<name<<endl;
     cout<<"Tax:"<<computeTax()<<endl;
     cout<<"Total Cost: "<<totalCost()<<endl;
 }

void ColdBeverage::setDrinkSsize(int sz)
{
    drinkSize=sz;
}
int ColdBeverage::getDrinkSize()
{
    return(drinkSize);
}

double ColdBeverage::computeTax()
{

    return (0.16*price);
}

double ColdBeverage::totalCost()
{

    return(price+computeTax());
}



double ColdBeverage::basePrice()
{
    double pr;
    double regularPr=30;
    switch(drinkSize)
    {
    case 1:  //regular,
         pr=regularPr;
        break;
    case 2:  //large.
         price=1.5*regularPr;
        break;
    case 3: //extra large.
         price=2*regularPr;
        break;

    }
    set_price(pr);
    return(pr);
}

int main()
{
    string name;
    int option;
    cout<<"Enter The Beverage Name=";
    cin>>name;




        cout<<"1. For Hot Beverage\n\n";
        cout<<"2. For Cold Beverage\n\n";
        cout<<"Select Your Choice(1,2)=";
        cin>>option;


        BeverageItem bi;
        bi.set_name(name);

        **//some other code here.**



    return 0;
}
Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22
  • 4
    [Read up on variable scope](http://en.cppreference.com/w/cpp/language/scope) to understand why what you are doing is wrong. Then crank up the warning level in your compiler and fix the warnings. – user4581301 Jul 18 '17 at 02:06
  • 1
    Also a good time to invest in learning [how to use a debugger](https://en.wikipedia.org/wiki/Debugger). – user4581301 Jul 18 '17 at 02:07
  • 2
    That's a cultural bias, man. Ninjas used a variety of weapons and non-weapons. Regardless, Go pirates! – user4581301 Jul 18 '17 at 02:09
  • I am aware of variable scope and I believe i am using it correctly.if you people can go through the code and pin point me the problem i can fix it that would be great – Husrat Mehmood Jul 18 '17 at 02:12
  • 2
    in `HotBeverage::basePrice()` you define `int tea_bags;` which hides the `tea_bags` member variable. The locally scoped `tea_bags` is never initialized when it is used, possibly resulting in problem 2. Your compiler should be telling you this. – user4581301 Jul 18 '17 at 02:18
  • An aside: I wouldn't expect `double HotBeverage::basePrice()` to _set_ anything (as it does with `set_price(pr);`). I would however expect your `set_*` functions to set stuff ... – txtechhelp Jul 18 '17 at 02:21
  • @user4581301--------- you are right I incidentally typed those variables in there. but first input issue is still there program exit with taking input of a variable option.-------thank you. int option inside the main function. – Husrat Mehmood Jul 18 '17 at 02:24
  • I have reduced the amount of code as well. – Husrat Mehmood Jul 18 '17 at 02:34
  • I have no warnings displayed when I compile. anyone who can help – Husrat Mehmood Jul 18 '17 at 02:37
  • Issues Fixed. I was not calling basePrice() method in in print() before computing Tax and TotalCost. Thank you every one. – Husrat Mehmood Jul 18 '17 at 03:18

2 Answers2

0

1) I don't really see a problem with the compiler i tried with http://cpp.sh/4p5mw

2) POD's in member variables are not default initialised. https://stackoverflow.com/a/15212447/4669663

Randolf
  • 125
  • 1
  • 9
0

Here was the issue

cin>>name;

cin stops when whitespaces are entered in a string so I would have to use

getline(cin,name);

Second issue was because i was not calling the basePrice() before print() thanks to everyone

Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22