Kuromiu
Kuromiu

Reputation: 31

How to get the value of a local variable in C++?

I've always struggled with getting local variables from selection structures. I'm new to programming in general, and I need help with calculating the total purchase of items where their values are inside if functions, and I'm having trouble with my while function, because I intend to use the it to infinitely accept meal code and quantity until the user enters 0, but it doesn't work. How do I make my infinite while function work, and how do I properly get the values from selection structures? Any help, tips, and suggestions are appreciated!

Update: I was able to fix my problem with variables inside functions. Current problem is that I need to type in 0 twice because the first 0 gets ignored, and I don't know why.

int main()
{
    char mealCode;
    bool noOrder = false;
    int quantity = -1, subTotalA = 0, subTotalB = 0, subTotalC = 0, subTotalD = 0, subTotalE = 0, total = 0;

while (quantity != 0)
{
    cin >> mealCode >> quantity;
    if (quantity == 0)
        break;
    else
    {
        if (mealCode == 'A')
        {
            subTotalA += 45 * quantity;
            cout << "Subtotal is " << subTotalA << endl;
        }
        else if (mealCode == 'B')
        {
            subTotalB += 50 * quantity;
            cout << "Subtotal is " << subTotalB << endl;
        }
        else if (mealCode == 'C')
        {
            subTotalC += 55 * quantity;
            cout << "Subtotal is " << subTotalC << endl;
        }
        else if (mealCode == 'D')
        {
            subTotalD += 60 * quantity;
            cout << "Subtotal is " << subTotalD << endl;
        }
        else if (mealCode == 'E')
        {
            subTotalE += 75 * quantity;
            cout << "Subtotal is " << subTotalE << endl;
        }
    }
    total = subTotalA + subTotalB + subTotalC + subTotalD + subTotalE;
    cout << "Total Purchase is" << total;
}

}

Upvotes: 2

Views: 601

Answers (3)

user4581301
user4581301

Reputation: 33932

I dislike the if and switch solutions, so let's play with this a bit and eliminate the duplication.

#include <iostream> //cin. cout
#include <cstring> //strchr
#include <numeric> //std::accumulate
#include <iterator> //std::begin and std::end
using namespace std;

int main()
{
    char mealCode;
    int quantity;

    // use tables to replace the different code for different meal codes
    // list valid meal codes
    const char mealCodes[] = "ABCDE";
    // list cost of each meal code.
    const int mealCosts[] = {45, 50, 55, 60, 75};

    // Sucks when you ass an item to one table and not the other
    // so lets give ourselves a reminder, hmmm?
    // Refuse to compile if the costs and codes don't line up
    // note the -1 to ignore the null terminator in the string
    static_assert(std::size(mealCodes) - 1 == std::size(mealCosts),
                  "Meal codes and meal costs aren't the same size");

    // when you have a bunch of sequentially numbered variables that's nature
    // telling you you want an array. This one is forced to be the same size
    // as the number of meal codes. They're all 0, so no magic is required.
   int subTotal[std::size(mealCodes)] = {0};

    while (cin >> mealCode >> quantity && quantity != 0) // loop until bad input
                                                         // or quantity 0
    {
        // verify meal code
        const char * found = strchr(mealCodes, mealCode);
        if (found) // found a valid meal code
        {
            //get index of mealCode in mealCodes
            size_t index = found - mealCodes;
            // Look up the cost in the table and accumulate
            subTotal[index] += mealCosts[index] * quantity;
            // not 100% sure about the += but not summing multiple 
            // runs of the same meal code run seems wrong

            cout << "Subtotal is " << subTotal[index] << endl;
        }
    }
    // sum up all of the subtotals
    int total = std::accumulate(std::begin(subTotal), std::end(subTotal), 0);
    cout << "Total Purchase is Php" << total;
}

Advantages: adding a new meal code is two changes in the code: Add the code to mealCodes and add the cost to mealCosts.

Without the gratuitous comments the program is pretty short and isn't getting much bigger as the number of meals grows.

#include <iostream> 
#include <cstring> 
#include <numeric> 
#include <iterator> 
using namespace std;

int main()
{
    char mealCode;
    int quantity;

    const char mealCodes[] = "ABCDEFGHIJK";
    const int mealCosts[] = {45, 50, 55, 60, 75, 13, 62, 88, 42, 10, 99 };
    static_assert(std::size(mealCodes) - 1 == std::size(mealCosts),
                  "Meal codes and meal costs aren't the same size");
   int subTotal[std::size(mealCodes)] = {0};

    while (cin >> mealCode >> quantity && quantity != 0) 
    {
        const char * found = strchr(mealCodes, mealCode);
        if (found)
        {
            size_t index = found - mealCodes;
            subTotal[index] += mealCosts[index] * quantity;
            cout << "Subtotal is " << subTotal[index] << endl;
        }
    }
    int total = std::accumulate(std::begin(subTotal), std::end(subTotal), 0);
    cout << "Total Purchase is Php" << total;
}

Upvotes: 0

SandiaDeDia
SandiaDeDia

Reputation: 291

Not sure what you want to do, but a couple of comments

  • if you want to keep track of the subtotals and totals you need to initialize the variables to 0 and then sum the new subtotals for each product
  • If you want to use more than one product you need to include the operations in the while loop (while the user keeps introducing meals with a quantity!=0 you keep computing the subtotals)

Something like

    char mealCode;
    int subTotalA=0, subTotalB=0, total,quantity;

   
    while (cin >> mealCode >> quantity)
    {
        if (quantity <= 0) { break; }

        if (mealCode == 'A')
        {
            subTotalA += 45 * quantity;
            cout << "Subtotal is " << subTotalA << endl;
        }
        if (mealCode == 'B')
        {
            subTotalB += 50 * quantity;
            cout << "Subtotal is " << subTotalB << endl;
        }
      
        total = subTotalA + subTotalB ;
        cout << "Total Purchase is Php" << total;
    }

Upvotes: 1

tdao
tdao

Reputation: 17678

If you want your while loop to be infinite until quantity equal 0, you need to write it so. You don't need two identical cin.

Also it's better to use switch control structure if you are going to compare with so many constants that way.

For example, you may want to do something like this:

char mealCode;
int quantity = -1;

while (quantity != 0)
{
    cin >> mealCode >> quantity;
    if(quantity == 0)
    {
        break;
    }

    switch(mealCode)
    {
        case 'A':
            subTotalA = 45 * quantity;
            cout << "Subtotal is " << subTotalA << endl;
            break;

        case 'B':
        // ...

        default:
            break;
    }
}

Upvotes: 1

Related Questions