Visual C++ - Runtime Check Failure #3 - Variable is not initiliazed

24,007

Solution 1

The runtime is telling you the truth, the following line comes after you have declared number as an int but have not given it a value.

 cout << "Enter " << number << " Value Name: ";

In your code you declare the following, in C++ this means give me 2 ints but the values are not defined yet, e.g.

int offset, number;

Change it to something like this ..

int offset = 0;
int number = 0;

Solution 2

You are printing the variable number without assigning to it first, i.e. it's uninitialized. When it prints some random number it's because that what happens to be in the memory at the time you run the program. Assign a value to it before you use it.

Solution 3

The problem is exactly the error message you're getting. You're using the variable number without initializing it.

You use the variable right here, at the top of your loop, when it hasn't been initialized to anything yet:

cout << "Enter " << number << " Value Name: ";

What is your intention with the number variable? It doesn't really seem to be serving any purpose. If you want to print which entry you're currently on, you could use the offset variable instead, like this:

cout << "Enter " << offset << " Value Name: ";

But that still seems a little unclear to me.

But the reason that you're having a problem is because the value is uninitialized, so you're experiencing undefined behavior. This is also the reason that Visual Studio doesn't always catch it; it will probably always catch in Debug mode, but in Release mode it will almost never catch it. You need to initialize all your variables before you use them.

Share:
24,007
Ram Sidharth
Author by

Ram Sidharth

Programming is like mathematics. It's all about logic. If you're good enough to understand the concepts practically and theoretically, there's nothing more fun than programming. I started learning C++ a few months ago, I'm still sulking around in the basics. My end-goal is to get a good grip on important languages like C++, Java, Ruby, Pearl, MySQL, Assembly so that I can develop object oriented software that can help people in their everyday lives. Like for instance, for entertainment purposes or for their businesses and so on and so forth. I also have a deep interest in Physics &amp; Mathematics, Calculus &amp; Trigonometry. I know Motion Graphics including software like Autodesk Combustion, Adobe After Effects CS5 and am currently doing a course in Autodesk Maya for 3D Animation.

Updated on July 07, 2022

Comments

  • Ram Sidharth
    Ram Sidharth almost 2 years

    I use Visual C++ 2010 Express Edition to compile and run the .exe files I write in the C++ programming language. I am trying to create a loop-based logic using C++ to ask the user how many entries he chooses to enter, and ask questions limited to that no. of entries. For example I want to output, "How many characters do you wish to enter?: " Say the user gives the answer as '3' which is stored in the int variable 'entries'. I then want to keep asking the question 3 times before it stops and continues with the next line of code. I hope you understand, here is a block of code to demonstrate what I am doing:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       cout << "How many values do you need to enter?: ";
       int entries;
       cin >> entries;
       int offset, number;
       string valueName[50];
       float valueValue[50];
       for (offset = 0; offset < entries; offset++)
       {
          cout << "Enter " << number << " Value Name: ";
          cin >> valueName[offset];
          cout << "Enter " << valueName[offset] << "\'s value: ";
          cin >> valueValue[offset];
          for (number = 1; number <= entries; number++)
          {
          }
       }
       char response;
       cin >> response;
       return 0;
    }
    

    Strangely when I run this simple program, it fails when I enter the value's name to be inserted into the 0th element of the valueName[] array. It just pauses the execution of the program and a dialog box pops up saying "Runtime Check Failure #3 - Variable 'number' is being used without being initialized!" Another problem regarding this program is that, for quite some time, when I ran this program this "Runtime Check Failure #3" box never appeared, and when it didn't, the number value went wrong, and first started with 1, and then for the next loop jumped to 6, and then repeated 6 again for the next loop!
    Please help me! I've checked online scouring this problem everywhere, but it just doesn't apply to my type of problem!
    Is it because the variables are out of scope?
    But they're declared outside the for loops right?

    So please help me!

    • Mahesh
      Mahesh about 12 years
      If anything is out of scope, compiler isn't happy. If you get run-time errors, meaning you passed the compilation phase. Syntactically everything is correct.
    • Michael Burr
      Michael Burr about 12 years
      If you break into the debugger when you get that message, it lands you right on the line using the uninitialized variable. Also, the build should be giving you a warning: warning C4700: uninitialized local variable 'number' used All of this tells you exactly what and where the problem is.
    • Ram Sidharth
      Ram Sidharth about 12 years
      thanks a lot Mahesh and Michael Burr for your insights, I'm happy to tell you guys that the problem has been resolved thanks to yours and everyone else's cooperation. Thank you very much.
  • Ram Sidharth
    Ram Sidharth about 12 years
    Thanks a lot for your cooperation 'eggbox'. I'm happy to tell you the problem has been resolved thanks to yours and everyone else's comments!
  • Ram Sidharth
    Ram Sidharth about 12 years
    Thank you 'Robert Kelly' for your cooperation in this problem I have been having for some time now. I'm happy to tell that the problem has been successfully resolved thanks to yours and everyone else's comments. Thank you.
  • Ram Sidharth
    Ram Sidharth about 12 years
    What you're saying is right, and thanks to your comment and many others, I have been able to resolve my problem. Thank you very much Joachim Pileborg.