我有以下問題。在我的howmany函數中,它應該讀取您有多少錢以及一個物品的成本,然後它應該告訴您可以購買多少物品以及剩餘的貨幣。到目前爲止,我所能得到的所有項目數量都是0,貨幣數量顯示爲輸入的原始數量。錯誤的值與分區
任何幫助將不勝感激,也無論何時我打Q退出程序,我必須輸入它2或3次循環實際停止。
#include <iostream>
using namespace std;
void bestbuy(double&, double&, double);
void discountresults (double&, double&);
void howmany(double&, double&);
char menu();
double price1, price2, price3;//bestbuy variables
double price, discount;//discountresults variables
double cash,item;//howmany variables
int main()
{
char choice;
do
{menu();
choice = menu();}
while(choice != 'Q');
menu();
system ("PAUSE");
return 0;
}
void bestbuy(double &val1,double &val2, double val3)
{
if (val1 < val2 && val1 < val3)
val2 = 1;
else if (val2 < val1 && val2 < val3)
{val1 = val2;
val2 = 2;}
else
{val1 = val3;
val2 = 3;}
}
void discountresults(double &price, double &discount)
{
double hold;
hold = price;
price *= discount; //discount amount
hold -= price;
discount = hold; //price after discount
}
void howmany(double &money, double &itemcost)
{
double items;
items = money/itemcost;
itemcost = itemcost * items;
money = money - itemcost;
}
char menu()
{
char option;
cout<<"(B)est Buy Calculation.\n";
cout<<"(D)iscount Calculation.\n";
cout<<"(H)ow Many Calculation.\n";
cout<<"(Q)uit.\n";
cout<<"Please enter the option B, D, H, or Q\n";
cin>>option;
switch(option)
{
case 'B':
cout<<"Please enter 3 prices\n";
cin>>price1;
cin>>price2;
cin>>price3;
bestbuy(price1,price2,price3);
cout<<"Your lowest price entered was "<<price1<<" and it was the "<<price2<<" number you entered.\n";
break;
case 'D':
cout<<"Please enter price of item and discount percent\n";
cin>>price;
cin>>discount;
discountresults(price,discount);
cout<<"Your discount amount is "<<price<<" and the discounted price is "<<discount<<endl;
break;
case 'H':
cout<<"Please enter amount of money available and cost of item\n";
cin>>cash;
cin>>item;
howmany(cash,item);
cout<<"You can buy "<<cash<<" of that item and have $"<<item<<" left over\n";
break;
case 'Q':
return option;
}}
您是否嘗試過調試? – crashmstr
沒有縮進讓我難過。 –