Hi! I need some guidance on this project. I wrote most of it, but i would like to improve my code by implementing more functions, and im open to hear suggestions on my code. My main concern is, every time the user picks one of the drinks it should subtract 1 from the total 20 available, and whenever the quantity available reaches 0 display a message. How can i do this?? Thanks in advance for those willing to help.
PS. I read the info from a file!

//Display Menu
    do
    {
    earnings += payment - change;
    selection = displayMenu(drinksList, selection);                                             
        while(selection > 7 || selection < 1)
        {
        cout << "Please enter one of the available options: ";
        cin >> selection;
        }
           if(selection !=7)
           {
            switch(selection)
            {
              case 1: 
                   system("cls");
                   cout << "You chose: Coca-Cola\n";
                   cout <<"Please enter the amount of money to be paid: ";
                   cin >> payment;
                   if((payment <= 0.00) || (payment >= 1.00))
                   {
                      cout <<"That's an invalid amount of change.\n";
                      cout <<"Please re-enter your amount: ";
                      cin >> payment;
                      change = payment - drinksList[0].cost;
                      cout <<"Your change is: $" << change << endl <<endl;
                   }
                   else
                   {
                   change = payment - drinksList[0].cost;
                   cout <<"Your change is: $" << change << endl <<endl;
                   }
                   drinksList[0].quantity - 1;
                   break;
              case 2:
                   system("cls");
                   cout << "You chose: Root-Beer\n";
                   cout <<"Please enter the amount of money to be paid: ";
                   cin >> payment;
                   if((payment <= 0.00) || (payment >= 1.00))
                   {
                      cout <<"That's an invalid amount of change.\n";
                      cout <<"Please re-enter your amount: ";
                      cin >> payment;
                      change = payment - drinksList[0].cost;
                      cout <<"Your change is: $" << change << endl <<endl;
                   }
                   else
                   {
                   change = payment - drinksList[0].cost;
                   cout <<"Your change is: $" << change << endl <<endl;
                   }
                   break;
              case 3:
                   system("cls");
                   cout << "You chose: Sprite\n";
                   cout <<"Please enter the amount of money to be paid: ";
                   cin >> payment;
                   if((payment <= 0.00) || (payment >= 1.00))
                   {
                      cout <<"That's an invalid amount of change.\n";
                      cout <<"Please re-enter your amount: ";
                      cin >> payment;
                      change = payment - drinksList[0].cost;
                      cout <<"Your change is: $" << change << endl <<endl;
                   }
                   else
                   {
                   change = payment - drinksList[0].cost;
                   cout <<"Your change is: $" << change << endl <<endl;
                   }
                   break;
              case 4:
                   system("cls");
                   cout << "You chose: Spring-Water\n";
                   cout <<"Please enter the amount of money to be paid: ";
                   cin >> payment;
                   if((payment <= 0.00) || (payment >= 1.00))
                   {
                      cout <<"That's an invalid amount of change.\n";
                      cout <<"Please re-enter your amount: ";
                      cin >> payment;
                      change = payment - drinksList[0].cost;
                      cout <<"Your change is: $" << change << endl <<endl;
                   }
                   else
                   {
                   change = payment - drinksList[0].cost;
                   cout <<"Your change is: $" << change << endl <<endl;
                   }
                   break;
              case 5:
                   system("cls");
                   cout << "You chose: Apple-Juice\n";
                   cout <<"Please enter the amount of money to be paid: ";
                   cin >> payment;
                   if((payment <= 0.00) || (payment >= 1.00))
                   {
                      cout <<"That's an invalid amount of change.\n";
                      cout <<"Please re-enter your amount: ";
                      cin >> payment;
                      change = payment - drinksList[0].cost;
                      cout <<"Your change is: $" << change << endl <<endl;
                   }
                   else
                   {
                   change = payment - drinksList[0].cost;
                   cout <<"Your change is: $" << change << endl <<endl;
                   }
                   break;
              case 6:
                   system("cls");
                   cout <<"Thank you for using the program.\n";
                   cout <<"This machine has earned: $"
                        << earnings << "." <<endl <<endl;
                   break;
              case 7:
                   cout <<"Thank you for using the program.\n";
                   break;
              }
            }
   }while(selection !=7); 
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Edited 5 Years Ago by hous3aholik: Had some personal information in my code.

You have a lot of repetitive code in your case statements. Think of a way you can convert all this code into a method and just call it. Simplifies making changes to the way the selections are handled as you only need to make changes in one place. Changes like subtracting one from the cans available.

Also, what if they enter the wrong amount the 2nd time you ask for it?
I don't see where you adjust earnings.

You have a lot of repetitive code in your case statements. Think of a way you can convert all this code into a method and just call it. Simplifies making changes to the way the selections are handled as you only need to make changes in one place. Changes like subtracting one from the cans available.

Also, what if they enter the wrong amount the 2nd time you ask for it?
I don't see where you adjust earnings.

I see what you're saying! but how could i fix this??

Are you at all familiar with functions?

yes. i just laid out my code to start from there, and to hear suggestions.

This article has been dead for over six months. Start a new discussion instead.