Findcheapest Function<?.

T

tomakated

How come it won't print out the lowest price item and subtract it from the
list<?.

and also what kind of code do I need for if the answer equals "N"<?.


Item 1 $ 2.25
Item 2 $ 2.25
Item 3 $ 2.25
Item 4 $ 1.25
Coupon Discount $( 0)
----------------------------
Total $ 8
$
#include <iostream>
#include <iomanip>

double findcheapest(double, double, double, double);
void printresults (double, double, double, double, double, double,
double);
void instructions();
double get_item_price();
int getresponse();

int main()
{
double item1, item2, item3, item4, lowest=0, total;
char response;

instructions();
item1 = get_item_price();
item2 = get_item_price();
item3 = get_item_price();
item4 = get_item_price();
total = item1 + item2 + item3 + item4;
cout << "Do you have a coupon for this purchase? \n" ;
response = getresponse() ;
if (response == 'y')
{ lowest = findcheapest(item1, item2, item3,
item4);
total = total - lowest;
}
printresults (response, item1, item2, item3, item4,
total, lowest);
}

void instructions()
{
// Lets see if this works<!!!
}

double get_item_price()
{
double i;
cout << "Enter the four item prices you want to buy.";
cout << ": ";
cin >> i;
while (i<0)
{
cout << "Please enter a positive number: ";
cin >> i;
}

return i;
}

int getresponse()
{ char response;

cout << "(y or n): \n";
cin >> response;

}
double findcheapest (double item1, double item2, double item3,
double item4)
{ double lowest;

if (((item1 <= item2) && (item1 <= item3) && (item1 <=
item4)))

lowest = item1;

else if (((item2 <= item1) && (item2 <= item3) && (item2
<= item4)))

lowest = item2;


else if (((item3 <= item1) && (item3 <= item2) && (item3 <=
item4)))

lowest = item3;


else if (((item4 <= item1) && (item4 <= item2) && (item4 <=
item3)))

lowest = item4;


}

void printresults(double response, double item1, double item2,
double item3, double item4, double total, double lowest)

{

cout << "Item 1 $" << setw(10) << item1 << endl;
cout << "Item 2 $" << setw(10) << item2 << endl;
cout << "Item 3 $" << setw(10) << item3 << endl;
cout << "Item 4 $" << setw(10) << item4 << endl;
cout << "Coupon Discount $(" << setw(9) <<
lowest<<")"<< endl;
cout << "----------------------------" << endl;
cout << "Total $" << setw(10) << total << endl;
}
 
A

ajk

How come it won't print out the lowest price item and subtract it from the
list<?.

and also what kind of code do I need for if the answer equals "N"<?.

note: normally one doesn't use doubles for monetary values since it is
not very precise - all doubles are rounded values. google bcd

in your function 'findcheapest' you should initialize 'lowest'.

another way to do it:

double findcheapest(double item1, double item2, double item3, double
item4)
{
return min( min( item1, item2 ), min( item3, item4 ) );
}

try using a vector instead to hold items:

#include <vector>

{
vector<double> items;
..

}

then it is easier to manipulate e.g. getting lowest:

vector<double>::iterator it = items.begin();
for ( ; it != items.end(); ++it )
{
if ( *it < lowest ) lowest = *it;
}


/ajk
 
I

Ivan Vecerina

tomakated said:
How come it won't print out the lowest price item and subtract it from the
list<?.
See below.
and also what kind of code do I need for if the answer equals "N"<?.
I'm not sure what you mean by this...
Item 1 $ 2.25
Item 2 $ 2.25
Item 3 $ 2.25
Item 4 $ 1.25
Coupon Discount $( 0)
----------------------------
Total $ 8
$
#include <iostream>
#include <iomanip>

double findcheapest(double, double, double, double);
void printresults (double, double, double, double, double, double,
double);
void instructions();
double get_item_price();
int getresponse();

int main()
{
double item1, item2, item3, item4, lowest=0, total;
char response;

instructions();
item1 = get_item_price();
item2 = get_item_price();
item3 = get_item_price();
item4 = get_item_price();
total = item1 + item2 + item3 + item4;
cout << "Do you have a coupon for this purchase? \n" ;
response = getresponse() ;
if (response == 'y')
{ lowest = findcheapest(item1, item2, item3,
item4);
total = total - lowest;
}
printresults (response, item1, item2, item3, item4,
total, lowest);
}

void instructions()
{
// Lets see if this works<!!!
}

double get_item_price()
{
double i;
cout << "Enter the four item prices you want to buy.";
cout << ": ";
cin >> i;
while (i<0)
{
cout << "Please enter a positive number: ";
cin >> i;
}

return i;
}

int getresponse()
{ char response;

cout << "(y or n): \n";
cin >> response;
And what is the return value here?
You need: return response;
}
double findcheapest (double item1, double item2, double item3,
double item4)
{ double lowest;

if (((item1 <= item2) && (item1 <= item3) && (item1 <=
item4)))

lowest = item1;

else if (((item2 <= item1) && (item2 <= item3) && (item2
<= item4)))

lowest = item2;


else if (((item3 <= item1) && (item3 <= item2) && (item3 <=
item4)))

lowest = item3;


else if (((item4 <= item1) && (item4 <= item2) && (item4 <=
item3)))

lowest = item4;

MISSING: return lowest;
Note also that the easier/usual way to compute the lowest value is:
double lowest = item1;
if( item2<lowest ) lowest = item2;
if( item3<lowest ) lowest = item3;
if( item4<lowest ) lowest = item4;
return lowest;
}

void printresults(double response, double item1, double item2,
double item3, double item4, double total, double lowest)

{

cout << "Item 1 $" << setw(10) << item1 << endl;
cout << "Item 2 $" << setw(10) << item2 << endl;
cout << "Item 3 $" << setw(10) << item3 << endl;
cout << "Item 4 $" << setw(10) << item4 << endl;
cout << "Coupon Discount $(" << setw(9) <<
lowest<<")"<< endl;
cout << "----------------------------" << endl;
cout << "Total $" << setw(10) << total << endl;
}

This program also lacks, to be standard-compliant, a using statement
or directive (it lacks std:: qualifications).
I haven't looked further than that...
but I hope this helps,

Ivan
 
J

Jerry Coffin

tomakated said:
How come it won't print out the lowest price item and subtract it from the
list<?.

Because you haven't done anything to tell it to do anything like that.
and also what kind of code do I need for if the answer equals "N"<?.

Something like "if (response == 'N')"

If you're trying to print the items out in order by price, I'd suggest
putting them into a vector and sort them before printing them out, or
perhaps a priority_queue (which is implicitly sorted).

I'd probably also create a class or struct to hold the information --
you might want things called "item1" and so on for now, but it's
almost inevitable that for any real use, you want a real names for
your items.

This has the distinct advantage of making it relatively easy to modify
the code to deal with an arbitrary number of items instead of being
specific to four items.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top