-2

I'm getting an error saying that:

error c4700: unitialized local variable 'aCount' used

(as well as bCount,cCount,dCount,fCount). This is a example from C++ How to Program: Late Objects Version (7th Edition) by Deitel and Deitel I copied verbatim, don't see why its not working) Please help. Thank you.

edit: thank you everyone for your responses!

#include <iostream>
using namespace std;

int main()
{
    int grade;
    int aCount;
    int bCount;
    int cCount;
    int dCount;
    int fCount;

    cout << "Enter the letter grades." << endl
    << "Enter the EOF character to end input." << endl;


    while ((grade = cin.get()) != EOF)
    {

        switch (grade)
        {
        case 'A':
        case 'a':
            aCount++;
            break;

        case 'B':
        case 'b':
            bCount++;
            break;

        case 'C':
        case 'c':
            cCount++;
            break;

        case 'D':
        case 'd':
            dCount++;
            break;

        case 'F':
        case 'f':
            fCount++;
            break;

        case '\n': 
        case '\t':
        case ' ':
            break;

        default: 
            cout << "Incorrect letter grade entered."
                << "Enter a new grade." << endl;
            break;

        }
    }

    cout << "\n\nNumber of students who received each letter grade:"
        << "\nA: " << aCount
        << "\nB: " << bCount
        << "\nC: " << cCount
        << "\nD: " << dCount
        << "\nF: " << fCount

        << endl;

}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Sagistic
  • 37
  • 1
  • 8
  • 4
    To fix put `int aCount = 0;` Learn to read your compiler errors and warnings well! – πάντα ῥεῖ Jun 30 '14 at 03:03
  • 3
    The error message is clear. Just initialize them. – Yu Hao Jun 30 '14 at 03:03
  • 3
    Get rid of that book. – Casey Jun 30 '14 at 03:19
  • @casey - I would.. it's just that it's a class book unfortunately. http://www.amazon.com/How-Program-Objects-Version-Edition/dp/0132165414/ref=sr_1_1?ie=UTF8&qid=1404098840&sr=8-1&keywords=C%2B%2B+late+objects+version – Sagistic Jun 30 '14 at 03:29
  • 1
    +1 considering this was taken from a book which should be teaching the OP the correct way to program it seems unfair to downvote. Other people may also face this same problem. It may make more sense to give more details such the book name, chapter etc... in the question text itself so that it is easier to search and may prevent repeats of this question. – Shafik Yaghmour Jun 30 '14 at 04:05

2 Answers2

4
int aCount;

This declares aCount to be an integer, as you probably knew. However, since the variable has automatic storage duration (like most non-global and non-static variables), the value it has is unknown and a program that assumes it has any meaningful value is buggy. In fact, using the value can cause unrelated parts of your code to stop working. This is all summed up nicely as undefined behaviour.

Now the next time you do anything with this variable is in the switch:

aCount++;

This does a few things:

  1. Read the value of aCount. The program is now instantly buggy.
  2. Increment that value.
  3. Store the incremented value in aCount.

Even if that case never happens, you again read from aCount later when outputting it. This requires reading the value, which, again, makes the program buggy. All bets are off here and it can do whatever it wants.

This is the case for the other counts as well. Your compiler is trying to help you by telling you that you're doing some dangerous stuff. To fix it, give the variables an initial value:

int aCount = 0;
//etc

†This can have specific exceptions.

Community
  • 1
  • 1
chris
  • 60,560
  • 13
  • 143
  • 205
  • Thank you, but it's just odd because I copied this directly from a book example, and they don't initialize the variable to any value (ie 0). – Sagistic Jun 30 '14 at 03:14
  • However, when I do initialize, it works completely fine. Maybe the book forgot, and is wrong. Thanks for you're detailed answer. – Sagistic Jun 30 '14 at 03:15
  • @user1222049, That's one red flag to get a new book. If the example was to demonstrate the effects of uninitialized variables, it's too long and off-topic imo. – chris Jun 30 '14 at 03:15
  • Is [this where you copied it from](http://udel.edu/~caviness/Class/CISC181-03F/deitel-files/ch02/Fig02_22.cpp)? If so, maybe learn to copy better, unless the book doesn't match up with the code. – Crowman Jun 30 '14 at 03:25
  • @PaulGriffiths, Thought you were talking to me for a moment O_o Come to think of it, though, I remember this example. I should have recognized the book. – chris Jun 30 '14 at 03:25
  • @PaulGriffiths - Nope, I copied it from http://www.amazon.com/How-Program-Objects-Version-Edition/dp/0132165414/ref=sr_1_1?ie=UTF8&qid=1404098840&sr=8-1&keywords=C%2B%2B+late+objects+version same author, just probably a different book. I copied it correctly, it just seems that the book forgot to put that in. -_- – Sagistic Jun 30 '14 at 03:27
  • @user1222049: I don't have the book, but printing errors happen, I guess. – Crowman Jun 30 '14 at 03:28
  • +1 may be helpful to include a link to the [wikipedia article on UB](http://en.wikipedia.org/wiki/Undefined_behavior). – Shafik Yaghmour Jun 30 '14 at 04:08
  • @ShafikYaghmour, Done. And thank you very much for your Q&A that I spent forever trying to find. – chris Jun 30 '14 at 04:09
1

Put

int aCount = 0;

or

int aCount; 
aCount = 0;

Both will work!!

JustStarted
  • 77
  • 1
  • 10