变量未在C++代码中初始化

Variable not initialized in C++ code?

本文关键字:初始化 代码 C++ 变量      更新时间:2023-10-16

编辑:非常感谢大家的回答,我现在知道这是一个愚蠢的问题,但我在掌握了Visual Basic之后一直试图进入C++,所以很多事情我都不熟悉。我保证我不会只是要求他们浪费你的时间。

我一直在尝试用C++做一个示例项目来学习基础知识,但我对当前代码有问题。该程序应该将用户输入的变量相加,并考虑到他们输入的月份来应用不同的费用。然而,它说我的变量usageCost还没有初始化。这是代码:

#include <iostream>
using namespace std;
int main()
{
    string monthtext;
    double month,
        days,
        hours,
        kwhr,
        completeCharge,
        energyadjustCharge;
    const double flatservicecharge = 5.31;
    cout << "Enter the month (1-12) :";
    cin >> month;
    cout << "Number of days in the period :";
    cin >> days;
    cout << "Kilowatt hours used :";
    cin >> hours;
    if (month = 1){
        monthtext = "January";
    }
    else{
        if (month = 2){
            monthtext = "February";
        }
        else{
            if (month = 3){
                monthtext = "March";
            }
            else{
                if (month = 4){
                    monthtext = "April";
                }
                else{
                    if (month = 5){
                        monthtext = "May";
                    }
                    else{
                        if (month = 6){
                            monthtext = "June";
                        }
                        else{
                            if (month = 7){
                                monthtext = "July";
                            }
                            else{
                                if (month = 8){
                                    monthtext = "August";
                                }
                                else{
                                    if (month = 9){
                                        monthtext = "September";
                                    }
                                    else{
                                        if (month = 10){
                                            monthtext = "October";
                                        }
                                        else{
                                            if (month = 11){
                                                monthtext = "November";
                                            }
                                            else{
                                                if (month = 12){
                                                    monthtext = "December";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    double usageCost;
    if (month <= 5 && month >= 10 && hours <= 500){
        usageCost = hours * 12.9266;
    }
    if (month <= 5 && month >= 10 && hours > 500){
        usageCost = 500 * 12.9266 + (hours - 500) * 10.9917;
    }
    if (month >= 6 && month <= 9 && hours <= 750){
        usageCost = 750 * 12.9266;
    }
    if (month >= 6 && month <= 9 && hours > 750){
        usageCost = 750 * 12.9266 + (hours - 750) * 14.2592;
    }
    energyadjustCharge = hours * .1305;
    completeCharge = usageCost + flatservicecharge + energyadjustCharge;
    cin.get();
    return 1;       
} 

您声明:

double usageCost;

但是正如您所看到的,您没有将它设置为任何值,因此它是非序列化的。

编译器正确。

解决方案:
尝试:

  double usageCost = 0.0;

编辑1:

您的程序的其他问题:

分配使用"=",比较使用"=="
请相应地更改您的所有if语句。

用数组查找替换if语句
添加以下内容将简化您的程序:

const std::string month_names[] =
{"No month index 0",
 "January", "February", "March", "April",
 "May", "June", "July", "August",
 "September", "October", "November", "December"
};
// ...
month_text = month_names[month];

错误是因为您有一些从未设置usageCost的代码路径。与其通过初始化usageCost来隐藏错误,不如修复您的代码,因为您的代码在所有情况下都确实打算给usageCost一个值。

事实上,您没有初始化usageCost是很好的,因为这意味着您得到了一个编译器警告,提醒您注意后面的逻辑错误。


month <= 5 && month >= 10从来都不是真的。没有一个月是在六月之前,也没有一个月份是在九月之后。你可能是指(month <= 5 || month >= 10)。括号很重要,因为后面还有另一个&&条件。

然而,最好避免代码重复,并通过重写该部分来避免四种情况中没有一种被输入的可能性:

if ( month >= 6 && month <= 9 )
{
    if ( hours > 750 )
        usageCost = ......;
    else
        usageCost = ......;
}
else
{
    if ( hours > 500 )
        usageCost = ......;
    else
        usageCost = ......;
}

在这种结构下,CCD_ 10不可能不被分配一些东西。

您已将month定义为double

double month;
cout << "Enter the month (1-12) :";
cin >> month;

如果我为这个输入键入5.5,您可以看到usageCost永远不会初始化为任何值。

double usageCost;
// This will evaluate to false
if (month <= 5 && month >= 10 && hours <= 500){
    usageCost = hours * 12.9266;
}
// This will evaluate to false
if (month <= 5 && month >= 10 && hours > 500){
        usageCost = 500 * 12.9266 + (hours - 500) * 10.9917;
    }
// This will evaluate to false
if (month >= 6 && month <= 9 && hours <= 750){
    usageCost = 750 * 12.9266;
        }
// This will evaluate to false
if (month >= 6 && month <= 9 && hours > 750){
                usageCost = 750 * 12.9266 + (hours - 750) * 14.2592;
            }

考虑:

  • usageCost初始化为默认值
  • 对于仅为"整数"的值,使用int或类似值
  • 使用CCD_ 17作为CCD_ 18的真逆

我为您修复了许多问题。

#include <iostream>
using namespace std;
int main()
{
    string monthtext;
    double month,
        days,
        hours,
        kwhr,
        completeCharge,
        energyadjustCharge;
    const double flatservicecharge = 5.31;
    cout << "Enter the month (1-12) :";
    cin >> month;
    cout << "Number of days in the period :";
    cin >> days;
    cout << "Kilowatt hours used :";
    cin >> hours;
    struct {int input; string month;} Calendar[] = {
        { 1, "January"   },
        { 2, "February"  },
        { 3, "March"     },
        { 4, "April"     },
        { 5, "May"       },
        { 6, "June"      },
        { 7, "July"      },
        { 8, "August"    },
        { 9, "September" },
        { 10, "October"  },
        { 11, "November" },
        { 12, "December" } };
    for(int i = 0; i < ARRAY_SIZE(Calendar); ++i)
    { 
        if (month == Calendar[i].input)
        {
            monthtext = Calendar[i].month;
            break;
        }
    }
    double usageCost = 0.0;
    if (month <= 5 && month >= 10 && hours <= 500)
    {
        usageCost = hours * 12.9266;
    }
    if (month <= 5 && month >= 10 && hours > 500)
    {
        usageCost = 500 * 12.9266 + (hours - 500) * 10.9917;
    }
    if (month >= 6 && month <= 9 && hours <= 750)
    {
        usageCost = 750 * 12.9266;
    }
    if (month >= 6 && month <= 9 && hours > 750)
    {
        usageCost = 750 * 12.9266 + (hours - 750) * 14.2592;
    }
    energyadjustCharge = hours * .1305;
    completeCharge = usageCost + flatservicecharge + energyadjustCharge;
    cin.get();
    return 0;       
}