Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我';我得到了nan的输出?_C++_Nan - Fatal编程技术网

C++ 为什么我';我得到了nan的输出?

C++ 为什么我';我得到了nan的输出?,c++,nan,C++,Nan,我是初学者 我在为一家旅行社做预订系统。除了计算总销售额,一切正常。我认为这是计算总销售额时的某种逻辑错误 我该如何解决这个问题。该程序必须包含选择、循环和函数。请让我容易理解,因为这学期我只学习了上面提到的3个主题 另外,关于包计数器(第56行),我是否可以将其作为一个函数而不是在主中? 我曾尝试将其作为函数,但失败了,然后我决定将其设置为main函数 提前谢谢你 #include <iostream> #include <string> using namespace

我是初学者

我在为一家旅行社做预订系统。除了计算
总销售额
,一切正常。我认为这是计算
总销售额
时的某种逻辑错误

我该如何解决这个问题。该程序必须包含选择、循环和函数。请让我容易理解,因为这学期我只学习了上面提到的3个主题

另外,关于包计数器(
第56行
),我是否可以将其作为一个函数而不是在
中? 我曾尝试将其作为函数,但失败了,然后我决定将其设置为
main
函数

提前谢谢你

#include <iostream>
#include <string>
using namespace std;

string packageName(char);
double calcPrice(char, char, int, int);
double calcTax(double);
double calcDisc(double, double);
double calcTotalPrice(double, double, double);
void displayReceipt(double, double, double, double, string);

void displaySaleSummary(int, int, int, double);

char pCode, meals, decision;
int numAdult, numChild;
string name;

// main function
int main()
{

    double netPrice, taxPrice, discPrice, totalSales, totalPrice;
    int countA = 0, countB = 0, countC = 0;
    string pName;

    //sentinel controlled loop starts here
    while (decision != 'N' && decision != 'n') {

        cout << "\n\t\t\t\tAI TRAVEL AGENCY\n\n";
        cout << "\t\t A. Langkawi Island   \t\t Adult : RM 250 \t Child : RM 200 \n";
        cout << "\t\t B. Pangkor Island    \t\t Adult : RM 350 \t Child : RM 300 \n";
        cout << "\t\t C. Perhentian Island \t\t Adult : RM 450 \t Child : RM 400 \n\n";

        cout << "\t\t Add Meals to the package  \t Adult : RM 100 \t Child : RM 50\n\n\n";

        cout << "Enter customer's name : ";
        cin >> ws;
        getline(cin, name);

        cout << "Enter package code : ";
        cin >> pCode;

        cout << "Enter number of Adult : ";
        cin >> numAdult;

        cout << "Enter number of Children : ";
        cin >> numChild;

        cout << "Meals (Y/N) : ";
        cin >> meals;

        displayReceipt(netPrice, taxPrice, discPrice, totalPrice, pName);

        // this if-else statements counts how many repititions for each selected package
        if (pCode == 'A' || pCode == 'a') {
            countA++;
        }
        else if (pCode == 'B' || pCode == 'b') {
            countB++;
        }
        else if (pCode == 'C' || pCode == 'c') {
            countC++;
        }
        else
            cout << "wrong code for counter";

        totalPrice = calcTotalPrice(discPrice, taxPrice, netPrice);
        totalSales = totalSales + totalPrice;

        cout << "\nEnter 'Y' to proceed next person or 'N' to terminate (Y/N) : "; //sentinel input value
        cin >> decision;
        cout << "*************************************************************************\n\n";
    }
    // sentinelled controlled loop ends here

    displaySaleSummary(countA, countB, countC, totalSales);

    return 0;
}

string packageName(char pCode)
{

    string pName = "";

    if (pCode == 'A' || pCode == 'a')
        pName = "Pulau Langkawi";
    else if (pCode == 'B' || pCode == 'b')
        pName = "Pulau Pangkor";
    else if (pCode == 'C' || pCode == 'c')
        pName = "Pulau Perhentian";
    else
        pName = " ";

    return pName;
}

double calcPrice(char pCode, char meals, int numAdult, int numChild)
{

    int pAdult, pChild, pAdultMeals, pChildMeals;
    double netPrice;

    if (pCode == 'A' || pCode == 'a') {

        pAdult = 250;
        pChild = 200;
    }
    else if (pCode == 'B' || pCode == 'b') {

        pAdult = 350;
        pChild = 300;
    }
    else if (pCode == 'C' || pCode == 'c') {

        pAdult = 450;
        pChild = 400;
    }
    else {
        cout << "\nWrong Package Code";
        netPrice = 0;
        return netPrice;
    }

    switch (meals) {
    case 'Y':
    case 'y':
        pAdultMeals = 100;
        pChildMeals = 50;
        break;
    case 'N':
    case 'n':
        pAdultMeals = 0;
        pChildMeals = 0;
        break;
    default:
        cout << "\nwrong meals code";
        netPrice = 0;
        return netPrice;
    }

    netPrice = (numChild * pChild) + (numAdult * pAdult) + (numChild * pChildMeals) + (numAdult * pAdultMeals);
    return netPrice;
}

