我的c++能耗计算器计算有错误

问题描述 投票:0回答:1

我的问题是,即使我的计算是正确的,它显示的输出也是错误的。这里唯一正确的是“小时”,但“日、周、月”输出是错误的,它总是达不到正确的答案。

const double FACTOR_RATE = 0.0097822; // PHP per watt-hour

case 1:
    watts = 3950;
    applianceName = "A/C Floor Stand non inverter 4.5 HP";
    cout << "You chose 1. A/C Floor Stand non inverter 4.5HP 3950 watts" << endl;
    break;

costPerHour = watts * FACTOR_RATE;             // PHP per hour
costPerDay = costPerHour * hoursPerDay;        // PHP per day
costPerWeek = costPerDay * daysPerWeek;        // PHP per week
costPerMonth = costPerWeek * weeksPerMonth;    // PHP per month


这是我的代码的完整视图。

#include <iostream>
#include <iomanip>
#include <string>
#include <limits> // For numeric_limits

using namespace std;

// Function to validate integer input within a specific range
int validateInput(int min, int max, const string& prompt) {
    int value;
    while (true) {
        cout << prompt;
        cin >> value;

        // Check for input failure (e.g., non-integer input)
        if (cin.fail()) {
            cin.clear(); // Clear the error flag
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
            cout << "Invalid input. Please enter an integer value." << endl;
            continue;
        }

        // Check if the input is within the valid range
        if (value < min || value > max) {
            cout << "Invalid input. Please enter a value between " << min << " and " << max << "." << endl;
            continue;
        }

        // Valid input
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard any remaining input
        return value;
    }
}

