Do I use a IF, WHILE, or DO-WHILE funtion here I am confused with this part of the program. Help!
"A void function named bestbuy that accepts three prices as float values and returns the smallest price and the number of the price as parameters. Return a 1 for the number if the first price is the lowest, a 2 if the second price is the lowest, and a 3 if the third price is the lowest"
void bestbuy (float price1, float price2, float price3, float dpriceq, float dindex)
{
cout << "Enter three prices " << endl;
cin >> price1 >> price2 >> price3;
__________________________________
__________________________________
cout << "The lowest price is " << dpriceq << " and it is number " << dindex << endl;
}
use if statements, and since dpriceq and dindex are values you want to change they should be passed by reference, that is:
void bestbuy (float price1, float price2, float price3, float& dpriceq, int& dindex)
the point of the function is to work with the values its given, so you don't need to use cin.
void bestbuy (float price1, float price2, float price3, float& dpriceb, float& nindex)
{
cout << "Enter three different prices " << endl;
cin >> price1 >> price2 >> price3;
if (price1 > price2)
cout << "The lowest price is " << price2 << " and is the price number " << endl;
else
cout << "The lowest price is " << price1 << " and is the price number " << endl;
again, you're not using cin and cout, you're using the parameters
1 2 3 4 5 6 7 8
void bestbuy (float price1, float price2, float price3, float& bestprice, int& index)
{
if (price1 <= price2 && price1 <= price3) {
bestprice = price1;
index = 1;
}
// do something similar for the other two
}
This is what I got How do I make the order correct. When I enter it the lowest value is correct the "nindex" is incorrect it puts out 3 every time how do I get it to say 1 when it is 1 and 2 when it is 2 and 3 when it is 3
void bestbuy (float price1, float price2, float price3, float& dpriceb, float& nindex)
{
cout << "Enter three different prices " << endl;
cin >> price1 >> price2 >> price3;
if (price1 <= price2 && price1 <= price3)
dpriceb = price1;
nindex = 1;
if (price2 <= price1 && price2 <= price3)
dpriceb = price2;
nindex = 2;
if (price3 <= price1 && price3 <= price2)
dpriceb = price3;
nindex = 3;
cout << "The lowest price is " << dpriceb <<" and it is price number " << nindex << endl;
}
Ok this is what I got left....I what dpriceb two numbers after the "." and I want nindex to have none how would I write it to work out??
cout << setiosflags(ios::fixed | ios::showpoint)<< setprecision(2);
cout << "The lowest value is " << dpriceb <<" and it is price number " << nindex << endl;
well, the easiest way would be to use an int for nindex, which is a more appropriate data type for an index. Otherwise you'd have to reset settings with << resetiosflags(ios::fixed | ios::showpoint)