实际初始化的未初始化的局部变量?

Meg*_*ney -3 c++ visual-c++

所以我正在编写一个处理输入文件中的值的程序.我的变量包括总数,taxtotal,小计等等.它们已经被声明和初始化了,但是我收到两条错误消息:"未初始化的局部变量'小计'使用过'"和变量"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;
}
Run Code Online (Sandbox Code Playgroud)

任何提示将非常感谢!

Kei*_*son 7

在这个声明中:

double price, taxtotal, subtotal, total = 0;
Run Code Online (Sandbox Code Playgroud)

类型名称double适用于所有4个变量,但= 0初始化仅适用于total.

正如其他人所说,最直接的解决方法是:

double price = 0, taxtotal= 0, subtotal = 0, total = 0;
Run Code Online (Sandbox Code Playgroud)

但是在自己的行上声明每个变量是更好的风格:

double price    = 0.0;
double taxtotal = 0.0;
double subtotal = 0.0;
double total    = 0.0;
Run Code Online (Sandbox Code Playgroud)

请注意,使用0完全有效(该int值将隐式转换为double0.0),但使用浮点常量更明确.

(我选择垂直对齐初始化器.有些人可能不愿意这样做.)

我猜你还没有指点.当你这样做时,你会遇到另一个在自己的行上声明每个变量的理由.这个:

int* x, y, z;
Run Code Online (Sandbox Code Playgroud)

定义x为一个int*,但yz作为int.对于上面的初始化程序,每行使用一个声明可以避免错误和混淆的机会:

int* x;
int* y;
int* z;
Run Code Online (Sandbox Code Playgroud)

一旦你得到你的代码进行编译,你就会遇到这个问题:

name, price, isTaxed = 0;
Run Code Online (Sandbox Code Playgroud)

这是一个有效的陈述,但它没有做你认为它做的.

,逗号运算符.它按顺序计算其左右操作数,并产生右操作数的值,丢弃左操作数的值.声明评估和丢弃的当前值name,然后计算并丢弃的当前值price,然后将值0isTaxed.(感谢user4581301指出这一点.)

你可以这样写:

name = price = isTaxed = 0;
Run Code Online (Sandbox Code Playgroud)

(因为赋值产生了分配的值),或者更简单地说,如下:

// name = 0;
price = 0.0
isTaxed = false;
Run Code Online (Sandbox Code Playgroud)

我已将注释注释掉name,因为它是一个数组而你无法为数组对象赋值.我不会显示更正版本,因为我不知道你在这里要做什么.

建议:从小处开始,保持简单,并在添加新代码之前在每个步骤确认代码是否正常工作.我想你曾经尝试过多写代码.你有近100行代码甚至无法编译.我已经编程长时间了,如果不确定编译和运行,我就不会编写那么多代码.


小智 5

看起来您在此处的声明 subtotal中声明:

double price, taxtotal, subtotal, total = 0;
Run Code Online (Sandbox Code Playgroud)

但只用值 0初始化 total,导致在赋值的右侧使用它触发错误:

subtotal = subtotal + price;
Run Code Online (Sandbox Code Playgroud)

要初始化多个项目,只需明确添加“=”。例子:

double price = 0, taxtotal = 0, subtotal = 0, total = 0;
Run Code Online (Sandbox Code Playgroud)