Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   How to add a loop, user-defined function and array to the code (http://www.go4expert.com/forums/add-loop-user-defined-function-array-t28257/)

LoveDream 24Apr2012 13:36

How to add a loop, user-defined function and array to the code
 
Hello, guys.

I have to do this assignment until tomorrow. We had to write a "selling program for computers, laptops and tablets", which I did but for the extra credit, we have to have those three points in the application and I have tried the entire weekend to do that but didn't know how to do the "extra credit" part, which I really need.

The application is pretty basic and easy but I have to also have these three things and I was wondering if you guys could help me with these:

1.) A loop to prompt the user if they would like to place another order

2.) At least one user-defined function

3.) An enumerated data type, array or struct (structure)


I did one of these three, it's a "DO WHILE" loop asking users if they want to make another order, it's right at the beginning of the code.

Anyway, this is the source code and I will be really, very grateful if someone helps me out here.



Code:


#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

double desktop();                //function prototype
double laptop();
double tablet();


int main()
{

        char order, makeAnotherOrder;
        double amountDue;

        do
        {

        cout << "Please select your model (d, l, t)\n";

        cin >> order;

        if (order == 'd')
                amountDue = desktop();

        if (order == 'l')
                amountDue = laptop();

        if (order == 't')
        amountDue = tablet();

                cout << "The total amount with a discount (if you were eligible for one) is : $" << amountDue << endl;

                cout << "Would you like to place another order? Press 'y' to confirm.\n";
                cin >> makeAnotherOrder;

                } //closing brace of the loop statement

                while (makeAnotherOrder == 'y' || makeAnotherOrder == 'Y');

                return 0;

}

double desktop()
{

        int quantity;
        double desktopAmount;
        double discount = .1;
        const double price = 999;
        double subtotal, tax, totalPrice;

        cout << "Please enter how many desktop computers you want to buy: \n";

        cin >> quantity;


        if (quantity > 3)
        {
                desktopAmount = quantity * price;
                tax = price * .07;
                totalPrice = desktopAmount + tax;
                subtotal = totalPrice - discount;
                cout << "You are entitled to a discount!\n";
        }

        else

        desktopAmount = quantity * price;
        tax = price * .07;
        totalPrice = desktopAmount + tax;
        cout << "The total price you have to pay is: \n"
        << totalPrice << endl;


        cout << setfill('.') << left << setw(35) << "Quantity: " << right << " " << quantity << endl;

        cout << left << setfill ('.') << setw(35) << "Price: " << setfill(' ') << right << " $" << setw(1) << price << endl;

        cout << setfill('.') << left << setw(28) << "Tax: " << setfill('.') << right << setw(9) << "$" << tax << endl;

        cout << left << setfill ('.') << setw(35) << "Total Price: " << setfill(' ') << right << " $" << setw(7) << totalPrice << endl;


        return desktopAmount;

}


double laptop()
{

        int quantity;
        double laptopAmount;
        double discount = .1;
        const double price = 699;
        double subtotal, tax, totalPrice;

        cout << "Please enter how many laptops you want to buy: \n";

        cin >> quantity;


        if (quantity > 5)
        {
                laptopAmount = quantity * price;
                tax = price * .07;
                totalPrice = laptopAmount + tax;
                subtotal = totalPrice - discount;
                cout << "You are entitled to a discount!\n";
        }

        else

        laptopAmount = quantity * price;
        tax = price * .07;
        totalPrice = laptopAmount + tax;
        cout << "The total price you have to pay is: \n"
        << totalPrice << endl;


        cout << setfill('.') << left << setw(35) << "Quantity: " << right << " " << quantity << endl;

        cout << left << setfill ('.') << setw(35) << "Price: " << setfill(' ') << right << " $" << setw(1) << price << endl;

        cout << setfill('.') << left << setw(28) << "Tax: " << setfill('.') << right << setw(9) << "$" << tax << endl;

        cout << left << setfill ('.') << setw(35) << "Total Price: " << setfill(' ') << right << " $" << setw(7) << totalPrice << endl;


        return laptopAmount;

}


double tablet()
{

        int quantity;
        double tabletAmount;
        double discount = .1;
        const double price = 399;
        double subtotal, tax, totalPrice;

        cout << "Please enter how many tablets you want to buy: \n";

        cin >> quantity;


        if (quantity > 10)
        {
                tabletAmount = quantity * price;
                tax = price * .07;
                totalPrice = tabletAmount + tax;
                subtotal = totalPrice - discount;
                cout << "You are entitled to a discount!\n";
        }

        else

        tabletAmount = quantity * price;
        tax = price * .07;
        totalPrice = tabletAmount + tax;
        cout << "The total price you have to pay is: \n"
        << totalPrice << endl;



        cout << setfill('.') << left << setw(35) << "Quantity: " << right << " " << quantity << endl;

        cout << left << setfill ('.') << setw(35) << "Price: " << setfill(' ') << right << " $" << setw(1) << price << endl;

        cout << setfill('.') << left << setw(28) << "Tax: " << setfill('.') << right << setw(9) << "$" << tax << endl;

        cout << left << setfill ('.') << setw(35) << "Total Price: " << setfill(' ') << right << " $" << setw(7) << totalPrice << endl;





        return tabletAmount;

}


xpi0t0s 25Apr2012 00:11

Re: How to add a loop, user-defined function and array to the code
 
You've also got the second of the three: desktop(), laptop() and tablet() are user defined functions.

Which of an enumerated data type, array or struct do you want to use, and what for? You could perhaps use a global array to list the prices of each type of hardware, for example:
Code:

const double prices[3]={999, 699, 399};
then reference these values instead of using the literals within the code.

LoveDream 25Apr2012 00:26

Re: How to add a loop, user-defined function and array to the code
 
So, I add array globally like this and that's it?

Code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

double desktop();                //function prototype
double laptop();
double tablet();

const doublePrices[3]={999, 699, 399};

int main()
{

        char order, makeAnotherOrder;
        double amountDue;

        do
        {

        cout << "Please select your model (d, l, t)\n"; etc...


xpi0t0s 25Apr2012 00:48

Re: How to add a loop, user-defined function and array to the code
 
Yep, that's about it. I'd be inclined actually to use those values, for example
Code:

desktopAmount = quantity * prices[0];


All times are GMT +5.5. The time now is 09:22.