8 Replies - 23495 Views - Last Post: 04 December 2011 - 10:46 PM Rate Topic: -----

#1 eblanco1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-December 11

Drink Machine Simulator Program Problem

Posted 03 December 2011 - 06:29 PM

Drink Machine Simulator

The purpose of this exercise is to give you practice with abstract data types, namely structures and arrays of structures.Write a program that simulates a soft drink machine. The program should use a structure that stores the following data:

Drink Name
Drink Cost
Number of Drinks in Machine

The program should create an array of five structures. The elements should be initialized with the following data:

Drink Name Cost Number in Machine
Coca-Cola .75 20
Root Beer .75 20
Sprite .75 20
Spring Water .80 20
Apple Juice .80 20

Please see the input file ("DrinkMachineInventory.txt"). Each time the program runs, it should read the data from the input file and then enter a loop that performs the following steps: A list of drinks is displayed on the screen. The user should be allowed to either quit the program or pick a drink. If the user selects a drink, he or she will next enter the amount of money that is to be inserted into the drink machine. The program should display the amount of change that would be returned and subtract one from the number of that drink left in the machine. If the user selects a drink that has sold out, a message should be displayed. The loop then repeats. When the user chooses to quit the program it should display the total amount of money the machine earned.
Input Validation: Only accept positive values for the amount of money. Also, do not accept values greater than $1.00.
Your code should be modular (use functions) and pass variables (by value/by reference) where appropriate. Also, use named constants where appropriate

DrinkMachineInventory.txt-
Coca-Cola 0.75 20
Root Beer 0.75 20
Sprite 0.75 20
Spring Water 0.80 20
Apple Juice 0.95 20

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;

struct Machine
{
    string name;
    double cost;
    int num;
};

void init(Machine []);
int menu(Machine[]); 
void payment(double);

int main()
{
    Machine drink[5];
    int choice;
    double made=0;
    init(drink);
    choice=menu(drink);
    while(choice!=5)
    {
        payment(drink[choice].cost);
        made+=drink[choice].cost;
        drink[choice].num--;
        choice=menu(drink);  
    }
    cout<<"Today the machine has made $"<<setprecision(2)<<fixed<<made<<endl;
    system("pause");
    return 0;
}

void payment(double p)
{
    double pay;
    cout<<"Your drink costs $"<<setprecision(2)<<fixed<<p<<endl;
    cout<<"Enter payment: ";
    cin>>pay;
    while(pay<0||pay>1.||pay<p)
    {
        cout<<"please insert the correct amount for your drink!\n";
        cout<<"maximum payment is $1.00\n";
        cout<<"Enter payment: ";
        cin>>pay;
    }
    cout<<"Your change is: $"<<setprecision(2)<<fixed<<pay-p<<endl;
    return;
}

void init(Machine d[])
{
    ifstream infile("DrinkMachineInventory.txt");

    if(infile.fail())
    {
        cout << "Could not find the file DrinkMachineInventory.txt \n";
        cout << "Exiting the program\n";
        exit(0);
    }

    int i=0;
    char ch;
    string word= "";

    while(!infile.eof())
    {
        word= "";
        ch = infile.get();
        while(true)
        {
            if(isdigit(ch) || ch == '\n')
                break;
            else
                word += ch;
            ch = infile.get();
        }

        if(word != "")
        {
            d[i].name = word;
            infile >> d[i].cost >> d[i].num ;
            i++;
        }
    }

    infile.close();   
}

int menu(Machine d[])
{
    int choice=8,i;
    bool soldout=true;
    while((choice<1||choice>6)||soldout)
    {
        soldout=false;
        cout<<"Menu\n";
        cout<<"      Drink      Cost\tleft\n";
        for(i=0;i<5;i++)
        {
            cout<<i+1<<". "<<setw(15)<<left<<d[i].name<<setw(5);
            cout<<setprecision(2)<<fixed<<d[i].cost<<"\t"<<d[i].num<<endl;
        }
        cout<<"6. Exit\n";
        cout<<"Enter Choice ";
        cin>>choice;
        if(choice<1||choice>6)
            cout<<"invalid entry\n";
        else
            if(d[choice-1].num==0)
            {cout<<"sold out\n";
        soldout=true;
        }
    }
    return choice-1;
}


My problem is the enter time i enter anything else but a number the program crashes.

Is This A Good Question/Topic? 0
  • +

Replies To: Drink Machine Simulator Program Problem

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 10:02 AM

Your problem is at line 110 where you get input for choice from cin using the extraction operator >>.

choice is an int, so this operation will succeed only if the user's entry is (or begins with) an integer. If not, the extraction operation fails, nothing is assigned to choice (it's value remains whatever it was before this operation), and the invalid character remains in the input stream.

Now choice still has the value (8) which you assigned it at the beginning of the menu function, so the while loop repeats, only this time when it gets to the lines
110 cout<<"Enter Choice ";

111 cin>>choice;

the cin.fail flag is set and the same invalid character is still in the stream so it doesn't even wait for another entry. It just skips over that operation, leaves the value of choice unchanged, and the while loop repeats -- infinitely.

To avoid this you can test the status of the cin.fail flag after the input operation, and if it has failed clear the flag and remove whatever is left in the stream. One way to do that is:

        cout<<"Enter Choice ";
        cin>>choice;
        if(cin.fail()) {
            cin.clear();   // This line just clears the "fail" flag & makes cin usable again.  It doesn't empty the stream.
            while(cin.get() != '\n') { } ;   // This line empties the stream.
        }
        // continue with the rest of your code ...


That empty while loop on line 5 just keeps extracting (and discarding) characters from cin until it finds a newline (enter) character, so it will clear out the invalid character (and anything else following it) that the user entered.
Was This Post Helpful? 2
  • +
  • -

#3 eblanco1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-December 11

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 02:26 PM

Thank you!, I didnt think it was that simple. I have another question for you. Once you run the program you will see the number 3 Sprite is not alined with rest..I know it has to do with loop for the infile but Ive done everything I could think of but it always comes back to the same program. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 03:26 PM

I don't see that. When I run it i get this:

      Drink      Cost	left
1. Coca-Cola      0.75 	20
2. Root Beer      0.75 	20
3. Sprite         0.75 	20
4. Spring Water   0.80 	20
5. Apple Juice    0.95 	20
6. Exit
Enter Choice 



Was This Post Helpful? 0
  • +
  • -

#5 eblanco1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-December 11

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 03:47 PM

I'm using Dev C++ Bloodshed..

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 03:58 PM

Well, Dev-C++ is hopelessly outdated & you should replace it with CodeBlocks or Microsoft Visual C++, but I don't want to get started on a rant ...

That's probably just something messed up in Dev-C++'s terminal window.
Why don't you try opening a Windows Terminal/Command window, cd to the directory where your program's .exe file is located (if you can find it) and run the program directly there & see what you get.
Was This Post Helpful? 0
  • +
  • -

#7 eblanco1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-December 11

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 04:32 PM

i've tried the codeblocks..same problem. I dont know how to use the windows terminal. But thanks you.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 04:49 PM

Hmmm...
check your input file & make sure there's just a single space, and not a <tab>, after "Sprite".
Was This Post Helpful? 0
  • +
  • -

#9 eblanco1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-December 11

Re: Drink Machine Simulator Program Problem

Posted 04 December 2011 - 10:46 PM

YES! it worked thanks for all your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1