Stupid problem

This is a discussion on Stupid problem within the C Programming forums, part of the General Programming Boards category; Hi, I don't get what is happening with this code. Code: // BEGIN DEBUG iNTies=0; printf("iNTies: %d\n", iNTies); if (iNTies!=0) ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    8

    Post Stupid problem

    Hi,
    I don't get what is happening with this code.
    Code:
    	// BEGIN DEBUG
    	iNTies=0;
    	printf("iNTies: %d\n", iNTies);
    	if (iNTies!=0) {
    		printf("Checked all players, player %d has winned", WinningPlayer);
    		AskForAnyKeyStroke();
    	} else {
    		printf("We have a tie\n");
    		AskForAnyKeyStroke();
    	}
    	// END DEBUG
    Unbelivable, the output of the program is:
    Code:
    iNTies: 0
    We have a tie
    Any idea? I really don't get it, iNTies was previously declared as an int (it is a local variable of a function).
    TIA & Regards ...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure your results match what the code should be outputting - unless my brain has turned into mashed potato in the last 15 seconds.

    Can you explain what you EXPECTED it to do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The output is saying that iNTies is 0. Then you have an if-statement which says that if iNTies is *not* 0, print a grammatically incorrect message about one player winning (it's "won"). Otherwise, i.e. if iNTies *is* 0, print that you have a tie, which is exactly what's happening.

    Where's the problem?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    You set iNTies to 0. Nothing in your code modifies the value. If iNTies is a variable from another part of the program, you would need to call that value instead of using one you've placed here.

    I'm new to this, so anyone please correct me if I'm wrong.

    Sorry, CornedBee. I was typing my answer and must have posted right after you. You said pretty much what I meant by my own statements above. And thank God someone else besides me gets "itchy" at grammatical errors.
    Last edited by Dogmasur; 08-23-2008 at 07:59 AM.
    "The art of living is more like wrestling than dancing." - Marcus Aurelius

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Strip the other stuff out, and you basically have...

    Code:
    /* no, this equates to false */
    if(0!=0)
    {
        printf("Checked all players, player %d has winned", WinningPlayer);
    
    /* ie, is 0 == 0? Well, der */
    }else{
        printf("We have a tie\n");
    }

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    Quote Originally Posted by matsp View Post
    I'm pretty sure your results match what the code should be outputting - unless my brain has turned into mashed potato in the last 15 seconds.

    Can you explain what you EXPECTED it to do?

    --
    Mats
    Fing unreal, have been stuck for almost a day with this :-)
    It was supposed to check for ==0 for a tie, not for !=0.
    So, it seems that it was my brain that was turned into a mashed potato, for almost a whole day.
    Thanks

  7. #7
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by Adrian20XX View Post
    It was supposed to check for ==0 for a tie, not for !=0.
    Thanks
    Won't iNTies always be 0 with the code you have written? Regardless of '==' or '!=', you will always get one statement and never the other, unless you include something that will modify iNTies value or call it with a function from somewhere else in your program that we don't see here.
    "The art of living is more like wrestling than dancing." - Marcus Aurelius

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21