Thread: HELP!!! Calculation problem!

  1. #1
    Unregistered
    Guest

    Question HELP!!! Calculation problem!

    I have completed my vending machine program but returning change is not working properly. What am I doing wrong. Also when you deposit not enough change it is suppose to start over.

    Here is the main, classes, and header files.

    //main.cpp
    #include <iostream.h>
    #include <stdlib.h>
    #include "candy.h"

    void main()
    {
    int sel;
    Candy c(4,50);
    Chip ch(6,60);
    Gum g(8,70);
    Cookies co(10,80);
    double pr;

    do
    {
    // system ("cls");
    cout<<endl;
    cout<<"*** Welcome to CIS-270's Candy Shop ***"<<endl
    <<"To select an item enter"<<endl
    <<"1. for Candy"<<endl
    <<"2. for Chips"<<endl
    <<"3. for Gum"<<endl
    <<"4. for Cookies"<<endl
    <<"9. to exit"<<endl<<endl;

    cin>>sel;

    while ((sel !=1) && (sel !=2) && (sel !=3)
    && (sel !=4) && (sel !=9))
    {
    cout << "You have made an invalid selection."
    << "Please enter an item."
    <<endl;
    cin>>sel;
    }
    switch(sel)
    {
    case 1:
    {
    if (c.GetStock() <1)
    {
    cout << "Out of Stock. Please try other goods"
    << endl;
    break;
    }
    else
    {
    cout <<"Please deposit:" << c.GetPrice() << "cents"
    <<endl;
    cin >> pr;
    if (pr = c.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy."
    << endl;
    c.SetStock(c.GetStock()-1);
    }
    else if (pr > c.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy.\n"
    << "Your changes is:" << pr - c.GetPrice()
    << endl;
    c.SetStock(c.GetStock()-1);
    }
    else
    cout << "Please deposit again" << endl;
    break;
    }
    }
    case 2:
    {
    if (ch.GetStock() <1)
    {
    cout << "Out of Stock. Please try other goods"
    << endl;
    break;
    }
    else
    {
    cout <<"Please deposit:" << ch.GetPrice() << "cents"<<endl;
    cin >> pr;
    if (pr = ch.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy."
    << endl;
    ch.SetStock(ch.GetStock()-1);
    }
    else if (pr > ch.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy.\n"
    << "Your changes is:" << pr - ch.GetPrice()
    << endl;
    ch.SetStock(ch.GetStock()-1);
    }
    else
    cout << "Please deposit again" << endl;
    break;
    }
    }
    case 3:
    {
    if (g.GetStock() <1)
    {
    cout << "Out of Stock. Please try other goods"
    << endl;
    break;
    }
    else
    {
    cout <<"Please deposit:" << g.GetPrice() << "cents"<<endl;
    cin >> pr;
    if (pr = g.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy."
    << endl;
    g.SetStock(g.GetStock()-1);
    }
    else if (pr > g.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy.\n"
    << "Your changes is:" << pr - g.GetPrice()
    << endl;
    g.SetStock(g.GetStock()-1);
    }
    else
    cout << "Please deposit again" << endl;
    break;
    }
    }
    case 4:
    {
    if (co.GetStock() <1)
    {
    cout << "Out of Stock. Please try other goods"
    << endl;
    break;
    }
    else
    {
    cout <<"Please deposit:" << co.GetPrice() << "cents"<<endl;
    cin >> pr;
    if (pr = co.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy."
    << endl;
    co.SetStock(co.GetStock()-1);
    }
    else if (pr > co.GetPrice())
    {
    cout << "Collect your item at the bottom and Enjoy.\n"
    << "Your changes is:" << pr - co.GetPrice()
    << endl;
    co.SetStock(co.GetStock()-1);
    }
    else
    cout << "Please deposit again" << endl;
    break;
    }
    }
    case 9:
    {
    cout<<"Goodbye"<<endl;
    break;
    }
    }
    }while(sel !=9);
    }

    //candy.cpp
    #include <iostream.h>
    #include "candy.h"

    Candy::Candy(int i, double d)
    {
    price = d;
    stock = i;
    }

    Chip::Chip(int i, double d)
    {
    price = d;
    stock = i;
    }

    Gum::Gum(int i, double d)
    {
    price = d;
    stock = i;
    }

    Cookies::Cookies(int i, double d)
    {
    price = d;
    stock = i;
    }

    void Candy::SetStock(int i)
    {
    stock = i;
    }

    int Candy::GetStock()
    {
    return stock;
    }

    double Candy::GetPrice()
    {
    return price;
    }

    void Chip::SetStock(int i)
    {
    stock = i;
    }

    int Chip::GetStock()
    {
    return stock;
    }

    double Chip::GetPrice()
    {
    return price;
    }

    void Gum::SetStock(int i)
    {
    stock = i;
    }

    int Gum::GetStock()
    {
    return stock;
    }

    double Gum::GetPrice()
    {
    return price;
    }

    void Cookies::SetStock(int i)
    {
    stock = i;
    }

    int Cookies::GetStock()
    {
    return stock;
    }

    double Cookies::GetPrice()
    {
    return price;
    }

    //candy.h
    class Candy
    {
    private:
    int stock;
    double price;
    public:
    Candy(int,double);
    int GetStock();
    void SetStock(int);
    double GetPrice();
    };

    class Chip
    {
    private:
    int stock;
    double price;
    public:
    Chip(int,double);
    void SetStock(int);
    int GetStock();
    double GetPrice();
    };

    class Gum
    {
    private:
    int stock;
    double price;
    public:
    Gum(int,double);
    int GetStock();
    void SetStock(int);
    double GetPrice();
    };

    class Cookies
    {
    private:
    int stock;
    double price;
    public:
    Cookies(int,double);
    int GetStock();
    void SetStock(int);
    double GetPrice();
    };

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    If you want to compare use the == operator and not the = operator:
    Code:
    if (pr = c.GetPrice())
    Change it to:
    Code:
    if (pr == c.GetPrice())

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    and please use code tags!

  4. #4
    Unregistered
    Guest
    Thanks!!!
    ......and sorry about not putting any tags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Calculation problem....
    By badran in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2006, 04:07 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Trigonometry Sin rule calculation problem
    By Harry in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2006, 12:14 PM