【问题标题】:How to use int and double type?如何使用 int 和 double 类型?
【发布时间】: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::tolowerstd::toupper,因此您只需要比较字母。
  • @ThomasMatthews :除了问题指出输入总是ST,所以根本不需要测试小写。

标签: c++ int double


【解决方案1】:

你必须将calcFee()的返回值赋给main()局部变量total_fee

total_fee = calcFee(category, type, member) ;

此外,main() 局部变量 categorytypemember 没有在 main() 中使用,也没有被 calcFee() 修改,它只修改了的 copy这些统一变量。原始问题指出输入是通过参数提供的,因此您不应该在函数中接受输入。相反,categorytypemember 应该在调用 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 ;
}

注意使用 cmets 来解释代码的用途。我希望您的导师会为未注释的代码打分。

前置条件表示您在调用函数时假定为真的事情,因此不需要验证 - 验证是调用代码的责任 - 否则在实际应用程序中,您将一遍又一遍地重复验证相同的数据而不是只在一个地方。

还要注意 member 的类型 - 拥有非整数数量的成员没有语义意义。

【讨论】:

  • 谢谢哥们。你帮了我很多。我还在学习如何使用它。以后我会好好利用你的课
  • 我得到的问题要求我编写一个名为 calcFee() 的函数定义,所以我假设他们想要函数中的输入并计算它
  • @MNHaris :我根本不会做这个假设;特别是如果您指定了函数签名 - 这意味着输入是已知的。此外,作为严格的需求分析,如果要求您执行计算,则不应在同一函数中包含参数输入。它使函数的用处大大降低 - 例如,输入可能来自多个成员的数据库,而不是每次都手动输入!
  • 注意到了。你能教我如何在问题 b 中使用 bool 吗?我不明白该怎么做。
  • @MNHaris ;问题 b 是一个不同的问题;所以不是一个讨论论坛 - 你应该发布一个新问题。除此之外,它将拥有大量观众,而不仅仅是我。也就是说,该函数需要包含不超过return accommodation ? 150.0 : 0 ; - 基本上如果accommodataion == true,成本ID RM150,否则为零。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
  • 2021-08-12
相关资源
最近更新 更多