【问题标题】:C++ Output Error?C++ 输出错误?
【发布时间】:2017-10-27 07:38:56
【问题描述】:

我已经编写了这个语句。

  1. 编写一个程序来获取 3 件商品的单价和数量。程序应输出总计的小计。如果总数大于 10,000.00,应给予 5% 的折扣。使用具有返回类型的函数,如下所述。 一种。名称:输入,返回类型:void 说明:取单价的价值和一件物品的数量。 湾。名称:calculateSubTotal,返回类型:double 描述:计算特定项目的小计。 C。名称:打印 返回类型:无效 说明:应打印最后一张账单,包括小计、总计和折扣
#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.

【问题讨论】:

  • 使用调试器逐行执行代码时,您观察到了什么?所有变量都具有预期值吗?
  • 您的主要问题是没有阅读和遵循说明。按照说明做,不要即兴发挥。

标签: c++ debugging output


【解决方案1】:

它不适合您,因为您的某些变量(如 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);
}

最好尝试从函数返回值,并尽量不要重复你的代码,稍微改进的代码: #包括

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';
}

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

【讨论】:

  • 你救了我!非常感谢。非常感谢兄弟!
猜你喜欢
  • 2014-05-17
  • 2013-05-26
  • 2013-06-23
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多