0
#include<iostream>
using namespace std;
main()
{
    int numGame,numSocks,numShoes;
    int countX = 1,countY = 0;
    float tempGame = 0,tempSocks = 0,tempShoes = 0,GrandTotal = 0;
    float totalP,priceGame,priceSocks,priceShoes;

    cout<<"Welcome to Numbani Bowling Arena Transaction System.\n";
    cout<<"Please fill in the required sections below.\n";
    cout<<"---------------------------------------------------------\n";
    while( countX > -1 )
    {
        countY = countY + 1;
        cout<<"Enter the number of game(s) you want to play.\n";
        cin>>numGame;

        if( numGame > 0 && numGame < 4) 
        {
        priceGame = numGame * 8.00;
        tempGame = tempGame + priceGame;
        }
        else if( numGame > 3 && numGame < 9) 
        {
        priceGame = numGame * 7.50;
        tempGame = tempGame + priceGame;
        }
        else if( numGame > 8) 
        {
        priceGame = numGame * 7.00;
        tempGame = tempGame + priceGame;
        }

        cout<<"\nEnter the number of shoe(s) you that are required.\n";
        cin>>numShoes;

        priceShoes = numShoes * 3.00;
        tempShoes = tempShoes + priceShoes;

        cout<<"\nEnter the number of pairing socks you that are required.\n";
        cin>>numSocks;

        priceSocks = numSocks * 2.50;
        tempSocks = tempSocks + priceSocks;

        totalP = priceGame + priceSocks + priceShoes;
        GrandTotal = tempGame + tempShoes + tempSocks;

        cout<<"\nTotal price for game(s) is RM"<<priceGame;
        cout<<"\nTotal price for shoes is RM"<<priceShoes;
        cout<<"\nTotal price for socks is RM"<<priceSocks;
        cout<<"\nGrand total for all is RM"<<totalP<<"\n";

        cout<<"\nEnter any positive number to continue executing the program.(Ex. 1,2,3,4,...)";
        cout<<"\nEnter any negative number to exit the program.(Ex. -1,-2,-3,-4,...)\n";
        cin>>countX;
        cout<<"----------------------------------------------------------------------------------------\n\n";
    }
    cout<<"\nThank you for using the system.\n";
    cout<<"\n\nGrand total for all transactions is RM"<<GrandTotal;
    cout<<"\nTotal transactions made by users is "<<countY;

    cout<<"\n\nHave a nice day!\n\n";

    system("PAUSE");

    return 0;
}
3
Contributors
2
Replies
26
Views
2 Months
Discussion Span
Last Post by thines01
0

You have lots of opportunity to create functions out of this and, eventually, a class.
One option might be to take the code that determines the price for the game and separate into another function.
Above main(), you could have a function that returns the float (price) based on numGame:

float GetGamePrice(int numGame)
{
   if (numGame <= 0) return 0; // invalid
   if (numGame <= 3) return (float)(numGame * 8.00); // >0<=3
   if (numGame <= 8) return (float)(numGame * 7.50); // >3<=8
   return (float)(numGame * 7.00); // > 8
}

Then, inside you loop, you can call the function like this:

float priceGame = GetGamePrice(numGame);
tempGame += priceGame;

...and remove all of the code currently shown between lines 19-33;

Edited by thines01: ...cleaner

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.