Thread: error in code

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    error in code

    Code:
    /--------------------------------------------------
    
    #include <stdio.h>       //printf()
    #include <conio.h>       //getch()
    
    //---------------------------------------------------
    
    int getKeyCode(void);   // This function detects if 
                           //  any key was pressed.
    
    //---------------------------------------------------
    
    int main()
    	{
    	int iletter;   
    
        printf (" Please press any key to continue\n");
    	
    	while(1)
    
    		{
    
          iletter = getKeyCode();
    
    		if (iletter != -1)
    
    			{		
    			
    			printf( "The letter pressed is %d\n",iletter);
                printf (" Please press any key to continue\n");
    			}
    			
    		} 
    	return 0;
    	}//end main
    
    
    	// following is the function getKeycode
    
    int getKeyCode(void) // function header
    		{
    
    		int keypress,letter,letterSecond;
    	    int value ;
    
    	keypress = kbhit();
    
    	if(keypress != 0)
    
    		{
    
    		letter = getch();
    
    			if((letter == 0x0) || (letter ==0xE0))
    			{		
    				letterSecond = getch();
    				letterSecond <<= 8;
    				value = (letter | letterSecond);
    		
    		    }
    	   }
    	   
    	
    	else 
    		value = -1;
    	  
    			    
    
    	return value;
    		}// end getKeyCode



    the error message i get is as follow

    Run-Time Check Failure #3 - The variable 'value' is being used without being initialized.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    is this an error? it looks like a warning.
    The variable 'value' is being used without being initialized.
    what does this tell you? it tells you exactly what the problem is. lets pretend we are running the program right now and the value of keypress is not 0. so the control goes to the "if" clause. now lets pretend that the value of "letter = getch()" is '0xA'. the inner if clause will not be executed because the condition is false in this case. there is nothing else left in the outer if clause, so all we do now is 'return value', which hasnt been initialized.

    you are not considering all control paths, in which case you will see unexpected results.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to properly indent your code, e.g.,
    Code:
    //--------------------------------------------------
    
    #include <stdio.h>       //printf()
    #include <conio.h>       //getch()
    
    //---------------------------------------------------
    
    int getKeyCode(void);   // This function detects if
                           //  any key was pressed.
    
    //---------------------------------------------------
    
    int main()
    {
        int iletter;
    
        printf (" Please press any key to continue\n");
    
        while(1)
        {
            iletter = getKeyCode();
    
            if (iletter != -1)
            {
                printf( "The letter pressed is &#37;d\n",iletter);
                printf (" Please press any key to continue\n");
            }
        }
        return 0;
    }//end main
    
    
    // following is the function getKeycode
    
    int getKeyCode(void) // function header
    {
        int keypress,letter,letterSecond;
        int value ;
    
        keypress = kbhit();
    
        if(keypress != 0)
        {
            letter = getch();
    
            if((letter == 0x0) || (letter ==0xE0))
            {
                letterSecond = getch();
                letterSecond <<= 8;
                value = (letter | letterSecond);
            }
        }
        else
            value = -1;
    
        return value;
    }// end getKeyCode
    Now, the flow of logic is more easily seen. It is clear that you have one control path in getKeyCode() where value is not assigned a value and yet is returned. In main(), iletter is always used, so consequently it is possible that iletter would contain garbage and yet be compared to -1.

    If you are wondering what is this control path, then consider what happens in getKeyCode() when keypress != 0 and !((letter == 0x0) || (letter ==0xE0)).

    Incidentally, note that <conio.h> is non-standard, along with getch(), and that the // style of comment was only introduced in the C99 standard (so it is fine to use it, but there may be consequences if you are aiming for your code to be compiled under C89).
    Last edited by laserlight; 09-26-2008 at 11:13 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    still confused

    i understand the problelm is with the if statement in function but i donot how to solve it.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The common way to solve this problem is to ALWAYS set the variable to a value, ideally as an initialization, e.g. "int value = 0;".

    --
    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.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    Thumbs up problem solved

    thank you all of you for help

    the program is working now. the problem was the nested if else statments. this was my first program after a long time.

    thanks again

    c...

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Whenever you see an error like that, rest assured that the compiler did a "run through", to see if your variables have any meaningful values in them at those points where that variable is being called for. It's not always a good guesser though. You may have constructed your code to execute in some non-linear fashion... the variables being nicely assigned lower down in the program, then used near the top - but it may be perfectly normal given the direction of flow you intend. The compiler really only detects problems based on "obvious" top-to-bottom execution flow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM