【发布时间】:2018-05-30 11:13:14
【问题描述】:
我想知道我的代码出了什么问题。它在计算后一直显示2.42092e-322 作为结果。我以为是因为我使用了int calcFee,所以我将其更改为double calcFee,但它仍然显示相同的结果。你们能指出哪里出了问题吗?
#include <iostream>
#include <iomanip>
using namespace std;
void detail();
double calcFee();
int main()
{
double total_fee;
detail();
total_fee = calcFee();
cout << "The total fee is RM " << total_fee << endl;
return 0;
}
void detail()
{
cout << "\t\t___________________________________________________________________________________" << endl;
cout << "\t\t| Participant Category\t|\tParticipant Type\t| Fee per Member(RM) |" << endl;
cout << "\t\t|_______________________|_______________________________|_________________________|" << endl;
cout << "\t\t|\t S\t\t|\t\t1\t\t|\t 50.00\t |" << endl;
cout << "\t\t|\t\t\t|_______________________________|_________________________|" << endl;
cout << "\t\t|\t\t\t|\t\t2\t\t|\t 75.00\t |" << endl;
cout << "\t\t|_______________________|_______________________________|_________________________|" << endl;
cout << "\t\t|\t T\t\t|\t\t1\t\t|\t 100.00\t |" << endl;
cout << "\t\t|\t\t\t|_______________________________|_________________________|" << endl;
cout << "\t\t|\t\t\t|\t\t2\t\t|\t 150.00\t |" << endl;
cout << "\t\t|_______________________|_______________________________|_________________________|" << endl;
}
double calcFee()
{
double total_fee = 0, member;
char category;
int type;
cout << endl << "Enter your category (S/T): ";
cin >> category;
cout << "Enter your type (1/2): ";
cin >> type;
cout << "Enter number of participants: ";
cin >> member;
if(category == 'S' || category == 's')
{
switch(type)
{
case 1:
{
total_fee = 50.00 * member;
}
break;
case 2:
{
total_fee = 75.00 * member;
}
break;
}
}
else if(category == 'T' || category == 't')
{
switch(type)
{
case 1:
{
total_fee = 100.00 * member;
}
break;
case 2:
{
total_fee = 150.00 * member;
}
break;
}
}
return total_fee;
}
感谢帮助过我的人。我一定会好好利用你的技巧和教训
【问题讨论】:
-
您永远不会在 main 中为该变量分配任何内容。
-
total_fee未初始化,因此打印它会调用未定义的行为。 -
赋予事物相同的名称并不会使它们成为相同的事物。 (而且你不应该将参数用作局部变量。)
-
请参阅
std::tolower和std::toupper,因此您只需要比较字母。 -
@ThomasMatthews :除了问题指出输入总是
S或T,所以根本不需要测试小写。