-3

I am writing a program that deals with values in an input file. My variables include total, taxtotal, subtotal, etc. & they have already been declared and initialized, yet I am getting two error messages: "uninitialized local variable 'subtotal' used" and the same for the variable "taxtotal".

Here is my source code:

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

How can I eliminate these error messages? I am using Microsoft's Visual C++ compiler, if that matters.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 3
    _"they have already been declared and initialized"_ These variables aren't initialized. – πάντα ῥεῖ Sep 27 '16 at 23:39
  • Also see http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – M.M Sep 27 '16 at 23:40
  • Just as a future debugging tip, you can explore these kinds of issues by removing bits of the program one-by-one until the error goes away (or you're left with a small snippet). This helps remove extraneous bits of code, which is probably the source of downvotes on your question. E.g.: http://coliru.stacked-crooked.com/a/5411adedef6dce85. If you don't know the answer at this point, then feel free to ask a question now that you have an [MVCE](http://stackoverflow.com/help/mcve). – GManNickG Sep 27 '16 at 23:50
  • Nothing after the line `double price, taxtotal, subtotal, total = 0;` is relevant to your question. It's best to narrow down your code to a small self-contained example that exhibits the problem. Read this: [mcve]. – Keith Thompson Sep 27 '16 at 23:54
  • You will find you have the same problem here: `name, price, isTaxed = 0;`. Read up on the comma operator in your text. More information here: http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work/19198977#19198977 – user4581301 Sep 27 '16 at 23:56

2 Answers2

9

In this declaration:

double price, taxtotal, subtotal, total = 0;

the type name double applies to all 4 variables, but the = 0 initialization applies only to total.

As others have said, the most direct fix is:

double price = 0, taxtotal= 0, subtotal = 0, total = 0;

but it's better style to declare each variable on its own line:

double price    = 0.0;
double taxtotal = 0.0;
double subtotal = 0.0;
double total    = 0.0;

Note that using 0 is perfectly valid (the int value will be implicitly converted to the double value 0.0), but using a floating-point constant is more explicit.

(I've chosen to align the initializers vertically. Some might prefer not to do that.)

I'm guessing you haven't gotten to pointers yet. When you do, you'll encounter another reason to declare each variable on its own line. This:

int* x, y, z;

defines x as an int*, but y and z as int. Using one declaration per line, as for the initializers above, avoids this opportunity for error and confusion:

int* x;
int* y;
int* z;

Once you get your code to compile, you'll have a problem with this line:

name, price, isTaxed = 0;

That's a valid statement, but it doesn't do what you think it does.

, is the comma operator. It evaluates its left and right operands in order, and yields the value of the right operand, discarding the value of the left operand. The statement evaluates and discards the current value of name, then evaluates and discards the current value of price, then assigns the value 0 to isTaxed. (Thanks to user4581301 for pointing this out.)

You could write this as:

name = price = isTaxed = 0;

(since an assignment yields the value that was assigned) or, more simply, as:

// name = 0;
price = 0.0
isTaxed = false;

I've commented out the assignment to name, since it's an array and you cannot assign a value to an array object. I won't show a corrected version because I don't know what you're trying to do here.

Suggestion: Start small, keep it simple, and confirm at each step that your code works before adding new code. I think you've tried to write too much code at once. You have nearly 100 lines of code that won't even compile. I've been programming for a long time and I wouldn't write that much code without making sure it compiles and runs.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
5

It looks like you declared subtotal in your statement here:

double price, taxtotal, subtotal, total = 0;

but only initialized total with value 0, causing its use on the right side of the assignment to trigger the error:

subtotal = subtotal + price;

To initialize multiple items simply add the "=" explicitly. Example:

double price = 0, taxtotal = 0, subtotal = 0, total = 0;
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
a5t
  • 81
  • 6
  • That works! I actually did try that at one point but I had another error that got in the way of me getting adequate results. Still very new to C++ so thank you for helping me troubleshoot! :) – Megan McKinney Sep 27 '16 at 23:46