簡體   English   中英

為什么我在 output 得到 nan?

[英]Why I'm getting nan at the output?

初學者在這里。

我正在為一家旅行社做一個預訂系統。 除了totalSales的計算,一切都很好。 我相信計算totalSales是某種邏輯錯誤。

我該如何解決這個問題。 該程序必須包含選擇、循環和函數。 請讓我容易理解,因為這個學期,我只學習了上面提到的 3 個主題。

另外,關於 package 計數器( line 56 ),我可以將其作為 function 而不是main的嗎? 我曾嘗試將其設為 function 但失敗了,然后我決定放入main function。

先感謝您!

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

NaN通常由除以零或未初始化的值引起。

在這種情況下是后者。 如果您的編譯器沒有警告您未初始化的變量,請使用更好的編譯器或確保為您當前的編譯器啟用警告。 Visual Studio 甚至不會編譯您的代碼:

error C4700: uninitialized local variable 'totalPrice' used
error C4700: uninitialized local variable 'discPrice' used
error C4700: uninitialized local variable 'taxPrice' used
error C4700: uninitialized local variable 'netPrice' used
error C4700: uninitialized local variable 'totalSales' used

將變量傳遞給 function 不會修改調用代碼中該變量的值。 如果您想要這種行為,您需要通過引用傳遞您的值。 因此,您需要將displayReceipt的聲明和定義更改為:

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

這消除了大多數未初始化的變量,只剩下需要初始化為0totalSales

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM