变量未在C++代码中初始化
Variable not initialized in C++ code?
编辑:非常感谢大家的回答,我现在知道这是一个愚蠢的问题,但我在掌握了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;
}
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 初始化正在影响与初始化无关的代码
- 开始多线程代码之前的初始化
- 在静态初始化期间运行代码
- 获取有关使用未初始化内存的错误代码
- 聚合初始化,clang想让我破坏代码吗
- 在运行时,何时完全初始化 std 库才能在不破坏代码的情况下使用它?
- 初始化代码的组织
- 初始化不可移动对象数组:为什么这样的代码无法在 GCC 上编译?
- 此代码中的数组初始化样式是什么?这是标准的吗?
- 为什么 GCC 6.3 在没有显式 C++11 支持的情况下编译此大括号初始化列表代码
- 程序告诉我初始化在编写简单的C ++代码时不需要初始化的变量
- 在通用模板代码中初始化聚合成员
- 如何初始化共享复杂初始化代码的多个常量成员变量?
- 为什么单一实例必须在以下代码中初始化
- 在构造函数代码之前禁用默认类成员初始化
- 关于c++静态初始化代码限制
- 将数组/矩阵初始化代码从C++转换为C#
- 从静态初始化代码启动CUDA内核时遇到问题
- *数组[]未初始化-代码分析警告C6001