首页 文章

C输出错误?

提问于
浏览
0

我编写了这个声明 .

编写程序以获得3件商品的单价和数量 . 程序应输出总计小计 . 如果总数大于10,000.00,则应给予5%的折扣 . 使用具有返回类型的函数,如下所述 . 一个 . 名称:输入,返回类型:void描述:取单位价格的值和项目的数量 . 湾名称:calculateSubTotal,返回类型:double描述:计算特定项目的小计 . C . 名称:print返回类型:void描述:应使用小计,总计和折扣打印最后一个帐单

#include <iostream>

using namespace std;

void netTotal1(double &uPrice1,int &qty1)
{
    double amount1;
    cout<<"Enter Unit Price of First Item : ";
    cin>>uPrice1;
    cout<<"Enter Quantity : ";
    cin>>qty1;
    amount1=uPrice1*qty1;
}
void netTotal2(double &uPrice2,int &qty2)
{
    double amount2;
    cout<<"Enter Unit Price of Second Item : ";
    cin>>uPrice2;
    cout<<"Enter Quantity : ";
    cin>>qty2;
    amount2=uPrice2*qty2;
}
void netTotal3(double &uPrice3,int &qty3)
{
    double amount3;
    cout<<"Enter Unit Price of Third Item : ";
    cin>>uPrice3;
    cout<<"Enter Quantity : ";
    cin>>qty3;
    amount3=uPrice3*qty3;
}

double tot(double &amount1,double &amount2,double &amount3)
{
    double total;
    total=amount1+amount2+amount3;
    cout<<"\nNet Amount of First Item : "<<amount1;
    cout<<"\nNet Amount of First Item :   "<<amount1;
    cout<<"\nNet Amount of First Item : "<<amount1;
    cout<<"\nTotal of  All Items : "<<total;
}

double discount( double &total)
{
    double dsc;
    if (total>10000)
    {
        dsc=(total*0.05);
    }
    else
    {
        dsc=0;
    }
    return dsc;
}

double subTot(double &total,double &dsc)
{
    double subTotal;
    subTotal=total-dsc;

    cout<<"\nDiscount : "<<dsc;
    cout<<"\nSub Total is : " <<subTotal;
}


int main()
{
    double uPrice1,uPrice2,uPrice3,total,amount1,amount2,amount3,dsc,subTotal;
    int qty1,qty2,qty3;

    netTotal1(uPrice1,qty1);
    netTotal2(uPrice2,qty2);
    netTotal3(uPrice3,qty3);

    tot(amount1,amount2,amount3);

    discount(total);

    subTot(total,dsc);
}

不幸的是输出有误 .

Enter Unit Price of First Item : 10
Enter Quantity : 10
Enter Unit Price of Second Item : 10
Enter Quantity : 10
Enter Unit Price of Third Item : 10
Enter Quantity : 10

Net Amount of First Item : 5.2668e+267
Net Amount of First Item :   5.2668e+267
Net Amount of First Item : 5.2668e+267
Total of  All Items : 7.14983e+281
Discount : 1.20132e-306
Sub Total is : -1.00361e-306
Process returned 0 (0x0)   execution time : 5.957 s
Press any key to continue.

1 回答

  • 0

    它不适合您,因为您的某些变量(如amount1,amount2等)仅在您的函数中本地修改 .

    尝试始终初始化变量,它将更容易调试,现在您只是从未初始化的变量打印一些垃圾 .

    如果要修改函数内部的值传递引用,或者更好地从函数返回一些输出并将该输出分配给所需的变量 .

    #include <iostream>
    
    using namespace std;
    
    void netTotal1(double &uPrice1,int &qty1, double &amount1)
    {
        cout<<"Enter Unit Price of First Item : ";
        cin>>uPrice1;
        cout<<"Enter Quantity : ";
        cin>>qty1;
        amount1=uPrice1*qty1;
    }
    void netTotal2(double &uPrice2,int &qty2, double &amount2)
    {
    
        cout<<"Enter Unit Price of Second Item : ";
        cin>>uPrice2;
        cout<<"Enter Quantity : ";
        cin>>qty2;
        amount2=uPrice2*qty2;
    }
    void netTotal3(double &uPrice3,int &qty3, double &amount3)
    {
        cout<<"Enter Unit Price of Third Item : ";
        cin>>uPrice3;
        cout<<"Enter Quantity : ";
        cin>>qty3;
        amount3=uPrice3*qty3;
    }
    
    void tot(double &amount1,double &amount2,double &amount3, double &total)
    {
        total=amount1+amount2+amount3;
        cout<<"\nNet Amount of First Item : "<<amount1;
        cout<<"\nNet Amount of First Item :   "<<amount1;
        cout<<"\nNet Amount of First Item : "<<amount1;
        cout<<"\nTotal of  All Items : "<<total;
    }
    
    void discount( double &total, double &dsc)
    {
        if (total>10000)
        {
            dsc=(total*0.05);
        }
        else
        {
            dsc=0;
        }
    
    }
    
    void subTot(double &total,double &dsc)
    {
        double subTotal;
        subTotal=total-dsc;
    
        cout<<"\nDiscount : "<<dsc;
        cout<<"\nSub Total is : " <<subTotal;
    }
    
    
    int main()
    {
        double uPrice1,uPrice2,uPrice3,total,amount1,amount2,amount3,dsc,subTotal;
        int qty1,qty2,qty3;
    
        netTotal1(uPrice1,qty1, amount1);
        netTotal2(uPrice2,qty2, amount2);
        netTotal3(uPrice3,qty3, amount3);
    
        tot(amount1,amount2,amount3, total);
    
        discount(total, dsc);
    
        subTot(total,dsc);
    }
    

    最好尝试从函数返回值,并尽量不重复代码,略微改进代码:#include

    double askForValues(double &unitPrice, int &quantity)
    {
        std::cout << "Enter Unit Price of Item: ";
        std::cin >> unitPrice;
        std::cout << "Enter Quantity: ";
        std::cin >> quantity;
    
        return unitPrice * quantity;
    }
    
    double sumAmounts(double const amount1, double const amount2, double const amount3)
    {
        double total = amount1 + amount2 + amount3;
        return total;
    }
    
    double calculateDiscount(double const totalPrice)
    {
        if (totalPrice > 10000) {
             return totalPrice * 0.05;
        } else {
             return 0;
        }
    }
    
    double calculatePriceAfterDiscount(double const total, double const dsc)
    {
        return total - dsc;
    }
    
    int main()
    {
        double uPrice1 = 0.0, uPrice2 = 0.0, uPrice3 = 0.0;
        double amount1 = 0.0, amount2 = 0.0, amount3 = 0.0;
        double totalPrice = 0.0, discount = 0.0, priceAfterDiscount = 0.0;
        int qty1 = 0, qty2 = 0, qty3 = 0;
    
        amount1 = askForValues(uPrice1,qty1);
        amount2 = askForValues(uPrice2,qty2);
        amount3 = askForValues(uPrice3,qty3);
    
        totalPrice = sumAmounts(amount1,amount2,amount3);
    
        discount = calculateDiscount(totalPrice);
    
        priceAfterDiscount = calculatePriceAfterDiscount(totalPrice,discount);
    
        std::cout << "Total price after discount is: " << priceAfterDiscount << '\n';
    }
    

    为了进一步改进,尝试将变量存储在某个容器中,如vector . 使用变量amount1,amount2等真的很糟糕,如果你有百万金额怎么办?

相关问题