this is my homework , and i have some code but please can you help me to fixed my homework please....

The purpose of this exercise is to give you practice with abstract data types, namely structrures 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
this is the code that i do it

// this program demostrate the data enumerated data type.
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
 enum Drinks { COCA COLA = 0.75 , ROOT BEER = 0.75 , SPRITE = 0.75, SPRING WATER = 0.80, APPLE JUICE = 0.80 };   
 // function prototype
 void displaydrinksName(drinks);


 const int NUM_DRINKS = 5;  // the number of drinks
 double sales[NUM_DRINKS];  // hold the sales for each one
 double total = 0.0;        // accumulator
 drinks workDrink;          // loop counter

//Get the sales for each drink.
for (workdrinks = Coca-Cola; workdrinks <= Spring Water;
                             workdrinks = static_cast < drinks > (workdrinks + 1))
{
cout << " pick a drink for you ";
displayeddrinkName(workdrinks);
cout << ":";
cin >> sales[workdrinks];
}

// calculate the total sales.
for (workdrinks = Coca-Cola; workdrinks <= Spring Water;
                             workdrinks = static_cast < drinks > (workdrinks + 1))
 total += sales[workdrinks];

 // display the total
 cout << "your total is $" <<setprecision(2)
 <<fixed <<total <<endl;

 return 0;
}


 DrinkTypes drinks;  // drinks is a drinkTypes structures   

    //Display the list of drinks.
    cout << "Here is the drinks data:\n";
    cout << "Name: " << 



    // Get a drink.
    cout << "Pick a drink: ";
    cin.ignore();   //skip the remaining '\n' character.
    cin.getline(drink.name,SIZE);

    //Get the amount in the number of machine.

    cout << " Enter the amount in the machine : ";


    // Display the amount of change 



 void displaydrinksName(drinks d)
 {
      switch(d)
 {
case Coca-Cola : cout << "Coca-Cola";
                  break;
case Root Beer : cout << "Root Beer";
                  break;
case Sprite    : cout << "Sprite";
                  break;
case Spring Water : cout << "Spring Water";
                  break;
case Apple Juice : cout << "Apple Juice";
                  break;  
}
}                                              



    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 2 Replies

Please don't hijack threads. Start a new thread for your question, and use code tags for your code.

OK, first off, you should use [ code ] tags when you're posting code. This helps people to see what you're doing.

Right, the question says that

The program should use a structure that stores the following data:

Drink Name
Drink Cost
Number of Drinks in Machine"

to me, this sounds like something like:

struct Drink{
   string name;
   double cost;
   int number;
};

You can make a C-style array of these structures, with an element for each kind of drink. I think this is sort of where you were going with the statement

enum Drinks { COCA COLA = 0.75 , ROOT BEER = 0.75 , SPRITE = 0.75, SPRING WATER = 0.80, APPLE JUICE = 0.80 };

However, there are two main problems with doing it this way: firstly, you have no way to conveniently store the number of drinks left this way and, secondly, it is not possible to use an enum in this way, with names that have spaces in such as COCA COLA . This will definitely cause a compiler error.

If I were you, I'd re-write your code from the point of view of storing the drinks in an array of structures like the one above. Also, you seem to have tried to give the function prototype and definition for the function displaydrinksName() inside the main() function. You should put these outside the main() function. I use something like:

#include <yourFavouriteHeadersHere>

using namespace std;

int myFirstFunction(int, string, MyClass);
void myPrintFunction();

int main()
{
   /* Actual code here */
   return 0;
}

int myFirstFunction(int, string, MyClass)
{
   /* make function do things here */
}

void myPrintFunction()
{
   /* make this function do things here */
}

There are a number of other little errors, but we can pick those up when you try to compile your code.

A final point is that C and C++ are case sensitive so you have to be careful that you keep a watch of what you have in capital and lower case letters. For instance you try to define the enum with one value as SPRING WATER (which will fail as I mentioned) but then try and use this in a switch... case statement as Spring Water . This would have failed due to the capitalisation, even if the variable name had been a valid one. I find it's best to use a convention when you make variable, class, structure and function names. I make variable names look like myVariable , with the first letter of the word in lower case and then upper case letters for the start of all the subsequent words, like myLongerVariableName . I do the same thing for function names and for classes and structures I use an upper case letter for the first work as well, so MyClass . This way I can tell if something is a normal variable, a function or a class quickly and consistently. I think that some system close to this might be fairly standard in C/C++ programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.