Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to display total price after looping

+10 votes
asked Nov 8, 2021 by Chang Wei Kit (250 points)
// Example program
#include <iostream>
using namespace std;

int main()
{
        
    
    char gender,length,style;
    char answer ;
  do{
    cout<<"Enter Your Gender(F/M):";
    cin>>gender ;
    
    cout<<"Enter Your Hair Length(S/M/L):" ;
    cin>>length ;
    
    cout<<"Enter Your Style(R/S):" ;
    cin>>style ;
    
    
      switch (gender)
    {
    
    case 'F':
      if (((length=='S')&&(style=='R')))
      {
      cout<<"Price:RM"<<"25"<<endl;
      }
      else if (((length=='S')&&(style=='S')))
      {
        cout<<"Price:"<<"RM 35"<<endl;
      }
      else if (((length=='M')&&(style=='R')))
      {
        cout<<"Price:"<<"RM 30"<<endl;
      }
      else if (((length=='M')&&(style=='S')))
      {
        cout<<"Price:"<<"RM 40"<<endl;
      }
      else if (((length=='L')&&(style=='R')))
      {
         cout<<"Price:"<<"RM 35"<<endl;
      }
      else
      {
          cout<<"Price:"<<"RM 45"<<endl;
      }
      break;
      
      case 'M':
      if (((length=='S')&&(style=='R')))
      {
      cout<<"Price:"<<"RM 28 "<<endl;
      }
      else if (((length=='S')&&(style=='S')))
      {
        cout<<"Price:"<<"RM 33"<<endl;
      }
      else if (((length=='M')&&(style=='R')))
      {
        cout<<"Price:"<<"RM 31"<<endl;
      }
      else if (((length=='M')&&(style=='S')))
      {
        cout<<"Price:"<<"RM 36"<<endl;
      }
      else if (((length=='L')&&(style=='R')))
      {
         cout<<"Price:"<<"RM 35"<<endl;
      }
      else
      {
          cout<<"Price:"<<"RM 40"<<endl;
      }
      

      break;
      default:
      cout<<"Not valid"<<endl;
    }
    cout<<"Add another customer?(Y/N):" ;
    cin>>answer ;
    }while(answer =='Y') ;

    return 0  ;

    
}

1 Answer

0 votes
answered Nov 9, 2021 by Peter Minarik (84,720 points)
edited Nov 9, 2021 by Peter Minarik

You can create a new variable, e.g. total, and increment it with the amount after every customer is billed. See it highlighted in the code below.

I'd also create a look-up table for the prices based on your 3 parameters: gender, length, style.

Your code enhanced:

#include <cctype>
#include <cstring>
#include <iostream>
#include <stdexcept>

const unsigned int prices[2][3][2] = 
{
    {               // Female
        { 25, 35 }, // Short: R, S
        { 30, 40 }, // Medium: R, S
        { 35, 45 }, // Long: R, S
    },
    {               // Male
        { 28, 33 }, // Short: R, S
        { 31, 36 }, // Medium: R, S
        { 35, 40 }, // Long: R, S
    }
};

const char * genders = "FM";
const char * lengths = "SML";
const char * styles = "RS";

// Returns the 0-based index of value in validValues; -1 if not found
int Lookup(const char * validValues, char value)
{
    const char * found = std::strchr(validValues, value);
    if (found)
        return found - validValues;
    else
        return -1;
}

// Finds the price in prices based on gender, length and style
unsigned int LookupPrice(char gender, char length, char style)
{
    int g = Lookup(genders, std::toupper(gender));
    if (g == -1)
        throw std::runtime_error("Unknown gender: " + std::string(1, gender));

    int l = Lookup(lengths, std::toupper(length));
    if (l == -1)
        throw std::runtime_error("Unknown length: " + std::string(1, length));

    int s = Lookup(styles, std::toupper(style));
    if (s == -1)
        throw std::runtime_error("Unknown style: " + std::string(1, style));
        
    return prices[g][l][s];
}

int main()
{
    char addMore;
    unsigned int total = 0;
    do
    {
        char gender;
        std::cout << "Enter your gender (F/M): ";
        std::cin >> gender;
    
        char length;
        std::cout << "Enter your hair length (S/M/L): ";
        std::cin >> length;
        
        char style;
        std::cout << "Enter your style (R/S): ";
        std::cin >> style;

        try
        {
            unsigned int price = LookupPrice(gender, length, style);
            std::cout << "Price: $" << price << std::endl;
            total += price;
        }
        catch (const std::runtime_error& e)
        {
            std::cout << "error: " << e.what() << std::endl;
        }
        catch (const std::exception& e)
        {
            std::cout << "error: " << e.what() << std::endl;
        }

        std::cout << "Add another customer? (Y/N): ";
        std::cin >> addMore;
    } while (std::toupper(addMore) == 'Y');

    std::cout << "Total: $" << total << std::endl;
    return 0;
}
commented Nov 18, 2021 by Chang Wei Kit (250 points)
thanks for your help
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...