need help with drinks program c++

here is my code:
#include <iostream>
using namespace std;

int displayMenu(int);

double payment;
int choice;
double change;

int main()
{
struct drinkInfo
{
char* drinkName;
double cost;
int quantity;
};

int x = 0;
const int ARRAY_SIZE = 5;
double drinkpayment = 0.0;
int selection = 0;
double drinkchange = 0.0;
double earnings = 0.0;

drinkInfo drinkList[ARRAY_SIZE];

drinkList[0].drinkName = "Cola";
drinkList[0].cost = .75;
drinkList[0].quantity = 0;

drinkList[1].drinkName = "Root Beer";
drinkList[1].cost = .75;
drinkList[1].quantity = 2;

drinkList[2].drinkName = "Lemon-Lime";
drinkList[2].cost = .75;
drinkList[2].quantity = 10;

drinkList[3].drinkName = "Grape Soda";
drinkList[3].cost = .80;
drinkList[3].quantity = 3;

drinkList[4].drinkName = "Cream Soda";
drinkList[4].cost = .80;
drinkList[4].quantity = 20;



do
{

earnings += payment;
cout << "Welcome to CMPSC 125's Drink Dispenser." << endl;
cout << "Created by Cedric Shumate" << endl;
cout <<"Please make a selection from the menu below:" << endl;

selection = displayMenu(selection);

if (selection == 1)
{
cout << "Please enter the amount of money to be paid: ";
cin >> payment;
do
{
cout << "That is an invalid amount of change. Please re-enter your amount.";
cin >> payment;
}
while ((payment <= 0) || (payment > 1));
change = drinkList[0].cost - payment;
cout << "Your change is $" << change << endl;
}
else if (selection == 2)
{
cout << "Please enter the amount of money to be paid: ";
cin >> payment;
do
{
cout << "That is an invalid amount of change. Please re-enter your amount.";
cin >> payment;
}
while ((payment <= 0) || (payment > 1));
change = drinkList[1].cost - payment;
cout << "Your change is $" << change << endl;
}
else if (selection == 3)
{
cout << "Please enter the amount of money to be paid: ";
cin >> payment;
do
{
cout << "That is an invalid amount of change. Please re-enter your amount.";
cin >> payment;
}
while ((payment <= 0) || (payment > 1));
change = drinkList[2].cost - payment;
cout << "Your change is $" << change << endl;
}
else if (selection == 4)
{
cout << "Please enter the amount of money to be paid: ";
cin >> payment;
do
{
cout << "That is an invalid amount of change. Please re-enter your amount.";
cin >> payment;
}
while ((payment <= 0) || (payment > 1));
change = drinkList[3].cost - payment;
cout << "Your change is $" << change << endl;
}
else if (selection == 5)
{
cout << "Please enter the amount of money to be paid: ";
cin >> payment;
do
{
cout << "That is an invalid amount of change. Please re-enter your amount.";
cin >> payment;
}
while ((payment <= 0) || (payment > 1));
change = drinkList[4].cost - payment;
cout << "Your change is $" << change << endl;
}
else if (selection == 6)
cout << "Thank you for using the program. This machine has earned $" << earnings << "." << endl;
else
{
cout << "Your selection is invalid. Please review the \nmenu and make another selection." << endl;
displayMenu(selection);
}

}
while (selection != 6);
return 0;
}
int displayMenu(int choice)
{
cout << endl;
cout << "1. Cola" <<endl;
cout << "2. Root Beer" << endl;
cout << "3. Lemon-Lime" << endl;
cout << "4. Grape Soda" << endl;
cout << "5. Cream Soda" << endl;
cout << "6. Quit" <<endl;
cout << endl;
cout << "Your selection: ";
cin >> choice;

return choice;
}

here is the feedback my teacher gave me but it seems vague:
1) should not have any global variables; was to pass by reference or value as needed
2) instead of having the numerous assignment statements to load the array, you should have used the braces as discussed in class and done it in 1 statement when declaring the array struct
3) Should only display the report heading and by line once
4) when I entered an invalid amount twice in a row, it took it and gave me negative change back -- error
5) when I entered 1.00 later, it said it was invalid, but 0 to 1.00 was to be valid -- error
6) you don't display the line with the soda and # remaining
7) when I continued to purchase same type of soda, I started getting error on amount even if I entered the exact amount
8) Never received sold out message
9) Needed to use while loops to validate inputs, not if else
10) you were to have GetChoice and ProcessTransaction functions; you just have one to Display the menu. Even it is wrong as it does not pull values from array, but just includes values in quotes for output
11) you had choice as a parameter, but then return its value, too; choice should have been a reference variable and the function void
> 2) instead of having the numerous assignment statements to load the array,
> you should have used the braces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct drinkInfo
{
    // must be const if you are goint to point to string literals
    const char* drinkName; // *** const added

    double cost;
    int quantity;
};

int main()
{
    const int ARRAY_SIZE = 5;
    drinkInfo drinkList[ARRAY_SIZE] =
    {
        { "Cola", 0.75, 0 },
        { "Root Beer", 0.75, 2 },
        { "Lemon-Lime", 0.75, 10 },
        { "Grape Soda", 0.80, 3 },
        { "Cream Soda", 0.80, 20 }
    };

    // ...
}



> Even it is wrong as it does not pull values from array, but just includes values in quotes for output

1
2
3
4
5
void display_menu( const drinkInfo info[], int size )
{
    for( int i = 0 ; i < size ; ++i )
        std::cout << i+1 << ". " << info[i].drinkName << '\n' ;
}



> 9) Needed to use while loops to validate inputs, not if else

1
2
3
4
5
6
7
8
9
10
11
12
int get_choice( int num_choices )
{
    int choice ;
    while( std::cout << "choice [1-" << num_choices << "]: " &&
            std::cin >> choice && ( choice < 1 || choice > num_choices ) )
    {
        std::cout << "invald choice\n" ;
        // std::cin.clear() ;
        // std::cin.ignore( 1000, '\n' ) ;
    }
    return choice ;
}


The rest seem to be logical errors in the code which need fixing.
Topic archived. No new replies allowed.