1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

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

Discussion in 'C++' started by LoveDream, Apr 24, 2012.

  1. LoveDream

    LoveDream New Member

    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;
    
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    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.
     
  3. LoveDream

    LoveDream New Member

    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...
    
     
  4. xpi0t0s

    xpi0t0s Mentor

    Yep, that's about it. I'd be inclined actually to use those values, for example
    Code:
    desktopAmount = quantity * prices[0];
    
     

Share This Page