Thread: C++ hw need help..

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    C++ hw need help..

    I cant figure out how to fix the loop for the program to work correctly and for it to display right..Please
    help..

    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

    Code:
    #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(17)<<left<<d[i].name<<"\t";
                cout<<setprecision(2)<<fixed<<d[i].cost<…
            }
            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;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your init loop could be simplified to
    infile >> name >> cost >> stock;

    Also, using eof as a loop condition is bad. eof returns true only when an attempt to read past the end of the file has occurred. That is to say, eof will return false after having read the last line, and will return true on the next loop when trying to read a non-existing line.
    See FAQ for more details.

    Avoid passing around arrays, since they are vulnerable to buffer overruns. Suggest you use std::array or std::vector if allowed.

    You haven't told us what the actual problem is either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed