Home > Net >  Adding value to variables in array
Adding value to variables in array

Time:11-16

guys! I am creating a menu program in which user chooses a drink like tea or coffee. The essence of the program is that the user can choose drinks in the menu through the corresponding buttons (numbers 0, 1, 2, 3). The user can choose as much as he wants until he enters the number -1, which stops the program and gives the result where it should be highlighted:

1) How many drinks did he choose in total (quantity)

2) The amount of each drink selected by him ( that is, if he chose tea 2 times, then Tea should be allocated: 2 ) The problem is that I can't output the amount of each drink that user has selected.

What I have tried: I initiated a string array called beverage and included there names of 4 drinks. After I created a while loop and used sentinel of -1. Then I used an integer variable beverageCount[size] to store the overall amount of a precise drink/beverage relating them with mutual index choice, however it does not calculate amount and shows me wrong amount of each drink. I will attach my code:

#include <iostream>
#include <iomanip>

using namespace std;
int main()
{
    const int size = 5;
    string beverage[size] = { "Coffee", "Tea", "Coke", "Orange Juice" };
    int beverageCount[size];
    int choice=0;
    int i = 0;
    int coffee = 0;
    int tea = 0;
    int coke = 0;
    int juice = 0;
    //cin>>choice;
    //cout<<beverage[choice];
    while (choice != -1)
    {

        cout << "Please input the favorite beverage of person #1: Choose 0, 1, 2, or 3 from the\n";
        cout << "above menu or -1 to exit the program \n";
        cin >> choice;
        
        while (choice > 3 && choice != -1)
        {
            
            cout << "Invalid, only 1,2,3,4 or -1. Try again\n";
            cin >> choice;
        }
        beverageCount[choice]=0;
        beverageCount[choice]  ;
          i;
        
        
        if (choice == -1)
        {
            cout << "Number of beverages:" << i-1 << endl;
            cout << "Number of each beverage: " << beverage[0] << "\t" << beverageCount[0] << endl;
            for (i=0; i<4; i  )
            {
                cout<< beverage[i] << "\t" << beverageCount[i] << endl;
            }
            
            
        }
    }
    
}

CodePudding user response:

Here is my full solution with lots of refactoring and cleanup:


#include <iostream>
#include <iomanip>


void processOrder( )
{
    const std::string beverage[] = { "Coffee", "Tea", "Coke", "Orange Juice" };
    constexpr size_t numOfBeverageTypes { sizeof( beverage ) / sizeof( std::string ) };
    int beverageCount[numOfBeverageTypes] { };

    int choice { };
    int totalBeverageCount { };

    do
    {
        std::cout << "Please input the favorite beverage of person #1: Choose 0, 1, 2, or 3 from the "
                  << "above menu or -1 to exit the program\n";
        std::cin >> choice;

        while ( choice > 3 || choice < -1 )
        {
            std::cout << "Invalid, only 0,1,2,3 or -1. Try again\n";
            std::cin >> choice;
        }

        if ( choice == -1 )
        {
            std::cout << "Number of beverages ordered: " << totalBeverageCount
                      << '\n' << "Number of each beverage: \n";

            for ( size_t idx = 0; idx < ( numOfBeverageTypes );   idx )
            {
                std::cout << beverage[idx] << "\t" << beverageCount[idx] << std::endl;
            }
        }
        else
        {
              beverageCount[choice];
              totalBeverageCount;
        }

    } while ( choice != -1 );
}

int main()
{
    processOrder( );

    return 0;
}

Notice that I moved the code from main() to a function named processOrder(). Also, the do-while loop will do a better job compared to a while loop in this case.

  •  Tags:  
  • c
  • Related