Thread: goto statement interpretation in VC 6 and 7

  1. #1
    Registered User
    Join Date
    Feb 2005

    goto statement interpretation in VC 6 and 7


    Recently I use a goto control in the following block.
    float fInput = -1;
    do {
    cin >> fInput;
    if (fInput == 0) goto EXIT_;
    if (fInput > 0)
    cout << " Invalid input, try again: $";
    } while (fInput < 0);
    unsigned uYears = -1;
    do {
    cin >> uYears;
    if (uYears == 0) goto EXIT_;
    if (uYears > 0)
    cout << " Invalid input, try again: ";
    } while (uYears < 0);
    Notice that
    unsigned uYears = -1;
    is after the first do while(){} loops containing
    goto EXIT_;

    Well, VC7 never complains anything but VC6 says initialization 'uYears' is skipped by the goto statement. My initial intension was to put all relevant code together as close as where it will be used. I can see why VC6 errors out that the variable 'uYears' is skipped. But Why do I bother to care about its initialization if other code ahead of it failed and lead to exit. ie., I really want to skip initialize it.

    My solution to this in VC6 is to bring the 'unsigned uYears = -1;' before ALL goto statement. But why VC7 took it? Did VC7 fix the VC6 difficiency or break? That is also interesting to me.

    Ant takers?
    Last edited by sean; 02-28-2005 at 10:34 PM. Reason: Improper usage of code tags

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Seattle, WA
    First of all, putting your entire post inside code tags is pretty much useless. Second, I don't know the answer to your question, but it is better to avoid goto's if possible. I can't tell from the code you gave, but it's quite possible there's a better way than using a goto statement.

    Oh, and you shouldn't try to assign a negative (signed) number to an unsigned variable
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Review the correct usage of code tags in the sticky concerning them - I edited your post to make it more readable.

    I don't see where the label "EXIT_" is in your code, so it's a bit hard to understand your problem. Furthermore, the use of goto is looked down upon in C-circles - there is almost always a better solution. Try to rearrange your code using more modern constructs such as functions (returning a value instead of going to EXIT_ perhaps?), etc...

Popular pages Recent additions subscribe to a feed