int main() {
    // Constants
    const double FACTOR_RATE = 0.0097822; // PHP per watt-hour

    // Variables for login
    string username, password;

    // Variables for menu choices
    char mainChoice;
    int categoryChoice;

    // Variables for appliance details
    double watts = 0.0;
    string applianceName = "";

    // Variables for usage inputs
    int hoursPerDay, daysPerWeek, weeksPerMonth;

    // Variables for cost calculations
    double costPerHour, costPerDay, costPerWeek, costPerMonth;

    // ------------------------------
    // Simple Login Section
    // ------------------------------
    cout << "Welcome to Meralco Electricity Calculator" << endl;
    cout << "Log In" << endl;
    cout << "Enter your username: ";
    cin >> username;
    cout << "Enter your password: ";
    cin >> password;
    cout << "-----------------------------" << endl;
    cout << "Hello, " << username << "!" << endl << endl;

    // ------------------------------
    // Main Menu Section
    // ------------------------------
    cout << "To begin the calculation, choose one of the categories below." << endl;
    cout << "A. Airconditioner" << endl;
    cout << "B. Computer and Gadgets" << endl;
    cout << "C. Kitchen Appliances" << endl;
    cout << "Enter your choice (A/B/C): ";
    cin >> mainChoice;
    cout << endl;

    // Convert mainChoice to uppercase for consistency
    mainChoice = toupper(mainChoice);

    // ------------------------------
    // Category Selection Section
    // ------------------------------
    switch (mainChoice) {
    case 'A':
        cout << "You chose Airconditioner" << endl;
        cout << "1. A/C Floor Stand non inverter 4.5HP 3950 watts" << endl;
        cout << "2. A/C Floor Split type inverter 1.00 HP 800 watts" << endl;
        cout << "3. A/C Floor Split type inverter 1.50 HP 1100 watts" << endl;
        cout << "4. A/C Floor Split type inverter 2.0 HP 1500 watts" << endl;
        cout << "5. A/C Floor Split type inverter 2.50HP 2300 watts" << endl;
        cout << "Enter the airconditioner category (1-5): ";
        cin >> categoryChoice;
        cout << endl;

        switch (categoryChoice) {
        case 1:
            watts = 3950;
            applianceName = "A/C Floor Stand non inverter 4.5 HP";
            cout << "You chose 1. A/C Floor Stand non inverter 4.5HP 3950 watts" << endl;
            break;
        case 2:
            watts = 800;
            applianceName = "A/C Floor Split type inverter 1.00 HP";
            cout << "You chose 2. A/C Floor Split type inverter 1.00 HP 800 watts" << endl;
            break;
        case 3:
            watts = 1100;
            applianceName = "A/C Floor Split type inverter 1.50 HP";
            cout << "You chose 3. A/C Floor Split type inverter 1.50 HP 1100 watts" << endl;
            break;
        case 4:
            watts = 1500;
            applianceName = "A/C Floor Split type inverter 2.0 HP";
            cout << "You chose 4. A/C Floor Split type inverter 2.0 HP 1500 watts" << endl;
            break;
        case 5:
            watts = 2300;
            applianceName = "A/C Floor Split type inverter 2.50HP";
            cout << "You chose 5. A/C Floor Split type inverter 2.50HP 2300 watts" << endl;
            break;
        default:
            cout << "Enter correct number." << endl;
            return 0;
        }
        break;

    case 'B':
        cout << "You chose Computer and Gadgets" << endl;
        cout << "1. Printer 478 watts" << endl;
        cout << "2. Router 110 watts" << endl;
        cout << "3. Scanner 11 watts" << endl;
        cout << "4. Laptop 60 watts" << endl;
        cout << "5. Desktop Standard 65 watts" << endl;
        cout << "Enter the computer or gadget category (1-5): ";
        cin >> categoryChoice;
        cout << endl;

        switch (categoryChoice) {
        case 1:
            watts = 478;
            applianceName = "Printer";
            cout << "You chose 1. Printer 478 watts" << endl;
            break;
        case 2:
            watts = 110;
            applianceName = "Router";
            cout << "You chose 2. Router 110 watts" << endl;
            break;
        case 3:
            watts = 11;
            applianceName = "Scanner";
            cout << "You chose 3. Scanner 11 watts" << endl;
            break;
        case 4:
            watts = 60;
            applianceName = "Laptop";
            cout << "You chose 4. Laptop 60 watts" << endl;
            break;
        case 5:
            watts = 65;
            applianceName = "Desktop Standard";
            cout << "You chose 5. Desktop Standard 65 watts" << endl;
            break;
        default:
            cout << "Enter correct number." << endl;
            return 0;
        }
        break;

    case 'C':
        cout << "You chose Kitchen Appliances" << endl;
        cout << "1. Induction cooker 1500 watts" << endl;
        cout << "2. Refrigerator French 193 watts" << endl;
        cout << "3. Rice cooker 3L 1000 watts" << endl;
        cout << "4. Washing Machine FrontLoad 2000 watts" << endl;
        cout << "5. Smart LED 15 watts" << endl;
        cout << "Enter the kitchen appliance category (1-5): ";
        cin >> categoryChoice;
        cout << endl;

        switch (categoryChoice) {
        case 1:
            watts = 1500;
            applianceName = "Induction cooker";
            cout << "You chose 1. Induction cooker 1500 watts" << endl;
            break;
        case 2:
            watts = 193;
            applianceName = "Refrigerator French";
            cout << "You chose 2. Refrigerator French 193 watts" << endl;
            break;
        case 3:
            watts = 1000;
            applianceName = "Rice cooker 3L";
            cout << "You chose 3. Rice cooker 3L 1000 watts" << endl;
            break;
        case 4:
            watts = 2000;
            applianceName = "Washing Machine FrontLoad";
            cout << "You chose 4. Washing Machine FrontLoad 2000 watts" << endl;
            break;
        case 5:
            watts = 15;
            applianceName = "Smart LED";
            cout << "You chose 5. Smart LED 15 watts" << endl;
            break;
        default:
            cout << "Enter correct number." << endl;
            return 0;
        }
        break;

    default:
        cout << "Enter correct number." << endl;
        return 0;
    }

    cout << "-----------------------------" << endl;

    // ------------------------------
    // Input Usage Details Section
    // ------------------------------
    // Validate hours per day (0-24)
    hoursPerDay = validateInput(0, 24, "Enter the number of hours used per day: ");

    // Validate days per week (1-7)
    daysPerWeek = validateInput(1, 7, "Enter the number of days used per week (1-7): ");

    // Validate weeks per month (1-4)
    weeksPerMonth = validateInput(1, 4, "Enter the number of weeks used per month (1-5): ");
    cout << "-----------------------------" << endl << endl;

    // ------------------------------
    // Cost Calculation Section
    // ------------------------------
    // Calculate cost based on watt-hours
    costPerHour = watts * FACTOR_RATE;             // PHP per hour
    costPerDay = costPerHour * hoursPerDay;        // PHP per day
    costPerWeek = costPerDay * daysPerWeek;        // PHP per week
    costPerMonth = costPerWeek * weeksPerMonth;    // PHP per month

    // ------------------------------
    // Output Results Section
    // ------------------------------
    cout << fixed << setprecision(2);
    cout << "Appliance: " << applianceName << endl;
    cout << "Wattage: " << watts << " watts" << endl;
    cout << "Usage: " << hoursPerDay << " hours/day for " << daysPerWeek << " days/week and " << weeksPerMonth << " weeks/month" << endl;
    cout << "-----------------------------" << endl;
    cout << "Cost per hour: PHP " << costPerHour << endl;
    cout << "Cost per day: PHP " << costPerDay << endl;
    cout << "Cost per week: PHP " << costPerWeek << endl;
    cout << "Cost per month: PHP " << costPerMonth << endl;

    return 0;
}


我尝试过的样品是

电器:1.空调落地式非逆变器4.5HP 3950瓦 小时:6 天:7 周:4

它始终显示的输出是

时间:38.64 // 唯一正确的答案 // 日:231.84 周:1622.87 月份:6491.47

正确的输出和我期望的是

时间:38.64 日:231.87 周:1623.06 月份:6492.24

c++
1个回答
0
投票

您的代码没有错误...

程序执行后,您将看到以下变量

watts = 3950
FACTOR_RATE = 0.0097822
hoursPerDay = 6

因此,对于以下行,

costPerDay = 231.838
四舍五入为
costPerDay = 231.84

cout << fixed << setprecision(2);

为什么你期望不同的结果?

注意:请不要在多个函数中使用

cin
,请在
main
中使用它来获取所需的所有用户输入并仅调用函数进行计算(单一责任原则)。

© www.soinside.com 2019 - 2024. All rights reserved.