8 Replies - 1050 Views - Last Post: 24 April 2012 - 12:52 PM Rate Topic: -----

#1 LoveDream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-April 12

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

Posted 24 April 2012 - 12:58 AM

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.




#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;

}


Is This A Good Question/Topic? 0
  • +

Replies To: How to add a loop, user-defined function and array to the program

#2 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 04:07 AM

You already have 3 user-defined functions.

Your else statements need {}. In this example:
if(condition)
{
    statement1;
    statement2;
}
else
    statement3;
    statement4;

Statement 3 and 4 have to be enclosed in {}, or statement 4 will always be executed.

Edit:
subtotal = totalPrice - discount;

You always give a discount of 0.1. Shouldn't it be 0.1 of the price?

This post has been edited by Hezekiah: 24 April 2012 - 04:51 AM

Was This Post Helpful? 1
  • +
  • -

#3 Mohsin01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 13-April 12

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 04:38 AM

desktopAmount = quantity * price; //as you have price as const
065
        tax = price * .07; //now here you are only multiplying one computer price,whereas its not right
                           //tax increases by the quantity or price,so you should change it. i think you got my point
066
        totalPrice = desktopAmount + tax;
067
        subtotal = totalPrice - discount;
068
        cout << "You are entitled to a discount!\n";






Was This Post Helpful? 1
  • +
  • -

#4 LoveDream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-April 12

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 09:27 AM

@Hezekiah,

thank you, I will change that.

For example, you meant like this, right?

062	    if (quantity > 3)
063	    {
064	        desktopAmount = quantity * price;
065	        tax = price * .07;
066	        totalPrice = desktopAmount + tax;
067	        subtotal = totalPrice - discount;
068	        cout << "You are entitled to a discount!\n";
069	    }
070	 
071	    else
072	  {
073	    desktopAmount = quantity * price;
074	    tax = price * .07;
075	    totalPrice = desktopAmount + tax;
076	    cout << "The total price you have to pay is: \n"
077	    << totalPrice << endl;



Was This Post Helpful? 0
  • +
  • -

#5 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 09:32 AM

Quote

For example, you meant like this, right?

Yes, but you need the closing }, which you didn't post in that snippet.
Was This Post Helpful? 1
  • +
  • -

#6 LoveDream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-April 12

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 09:34 AM

I'm sorry, this is what I meant to say, I couldn't find an "Edit Post" button.

@Hezekiah,

thank you, I will change that.

For example, you meant like this, right?

062	    if (quantity > 3)
063	    {
064	        desktopAmount = quantity * price;
065	        tax = price * .07;
066	        totalPrice = desktopAmount + tax;
067	        subtotal = totalPrice - discount;
068	        cout << "You are entitled to a discount!\n";
069	    }
070	 
071	    else
072	 [b]{[/b]
073	    desktopAmount = quantity * price;
074	    tax = price * .07;
075	    totalPrice = desktopAmount + tax;
076	    cout << "The total price you have to pay is: \n"
077	    << totalPrice << endl;
         [b]}[/b]




@Mohsin01, I got what you wanted to say, thank you, I missed that.

So, this code now has "A loop to prompt the user if they would like to place another order" and "at least one user-defined."

How can I add an "An enumerated data type array or struct" to the code?
Was This Post Helpful? 0
  • +
  • -

#7 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 10:05 AM

Quote

How can I add an "An enumerated data type array or struct" to the code?

Why not use all 3? First, it looks like the only difference between your functions is the product name which is printed, the price, and the quantity needed for a discount. Why not make one function and pass those values to it? You can put the values into a struct.

Now you have 3 sets of values which you pass to a function. This sounds like a good place to use an array. Array index 0 is for desktops, index 1 is for laptops and index 2 is for tablets. Why not give those indices names? This is a nice way to create an array which uses an enum for its indices:
enum Index {
    first,  //0
    second, //1
    third,  //2
    count   //3
};
Type array[count];

Was This Post Helpful? 1
  • +
  • -

#8 LoveDream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-April 12

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 10:20 AM

So, it would be something like this?

enum computerType {DESKTOP, LAPTOP, TABLET, count};
computerType[count];


I am really sorry but we didn't learn about arrays, that's why all the "un-knowledge". :)
Was This Post Helpful? 0
  • +
  • -

#9 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: How to add a loop, user-defined function and array to the program

Posted 24 April 2012 - 12:52 PM

Have you learned about structs and enums yet? Here is a tutorial on arrays. And here is an example of how you can use an array with an enum (you'll replace int with your struct):
enum index {
    first,  //0
    second, //1
    third,  //2
    count   //3
};

int main() {
    int array[count] = {7, 8, 9};
    cout << "first: " << array[first]
        << "\nsecond: " << array[second]
        << "\nthird: " << array[third] << endl;
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1