What am I doing Wrong??

This is what I have to write at the bottom is my program what am I doing wrong?

Write the C++ code that will contain 4 functions:

1. 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.

2. A void function named discountresults that accepts a price and a discount percent as float values and returns the discount amount and discounted price as parameters. Return these values as float values.

3. A void function named howmany that accepts the amount of money available and the cost of an item as float values and returns the number of items that can be purchased as an integer parameter and the amount of money left over from the purchase as parameters.

4. A function named menu that returns a character and does not accept any parameters. This function will display:

(B)est Buy Calculation
(D)iscount Calculation
(H)ow Many Calculation
(Q)uit

Please enter the option (B,D,H, or Q)

The main function will continue looping until Q is entered. Depending on which character is entered, the program will request the information for the function, pass the parameters into the function, and display to the monitor the returned values from the function.

Example:

(B)est Buy Calculation
(D)iscount Calculation
(H)ow Many Calculation
(Q)uit

Please enter the option (B,D,H, or Q) B


Please enter 3 prices 12.13 15.14 8.95
The smallest price is 8.95 and is price number 3


(B)est Buy Calculation
(D)iscount Calculation
(H)ow Many Calculation
(Q)uit

Please enter the option (B,D,H, or Q) D


Please enter a price and a discount amount 11.00 .10
The discount amount is 1.10 and the discounted price is 9.90

(B)est Buy Calculation
(D)iscount Calculation
(H)ow Many Calculation
(Q)uit

Please enter the option (B,D,H, or Q) H


Please enter amount available and cost of each 10.00 2.15
You can buy 4 and have 1.40 left over.

(B)est Buy Calculation
(D)iscount Calculation
(H)ow Many Calculation
(Q)uit

Please enter the option (B,D,H, or Q) Q


This is what I wrote

#include<iostream>
#include<iomanip>
using namespace std;

void discountresults (float price,float percent,float damount,float dprice);
void howmany (float money,float cost,float amt,float cashleft);
char menu();

int main ()
{
float price,percent,damount,dprice,money,cost,cashleft, amt;
char selection;
while(selection!='Q')
{
selection=menu();
cin>>selection;
switch(selection)
{case 'D':discountresults(price,percent,damount,dprice);
break;
case 'H':howmany(money,cost,amt,cashleft);
break;
case 'Q':break;
}
}
return 0;
}
char menu()
{
cout<<"(B)est Buy Calculation"<<endl;
cout<<"(D)iscount Calculation"<<endl;
cout<<"(H)ow Many Calculation"<<endl;
cout<<"(Q)uit"<<endl;
cout<<endl;
cout<<"Please enter the option (B,D,H,or Q) "<<endl;
}
void discountresults (float price, float percent, float damount, float dprice)
{
cout<<"Please enter a price and a discount amount "<<endl;
cin>>price;
cin>>percent;
damount=price*percent;
dprice=price-damount;
cout<<"The discount amount is "<<damount;
cout<<" and the discounted price is "<<dprice<<endl;

}
void howmany (float money,float cost,float amt, float cashleft)
{
cout<<"Please enter amount available and cost of each"<<endl;
cin>>money;
cin>>cost;
amt=money/cost;
amt=(int)amt;
cashleft=money-amt*cost;
cout<<"You can buy "<<amt;
cout<<" and have "<<cashleft;
cout<<" left over."<<endl;
cashleft=money-amt*cost;

}
You shouldn't be doing I/O in any of your functions, including main(), except for the menu() function.

I'll help with the discountresults() function:
1
2
3
4
5
6
7
8
9
10
11
void discountresults(
  // input arguments
  float price,
  float percent,
  // output arguments (notice the '&' ampersand)
  float& damount,
  float& dprice
  ) {
  damount = price * percent;
  dprice  = price - damount;
  }

Your other functions should have the following forms:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void bestbuy( float price1, float price2, float price3, float& dprice, int& dindex )
  {
  // using price1..3 figure out which is best --> dprice, and which argument index it was (1, 2, or 3)
  }

void howmany( float money_available, float item_cost, int& dnum_purchased, float& dmoney_left )
  {
  // dnum_purchased <-- number of items that can be purchased with the available money
  // dmoney_left <-- how much money is left from such a purchase
  }

char menu()
  {
  char result;
  // cout << "the menu" ...
  // cin >> result;
  return result;
  }

Good luck!
I need more help with this!!!
If someone wants to help add me to your Yahoo messenger: gonzales8286
I need someone to walk me through it!
What is this?
post using the code button (#) it makes it easer to read
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void howmany (float money,float cost,float amt, float cashleft)
{
cout<<"Please enter amount available and cost of each"<<endl;
cin>>money;
cin>>cost;
amt=money/cost;  
//you cant modify these unless there global and im guessing because you passed them in that they arnt
amt=(int)amt;
cashleft=money-amt*cost;
cout<<"You can buy "<<amt;
cout<<" and have "<<cashleft;
cout<<" left over."<<endl;
cashleft=money-amt*cost;

}


if you still want to modify them with out returning something you could pass the reference of them using & and modify them that way though you could

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float howmany (float money,float cost,float amt, float cashleft)
{
cout<<"Please enter amount available and cost of each"<<endl;
cin>>money;
cin>>cost;
float temp;
float temp2;
temp += amt;
temp += money/cost;  
//you cant modify these unless there global and im guessing because you passed them in that they arnt
temp=(int)temp;
temp2=money-temp*cost;
cout<<"You can buy "<<temp;
cout<<" and have "<<temp2;
cout<<" left over."<<endl;
temp2=money-amt*cost;
return temp2;
}

kinda just speculating what your trying to do but you get the point eh?
Last edited on
Topic archived. No new replies allowed.