double calcTax(double netPrice)
{

    double taxPrice, tax;

    tax = 0.06;
    taxPrice = tax * netPrice;
    return taxPrice;
}

double calcDisc(double taxPrice, double netPrice)
{

    double disc, discPrice;

    if (taxPrice + netPrice >= 1500)
        disc = 0.10;
    else
        disc = 0;

    discPrice = disc * (taxPrice + netPrice);
    return discPrice;
}

double calcTotalPrice(double discPrice, double taxPrice, double netPrice)
{

    double totalPrice;
    totalPrice = (taxPrice + netPrice) - discPrice;
    return totalPrice;
}

void displayReceipt(double netPrice, double taxPrice, double discPrice, double totalPrice, string pName)
{

    netPrice = calcPrice(pCode, meals, numAdult, numChild);
    taxPrice = calcTax(netPrice);
    discPrice = calcDisc(taxPrice, netPrice);
    totalPrice = calcTotalPrice(discPrice, taxPrice, netPrice);
    pName = packageName(pCode);

    cout << "\n\nRECEIPT\n";
    cout << "---------------------------------------------\n";
    cout << "Customer's Name  : " << name << endl;
    cout << "Package Code     : " << pCode << endl;
    cout << "Package Name     : " << pName << endl;
    cout << "Add meals        : " << meals << endl;
    cout << "Price before tax : RM " << netPrice << endl;
    cout << "Total tax        : RM " << taxPrice << endl;
    cout << "Discount         : RM " << discPrice << endl;
    cout << "Total price      : RM " << totalPrice << endl;
    cout << "---------------------------------------------\n";
}

void displaySaleSummary(int countA, int countB, int countC, double totalSales)
{

    cout << "\n\nAI Travel & Tours" << endl;
    cout << "********************************" << endl;
    cout << "Package A   : " << countA << endl;
    cout << "Package B   : " << countB << endl;
    cout << "Package C   : " << countC << endl;
    cout << "Total Sales : " << totalSales << endl;
    cout << "********************************" << endl;
}
#包括
#包括
使用名称空间std;
字符串packageName(char);
双计算器(char,char,int,int);
双倍calcTax(双倍);
双calcDisc(双,双);
双倍计算价格(双倍,双倍,双倍);
作废显示收据(双、双、双、双、字符串);
void displaySaleSummary(int,int,int,double);
字符编码、膳食、决策;
国际努马多,努姆希尔德;
字符串名;
//主要功能
int main()
{
双倍净价、taxPrice、DispPrice、totalSales、totalPrice;
int countA=0,countB=0,countC=0;
字符串pName;
//哨兵控制回路从这里开始
while(decision!=“N”&&decision!=“N”){
禁餐;
显示收据(净价、税价、盘价、总价、pName);
//此if-else语句统计每个选定包的重复次数
如果(pCode=='A'| | pCode=='A'){
countA++;
}
else if(pCode='B'| | pCode=='B'){
countB++;
}
else if(pCode=='C'| | pCode=='C'){
c++;
}
其他的
不能决定;

coutA
NaN
通常由被零除或未初始化的值引起

在本例中是后者。如果编译器没有警告您未初始化的变量,请使用更好的编译器,或者确保为当前编译器启用警告。Visual studio甚至不会编译您的代码:

错误C4700:使用了未初始化的局部变量“totalPrice”
错误C4700:使用了未初始化的局部变量“discPrice”
错误C4700:使用了未初始化的局部变量“taxPrice”
错误C4700:使用了未初始化的局部变量“netPrice”
错误C4700:使用了未初始化的局部变量“totalSales”
将变量传递给函数不会修改调用代码中该变量的值。如果需要此行为,则需要通过引用传递值。因此,需要将
displayReceive
的声明和定义更改为:

void displayReceipt(double& netPrice, double& taxPrice, double& discPrice, double& totalPrice, string& pName) {
这样就去掉了大多数未初始化的变量,只剩下需要初始化为
0
totalSales

double netPrice, taxPrice, discPrice, totalSales = 0, totalPrice;
虽然没有严格的必要性,因为这些值从未读取过,但我建议初始化所有变量,以避免将来代码更改时难以诊断的问题:

double netPrice = 0, taxPrice = 0, discPrice = 0, totalSales = 0, totalPrice = 0;
主要是出于个人偏好,我喜欢单独声明变量,因为这样更容易发现缺少的初始值设定项:

double netPrice = 0;
double taxPrice = 0;
double discPrice = 0;
double totalSales = 0;
double totalPrice = 0;

请尝试摆脱
使用命名空间std的习惯,只需使用前缀来明确使用的内容,并避免使用全局变量,除非您没有其他选择。请提供一个(重点是最小值)在所有输入和预期输出的情况下,编译器应该警告您许多未初始化的变量传递给
DisplayReceive
的大多数变量都未初始化。打开编译器警告!可能您不知道按值传递和按引用传递之间的区别,而是要按引用传递一些变量e、 话虽如此,我认为您应该分离
displayReceivement()
中的功能,以便只显示和创建一些其他函数来进行计算。您将未初始化的值传递给
displayReceivement()
,并在函数内更新这些值的副本,但在main()中它们保持未初始化状态。下面是一个小示例,展示使用未初始化变量的效果: