我想知道我的代码出了什么问题。它在计算后不断显示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;
}
感谢那些帮助过我的人。我会确保充分利用您的提示和课程
答案 0 :(得分:1)
您必须将calcFee()
的返回值分配给main()
本地变量total_fee
:
total_fee = calcFee(category, type, member) ;
此外,main()
中未使用category
局部变量type
,member
和main()
,calcFee()
未修改category
仅修改这些单元化变量的副本。原始问题表明输入是通过参数提供的,因此您不应接受函数内的输入。相反,在调用type
之前,应从输入中为member
,calcFee()
和double calcFee( char category, int type, int member )
{
double total_fee = 0 ;
if( category == 'S' )
{
switch(type)
{
case 1:
{
total_fee = 50.00 * member;
}
break;
case 2:
{
total_fee = 75.00 * member;
}
break;
}
}
else if(category == 'T')
{
switch(type)
{
case 1:
{
total_fee = 100.00 * member;
}
break;
case 2:
{
total_fee = 150.00 * member;
}
break;
}
}
return total_fee;
}
分配值。这个问题根本不要求你接受用户输入,并保证输入有效(例如,不需要测试小写),所以以下是一个合适的解决方案:
// Pre-conditions: category = `S` or `T`
// type = 1 or 2
// member > 0
double calcFee( char category, int type, int member )
{
double total_fee = member * 100 ; // Primary student fee
// Secondary staff/students pay 50% more
if( type == 2 )
{
total_fee *= 1.5 ;
}
// Students pay half secondary/primary fee
if( category == 'S' )
{
total_fee /= 2.0 ;
}
return total_fee ;
}
该功能可以大大简化:
member
请注意使用注释来解释代码的目的。我希望你的导师会删除未评论代码的分数。
前置条件表示在调用函数时假设为真的事情,因此无需验证 - 验证是调用代码的责任 - 否则在实际应用程序中,您将反复验证相同的数据一遍又一遍而不只是在一个地方。
还要注意@RequestBody
的类型 - 拥有非整数的成员没有语义意义。