实际初始化的未初始化的局部变量?
所以我正在写一个处理输入文件中的值的程序。我的变量包括total,taxtotal,subtotal等。&它们已经被声明和初始化,但我收到两条错误消息:“未初始化的局部变量”使用的小计“和变量”taxtotal“相同。 的源代码:实际初始化的未初始化的局部变量?
#include "stdafx.h"
#include<stdio.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ifstream shoppingBasketFile;
shoppingBasketFile.open("HW3_Data.txt");
bool isTaxed = false;
char taxValue = 0;
char inPrice[64];
char name[128];
double price, taxtotal, subtotal, total = 0;
if (shoppingBasketFile.is_open())
{
// display the header info:
cout << "o Thank you for shopping at StuffMart" << endl;
cout << setw(3) << left << "o "
<< setw(20) << left << "Item"
<< setw(12) << "Unit Price"
<< setw(4) << "Tax"
<< endl
<< "o -------------------------------------" << endl;
// parse the input file until end of file;
while (!shoppingBasketFile.eof())
{
// parse the item name:
shoppingBasketFile >> name;
cout << "Name = " << name << endl;
if (name == NULL)
{
// what should we really do here?
continue;
}
// parse the price:
shoppingBasketFile >> price;
if (price < 0 || price > 100000000000) {
continue;
}
cout << "Price = " << price << endl;
// parse the isTax flag:
shoppingBasketFile >> isTaxed;
shoppingBasketFile >> taxValue;
cout << "Is taxed? = " << taxValue << endl;
// if end of file break out of this loop:
if (!shoppingBasketFile.good()) break;
if (isTaxed == true) {
taxtotal = taxtotal + (.085 * price);
taxValue = 'Y';
}
else {
taxValue = 'N';
}
//display tax as Y instead of T/1
if (isTaxed == true) {
cout << "Tax: Y" << endl;
}
else {
cout << "Tax: N" << endl;
}
//compute the subtotals
subtotal = subtotal + price;
// display the item info:
cout << "name" << name << ", price: $" << price << ", is taxed: " << taxValue << endl;
// reset input values:
name, price, isTaxed = 0;
// end of while loop
}
//compute the final total:
total = subtotal + taxtotal;
//output the totals
cout << "o" << setw(37) << "---------------" << endl
<< "o " << setw(26) << "Subtotal $" << fixed << setprecision(2) << right << subtotal << endl
<< "o " << setw(26) << "Tax (8.5%) $" << fixed << setprecision(2) << right << taxtotal << endl
<< "o " << setw(26) << "Total $" << fixed << setprecision(2) << right << total << endl;
}
shoppingBasketFile.close();
return 0;
}
任何提示将不胜感激!
看起来你宣布在发言这里subtotal
:
double price, taxtotal, subtotal, total = 0;
但只有初始化total
值为0,导致其在赋值的右侧使用触发错误:
subtotal = subtotal + price;
要初始化多个项目,只需显式添加“=”。 例子:
double price = 0, taxtotal = 0, subtotal = 0, total = 0;
这有效!我其实曾经尝试过这一点,但是我又遇到了另一个阻碍我获得足够结果的错误。对C++还是很新的,所以谢谢你帮我排除故障! :) –
在此声明:
double price, taxtotal, subtotal, total = 0;
类型名称double
适用于所有4个变量,但= 0
初始化仅适用于total
。
正如其他人所说,最直接的解决方法是:
double price = 0, taxtotal= 0, subtotal = 0, total = 0;
但最好的风格声明对自己的行每个变量:
double price = 0.0;
double taxtotal = 0.0;
double subtotal = 0.0;
double total = 0.0;
注意,使用0
是完全合法的( int
值将被隐式转换为double
值0.0
),但使用浮点常量更加明确。
(我选择了垂直对齐的初始化。有些人可能不喜欢这样做。)
我猜你没有得到的指针呢。当你这样做时,你会遇到另一个原因来声明每个变量在自己的行上。此:
int* x, y, z;
x
定义为一个int*
,但y
和z
为int
。采用每行一个声明,如对上述初始化,避免了这个机会,错误和混乱:
int* x;
int* y;
int* z;
一旦你的代码编译,你必须与该行的一个问题:
name, price, isTaxed = 0;
这是一个有效的陈述,但它不会做你认为它的作用。
,
是逗号运算符。它按顺序评估它的左和右操作数,并产生右操作数的值,丢弃左操作数的值。该声明评估并丢弃当前值name
,然后评估并丢弃price
的当前值,然后将值0
指定为isTaxed
。 (感谢user4581301指出这一点。)
你可以这样写:
name = price = isTaxed = 0;
(因为转让产生已分配的值),或者更简单地说,如:
// name = 0;
price = 0.0
isTaxed = false;
我已将注释分配给name
,因为它是一个数组,并且您不能将值分配给数组对象。我不会显示正确的版本,因为我不知道你在这里做什么。
建议:从小开始,保持简单,并在每一步确认您的代码在添加新代码之前工作。我想你已经试过一次写太多的代码。你有近100行代码甚至不能编译。我一直在编程一个long的时间,我不会编写那么多的代码,但不确保它编译和运行。
_“它们已被声明并初始化”_这些变量未被初始化。 –
请停止尝试通过试错来学习C++,它会让你无处可去。相反,从一本好书中系统地学习它。 –
请停止假设我如何学习C++,假设会让你无处可去。我其实是在上一堂课,指的是一本好书,并有两位导师。不过谢谢你。 –