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

时间:2015-02-17 22:56:24

标签: c++ variables

编辑:非常感谢你的回答,我现在知道这是一个愚蠢的问题,但我在尝试使用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;       
} 

4 个答案:

答案 0 :(得分:1)

你声明:

double usageCost;

但正如您所看到的,您不会将其设置为任何值,因此它是未初始化的。

编译器是正确的。

<强>解决方案:
尝试:

  double usageCost = 0.0;

修改1:

您的计划的其他问题:

作业使用&#34; =&#34;,比较使用&#34; ==&#34;
请相应更改所有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];

答案 1 :(得分:1)

错误是因为您有一些从未设置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 = ......;
}

使用这种结构,usageCost不可能被赋予某种东西。

答案 2 :(得分:0)

您已将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或类似的值,只会是“整数”。
  • 使用month > 5作为month <= 5
  • 的真实倒数

答案 3 :(得分:-1)

我为你解决了很多问题。

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