我在学校任务中遇到了这个课程的问题。该程序的目的是从用户输入计算加仑。我的问题是,我认为我的函数是错误的,当我编译时,我一直都会得到0。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int getdata(char, double, char);
string findGasName(char);
int computeGallons(char, double, char);
void display(string, double, double, double);
int main()
{
string gasName;
double washCost = 0;
char gasType = 'a';
double dAmountremaining = 0;
double gallons = 0;
const double washResponseunder30 = 6.95;
const double REG = 2.199;
const double UNLEAD = 2.399;
const double SUNLEAD = 2.599;
double dAmount = 0;
char washResponse = 'a';
getdata(gasType, dAmount, washResponse);
gasName = findGasName(gasType);
gallons = computeGallons(gasType, dAmount, washResponse);
display (gasName, gallons, washCost, dAmount);
system("pause");
return 0;
}
这个区域是我认为遇到麻烦的地方。我以前把int getdata函数作为一个空格但是它也没有工作。
int getdata(char gasType, double dAmount, char washResponse)
{
cout << "Enter gas type(R,U,S) ";
toupper(gasType);
cin >> gasType;
while (gasType != 'R' && gasType != 'r' && gasType != 'U' && gasType != 'u' && gasType != 'S' && gasType != 's')
{
cout << "Enter a gas type (R,U,S) ";
cin >> gasType;
}
cout << "Enter dollar Amount ";
cin >> dAmount;
while (dAmount < 4.95)
{
cout << "Please enter Amount greater than gasType " << endl;
cin >> dAmount;
}
cout << "Car Wash (Y or N)";
cin >> washResponse;
while (washResponse != 'Y' && washResponse != 'y' && washResponse != 'N' && washResponse != 'n')
{
cout << "Please enter Y for (Yes) or N for (No)";
cin >> washResponse;
}
return gasType, dAmount, washResponse;
}
string findGasName(char gasType)
{
string gasName;
if (gasType == 'R' && gasType == 'r')
gasName = "Regular";
else if (gasType == 'U' && gasType == 'u')
gasName = "Unleaded";
else if (gasType == 'S' && gasType == 's')
gasName = "Super";
return gasName;
}
int computeGallons(char gasType, double dAmount, char washResponse)
{
double washCost;
const double washResponsewith30 = 4.95;
double dAmountremaining = 0;
double gallons = 0;
const double washResponseunder30 = 6.95;
const double REG = 2.199;
const double UNLEAD = 2.399;
const double SUNLEAD = 2.599;
if (dAmount >= 30) {
washCost = washResponsewith30;
dAmountremaining = dAmount - washCost;
switch (gasType)
{
case 'R': case 'r':
gallons = dAmountremaining / REG;
break;
case 'U': case 'u':
gallons = dAmountremaining / UNLEAD;
break;
case 'S': case 's':
gallons = dAmountremaining / SUNLEAD;
break;
}
}
else if (dAmount < 30)
{
washCost = washResponseunder30;
dAmountremaining = dAmount - washCost;
switch (gasType)
{
case 'R': case 'r':
gallons = dAmountremaining / REG;
break;
case 'U': case 'u':
gallons = dAmountremaining / UNLEAD;
break;
case 'S': case 's':
gallons = dAmountremaining / SUNLEAD;
break;
}
}
else
{
washCost = 0;
switch (gasType)
{
case 'R': case 'r':
gallons = dAmountremaining / REG;
break;
case 'U': case 'u':
gallons = dAmountremaining / UNLEAD;
break;
case 'S': case 's':
gallons = dAmountremaining / SUNLEAD;
break;
}
}
return gallons;
}
void display(string gasName, double gallons, double washCost, double dAmount)
{
cout << "Gas type" << setw(12) << setprecision(2) << ":" << gasName << endl;
cout << "Gallons" << setw(13) << ":" << gallons << endl;
cout << "Car Wash" << setw(13) << ":$" << washCost << endl;
cout << endl;
cout << "Purchase Amount" << setw(6) << ":$" << dAmount << endl;
}
答案 0 :(得分:0)
好吧,因为你的函数int getdata(char, double, char);
没有按照你的想法做到。你想这样做。不要忘记选择最佳答案。
void getdata(char *, double*, char*);
//In your main getdata(&gasType, &dAmount, &washResponse); it should look like this
void getdata(char *gasType, double *dAmount, char *washResponse)
{
cout << "Enter gas type(R,U,S) ";
toupper(*gasType);
cin >> *gasType;
while (*gasType != 'R' && *gasType != 'r' && *gasType != 'U' && *gasType != 'u' && *gasType != 'S' && *gasType != 's')
{
cout << "Enter a gas type (R,U,S) ";
cin >> *gasType;
}
cout << "Enter dollar Amount ";
cin >> *dAmount;
while (*dAmount < 4.95)
{
cout << "Please enter Amount greater than gasType " << endl;
cin >> *dAmount;
}
cout << "Car Wash (Y or N)";
cin >> *washResponse;
while (*washResponse != 'Y' && *washResponse != 'y' && *washResponse != 'N' && *washResponse != 'n')
{
cout << "Please enter Y for (Yes) or N for (No)";
cin >> *washResponse;
}
}