debugging: where are the variables?

Let me start by saying that I am a beginning C++ programmer and am very new to Visual Studio 2005, so please forgive me if this is a silly question. When running the debugger on a simple program such as the one listed below, I find that it doesn't bother to display anything for most of the variables, and even for the variables it does display for, it seems to be garbage.

#include <iostream>
using namespace std;

void main()
{
const int arraySize = 12;
double sales[arraySize] = {0};
int count = 0;
double smallestSales = 0;
int smallestSalesMonth = 0;

for (count = 0; count < arraySize; ++count)
{
cout << "Enter sales data for month " << count + 1 << ": ";
cin >> sales[count];

if (count == 0)
smallestSales = sales[count];

if (sales[count] < smallestSales)
{
smallestSales = sales[count];
smallestSalesMonth = count;
}
}

cout << "\n" << "The smallest array element is " << smallestSalesMonth << "\n\n";
}

For example, after entering a value of "3" for sales[0] the first time through the for loop, the debugger displays the value as "5.307579804306e-315#DEN". Huh Also, smallestSales is the only other variable the debugger lists, and it also has a garbage value, regardless of the value it actually holds. arraySize, count, and smallestSalesMonth seem to be completely ignored (even though they're used and the program works fine).

Am I completely missing something As a novice programmer it is very frustrating to not be able to follow the changes in variable data through even a simple program like this. Also, why is it that I have to manually "turn on" the debugging information for every single project I start

Any help would be appreciated!


Answer this question

debugging: where are the variables?

  • C Ruybal

    HI i dont know what settings do you have in ur project properties, try with a new project --> win32 console application --> application setting --> console application replace default code with your above code

    hope this will work

    regards

    Manoj


  • rronny

    Hmm, strange problem. First off, do a Build + Clean, just in case the debug info is messed up somehow. Ensure you're looking at the Debug + Windows + Locals window. If you start debugging and step into the main() function, you'll see garbage values until you step through the variable initialization statements.

    Sorry, this is probably not much help...



  • Serge A. G.

    I get the feeling you put #include"stdafx.h" after the line #include<iostream>. It's a common problem (and also occurs in VC6).

    To solve the stdafx.h error, make sure the #include"stdafx.h" line is the first one included. Any line before #include "stdafx.h" will be ignored.

    As for the missing symbols, I would make sure that all C/C++ optimisations are disabled (Project Properties -> C/C++ Optimization -> set to Disabled). Importantly, the omit frame pointers option should be disabled. It might also be a good idea to check the linker -> Optimization options to see if optimisations are being enabled there.

    Finally, you may want to fiddle with the Debugging section of the Tools -> Options Dialog.



  • javier27

    Throw a '#include <iostream>' into the code as well.

    I'd recommend reading this thread for a list of C++ books.



  • That Tim Guy

    n0n4m3 wrote:
    Hi,
    this is because your project settings are set to not generate any debug info. Check if you don't have the project in release configuration (in the toolbar you should see a dropdown/combobox that shows release or debug). Normally, the release configuration settings are set to not generate any debug information.

    If you're running in debug configuration and you still can't see the correct values in the debugger, the go to your project settings: right click in your project's name in the solution explorer and choose properties. Next
    - In the configuration choose debug.
    - In linker->debugging->generate debug info choose YES (/Debug)

    Hope this helps.


    Unfortunately every "config" setting I can find is already set to debug. In properties I have the following:

    C/C++ -> General -> Debug Information Format = Program Database (/Zi)
    Linker -> Debugging -> Generate Debug Info = Yes (/DEBUG)

    I have to set those two options manually for every project I start, otherwise it won't even try to debug - I get some sort of "binary was not built with debug information" message. With these settings, debug works, but it still has the problems I mentioned in the first post.

  • Russell Hunter

    Hi,
    this is because your project settings are set to not generate any debug info. Check if you don't have the project in release configuration (in the toolbar you should see a dropdown/combobox that shows release or debug). Normally, the release configuration settings are set to not generate any debug information.

    If you're running in debug configuration and you still can't see the correct values in the debugger, the go to your project settings: right click in your project's name in the solution explorer and choose properties. Next
    - In the configuration choose debug.
    - In linker->debugging->generate debug info choose YES (/Debug)

    Hope this helps.


  • satishD

    Hi,
    it's very strange that the debugger shows the values messed up even after you have the settings for creating debug information. Like others suggested, a clean solution/project should solve that. The problems you mentioned when trying to compile a new project, if you post the code and the errors we should be able to give you a bit more of information.


  • Stephen Joseph Brown

    For what it's worth, here's a screenshot of the code from my initial post running in debug mode right before the final cout statement (with fewer array values), just in case there's something blindingly obvious I'm missing (or in case I'm looking in the wrong place). You can see the values I entered in the program window. Shouldn't there be values listed for arraySize, count, and smallestSalesMonth in the debug window The values that do show up clearly have very weird values.

    http://saru_san.tripod.com/vs-shot.html

    As for not getting it to compile, here is what happens: I start a new project (Win32 Console Application) and replace the default code with the code in the first post. The first time through I get this:

    fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source

    So I throw a '#include "stdafx.h"' into the code and try again. This time I get:

    error C2871: 'std' : a namespace with this name does not exist
    error C2065: 'cout' : undeclared identifier
    error C2065: 'cin' : undeclared identifier

    Again, I appreciate any help you can offer!

  • Arshad98766

    kefka95 wrote:
    It could be that all I needed was the #include"stdafx.h" line to begin with and not the Win32 Console Application part, but I don't know how to make it work without creating a Win32 Console Application. If I try to stick it in a blank project I get a "fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory"

    You'll notice that when you create a new Win32 console application project, it adds a number of "benign" files, including a readme, a free cpp file with an empty main, and a file called stdafx.h.

    These files are not added when you create a blank project, so when you throw in a #include "stdafx.h", it won't be found in your project, and complain that it can't find this non-existant header.

    kefka95 wrote:
    I'm still not clear on why the debug couldn't just work with a Blank Project, but this is good to know and certainly better than nothing. Thanks again to all who helped!

    Aha! So it was a blank project that caused it!

    I believe I know why a blank project does not work well with the debugger: optimisations are enabled!

    Notice in project properties that C/C++ -> Optimization -> Optimization is set to "Maximize speed (/O2)". For debugging to work, this should be set to "Disabled (/Od)". A Debug build with optimisations enabled is just a synonym for a Release build (okay, I am over-generalising, but it does rearrange the local variables). For the same reason debugging a release build is problematic, debugging an optimised debug build is problematic.

    If you want my opinion, I feel this is a bug. When you create a blank project, you should expect its debug configuration to be set up for debugging, with optimisations disabled. The fact that it isn't, is a bug.



  • Bhaskar Thamma

    Thanks for trying to help, but I'm still not getting anywhere. I tried the suggestion of starting a new "standard" project, but I don't even know how to get it to compile (unexpected end of file, no namespace called 'std', cout and cin don't work, etc). I tried cleaning it too, with no better results. If it would help, I could upload a screenshot of exactly what it does when it's debugging.

    With any luck I can find a computer at school that still has Visual Studio 6.0 loaded, as I don't remember the older version being anywhere near this complicated : X

  • salours

    Thank you so much, placing the #include"stdafx.h" at the top solved everything - it compiles, and even the debug works correctly now! So, to sum up, I had to use a Win32 Console Application rather than a Blank Project, and then make sure that #include"stdafx.h" was placed at top, and now everything seems to be as it should :P

    It could be that all I needed was the #include"stdafx.h" line to begin with and not the Win32 Console Application part, but I don't know how to make it work without creating a Win32 Console Application. If I try to stick it in a blank project I get a "fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory"

    I'm still not clear on why the debug couldn't just work with a Blank Project, but this is good to know and certainly better than nothing. Thanks again to all who helped!


  • debugging: where are the variables?