TAGS :Viewed: 10 - Published at: a few seconds ago

[ How do I pass these values correctly? ]

I am supposed to use a structure to store the drink name, drink cost, and number of drinks in machine. I shoul create an array of five structures with the elements initialized with the names, costs, and number in there. The program should display a list of drinks and the user should make their selection(1-6) in the first function. The selection is validated and passed back to the main routine by value. In the second function, the user inserts money and the amount of change is displayed and one should be subtracted from the number of that drink in the machine and it loops. When the user quits, it displays the total amount the machine earned. My problem is with passing the array to the functions. I thought I did it right but I am getting errors everywhere with the functions and array. Does anyone know how i would pass the array and return the values from the function? Thanks

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


struct Drink
{
string drinkName;
double cost;
int numberInMachine;
};

struct Drink options[] = {{"Cola", .75, 0}, {"Root Beer", .75, 2}, 
       {"Lemon-Lime", .75,     10},
                      {"Grape Soda", .80, 3}, {"Cream Soda", .80, 20}};

int getChoice(Drink, int);
void showTransaction(Drink&);

int main()
{
const int NUM_DRINKS = 5; // Number of drink options
Drink options[NUM_DRINKS];

getChoice(Drink, value);
showTransaction(choice);


system("pause");
return 0;
}

int getChoice(Drink, choice)
{
int choice;

cout << "Enter the number(1-6) of the drink you would like: " << endl;
cout << "Drink Name         Cost        Number in Machine " << endl; 
cout << "1. Cola            .75             " << endl;
cout << "2. Root Beer       .75             " << endl;
cout << "3. Lemon-lime      .75             " << endl;
cout << "4. Grape Soda      .80             " << endl;
cout << "5. Cream Soda      .80             " << endl;
cout << "6. Quit " << endl;

cout << " Enter the number of your selection: ";
cin >> choice;

while(choice != 1 && choice != 2 && choice != 3 && choice != 4 
          && choice != 5 &&     choice != 6)
{
    cout << "Please enter a valid number 1-6" << endl;
    cin >> choice;
}

return choice;
}

void showTransaction(choice)
{
double moneyIn;
double moneyOut;

if(choice ==1)
{
    cout << "Enter money inserted up to $1.00: ";
    cin >> moneyIn;
    while(moneyIn < options[0].cost)
    {
        cout << "Enter correct amount" << endl;
        cin >> moneyIn;
    }

    if(moneyIn > options[0].cost)
    {
        cout << "Your change is: " << (moneyIn - options[0].cost) << endl;
    }

}

}

Answer 1


I have fixed some things within your code. I have commented most of it; please have a read through it and try to understand why I have what I did.

Main things to note are:

  1. You declared an array globally called options, so there is no need to pass this array to any of your functions because they have access to it
  2. When you are dealing with arrays and their index, there is no need to use if statements to figure out what the customer is talking about; we can use the choice - 1 as the index to the array (you will know what I am talking about after reading the code)

Fixed Code:

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

// Structure to hold information about drink
struct Drink
{
    string drinkName;
    double cost;
    int numberInMachine;
};

// Essentially the machine with information about what is in it
Drink options[] = {{"Cola", .75, 0}, {"Root Beer", .75, 2}, {"Lemon-Lime", .75, 10},{"Grape Soda", .80, 3}, {"Cream Soda", .80, 20}};

int getChoice();
double showTransaction(int);

int main()
{
    int choice;
    double moneyEarned = 0.0;

    // Figuring out what the cutomer chose
    choice = getChoice();

    // Figuring out how much money the machine earned
    moneyEarned = showTransaction(choice);

    cout << "The machine earned: $" << moneyEarned << "." << endl;
    return 0;
}

int getChoice()
{
    int choice;

    cout << "Enter the number(1-6) of the drink you would like: " << endl;
    cout << "Drink Name         Cost        Number in Machine " << endl; 
    cout << "1. Cola            .75         " << options[0].numberInMachine << endl;
    cout << "2. Root Beer       .75         " << options[1].numberInMachine << endl;
    cout << "3. Lemon-lime      .75         " << options[2].numberInMachine << endl;
    cout << "4. Grape Soda      .80         " << options[3].numberInMachine << endl;
    cout << "5. Cream Soda      .80         " << options[4].numberInMachine << endl;
    cout << "6. Quit " << endl;

    cout << "Enter the number of your selection: ";
    cin >> choice;

    while(choice < 1 || choice > 6)
    {
        cout << "Please enter a valid number 1-6" << endl;
        cin >> choice;
    }

    return choice;
}

double showTransaction(int choice)
{
    double moneyIn;

    // If there isn't enough drinks ie. more than 0, then we can't sell any
    if(options[choice-1].numberInMachine < 1)
    return 0.0;

    cout << options[choice-1].drinkName << "costs $" << options[choice-1].cost << "." << endl;
    cout << "Enter money inserted up to $1.00: ";
    cin >> moneyIn;

    // If they enter less money than we need
    while(moneyIn < options[choice-1].cost)
    {
        cout << "The entered money is not enough, Please enter more: ";
        cin >> moneyIn;
    }

    cout << "Your change is: $" << (moneyIn - options[choice-1].cost) << "." << endl;

    return moneyIn;
}

Answer 2


In main, "drink" is a type and you are attempting to use it as a variable, "choice" has not been declared. I don't see why you are passing anything into "getchoice" since the function does use its arguments. Also, you have one signature for the function prototype for "showTransaction" and something else for its implementation.

Fix all that to begin with. You might also want to read this.

http://www.cprogramming.com/tutorial/lesson4.html