Thread: Noob Help......If/Else :S

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    54

    Noob Help......If/Else :S

    Thought I had it all figured out, but i can't get it to compile properly!! grrrrrrrr!!


    Basically everything is good, it compiles, but then it crashes and says that either 'meter', 'inlet', or 'pressure' is being USED WITHOUT BEING INITIALIZED. So annoying, what does that even mean??


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main (void)
    
    {
        char lights, inlet, pressure;
        int meter;
    	
          
        printf ("\nCheck Your Status Lights, are they Green, Amber, or Red?");
        printf ("\nEnter 'G' for GREEN, 'A' for AMBER, or 'R' for RED ==> \n");
        scanf("%c", &lights);
    
        if (lights == 'G' || lights == 'g')
        {               
            printf ("\nDo Restart Procedure!\n" ); 
        }
        else if (lights == 'A' || lights == 'a')
        {
            printf( "\nCheck Fuel line Service routine\n" );       
        }
        else 
        {
            printf ("\nShut off all input lines and check meter #3\n" );   
            printf ("\n\nPlease Enter the ammount on meter, to the nearest whole number ");
            scanf ("%i", &meter);
        }
        if (meter >= 50)
        {
            printf ("\n\nMeasure Flow Velocity in inlet 2-B");
            printf ("\nIs inlet volocity High, Low or Normal?");
            printf ("\n\n PRESS 'H' for HIGH, 'L' for LOW, or 'N' for NORMAL==> ");
            scanf ("%i", &inlet);
    	  
            if (inlet == 'H' || inlet == 'h' || inlet == 'L' || inlet == 'l')
            {
                printf ("\nREFER UNIT FOR FACTORY SERVICE");
            }
        } 
        else if (inlet == 'N' || 'n')
        {
            printf ("\nRefer to Inlet Service Manual");
        }
    
    	else
        {
            printf ("\n\ncheck main line for test pressure");
            printf ("\nIs main line pressure High, Low or Normal?");
            printf ("\n\n PRESS 'H' for HIGH, 'L' for LOW, or 'N' for NORMAL==> ");
            scanf ("%i", &pressure);
    	  
            if (pressure == 'H' || pressure == 'h' || pressure == 'L' || pressure == 'l')
            {
                printf ("\nREFER TO MAIN LINE MANUEL");
    			_getch();
    			printf("\n\n\nPRESS ANY KEY TO EXIT");
            }
    		
            else
            {
                printf ("\nREFER TO MOTOR SERVICE MANUEL");
    			_getch();
    			printf("\n\n\nPRESS ANY KEY TO EXIT");
            }
        }
    
    }
    Last edited by matt.s; 03-19-2010 at 08:35 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Both inlet and meter are defined as char, but you scanf them as int's
    Code:
    scanf ("%i", &inlet);
    Also you are going to have nelines in your input bufer after the scanf so you will need to get rid of them beofre the next scanf.

    Also your main should have a return at the end of it.

    Dylan

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^^

    Thanks for the tips, I made the corrections, but meter is still coming up as being used without being initalized, and i need it to be an int.....


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main (void)
    
    {
        char lights, inlet, pressure;
        int meter;
    	
          
        printf ("\nCheck Your Status Lights, are they Green, Amber, or Red?");
        printf ("\nEnter 'G' for GREEN, 'A' for AMBER, or 'R' for RED ==> \n");
        scanf("%c", &lights);
    
        if (lights == 'G' || lights == 'g')
        {               
            printf ("\nDo Restart Procedure!\n" ); 
        }
        else if (lights == 'A' || lights == 'a')
        {
            printf( "\nCheck Fuel line Service routine\n" );       
        }
        else 
        {
            printf ("\nShut off all input lines and check meter #3\n" );   
            printf ("\n\nPlease Enter the ammount on meter, to the nearest whole number ");
            scanf ("%i", &meter);
        }
        if (meter >= 50)
        {
            printf ("\n\nMeasure Flow Velocity in inlet 2-B");
            printf ("\nIs inlet volocity High, Low or Normal?");
            printf ("\n\n PRESS 'H' for HIGH, 'L' for LOW, or 'N' for NORMAL==> ");
            scanf ("%c", &inlet);
    	  
            if (inlet == 'H' || inlet == 'h' || inlet == 'L' || inlet == 'l')
            {
                printf ("\nREFER UNIT FOR FACTORY SERVICE");
            }
        } 
        else if (inlet == 'N' || 'n')
        {
            printf ("\nRefer to Inlet Service Manual");
        }
    
    	else
        {
            printf ("\n\ncheck main line for test pressure");
            printf ("\nIs main line pressure High, Low or Normal?");
            printf ("\n\n PRESS 'H' for HIGH, 'L' for LOW, or 'N' for NORMAL==> ");
            scanf ("%c", &pressure);
    	  
            if (pressure == 'H' || pressure == 'h' || pressure == 'L' || pressure == 'l')
            {
                printf ("\nREFER TO MAIN LINE MANUEL");
    			_getch();
    			printf("\n\n\nPRESS ANY KEY TO EXIT");
            }
    		
            else
            {
                printf ("\nREFER TO MOTOR SERVICE MANUEL");
    			_getch();
    			printf("\n\n\nPRESS ANY KEY TO EXIT");
            }
        }
    
    	return 0;
    
    }

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    if (meter >= 50)
    {
    When you reach this code, meter may or may not have a meaningful value.

    I notice you have some serious structure issues with you code (if's and else's in the wrong places). You need to examine it closely, maybe walk through the code by hand, make some flowcharts.
    Last edited by NeonBlack; 03-19-2010 at 11:04 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    You only set a value to meter if lights is not (G,g,A,a) but you always check to see if its > 50.
    If that's what you want just set it to 0 when you initialise it
    Code:
    int meter = 0;
    The complier is being nice, if you do not set it before you use it, it could have ANY value.

    Dylan

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yeh - you have made a few mistakes with stucturing. I imagine that figuring this stuff out is the purpose of the exercise though, so don't worry

    If it were me I'd step through it with a debugger and try to see what is actually happening. To point you in the right direction:

    Code:
        if (lights == 'G' || lights == 'g')  // if 'g'
    .....
     else // equivalent to if not g or a 
        {
            printf ("\nShut off all input lines and check meter #3\n" );   
            printf ("\n\nPlease Enter the ammount on meter, to the nearest whole number ");
            scanf ("%i", &meter);
         ^^^ this line is only executed if "r" was input
        } // end of else statement
    
        if (meter >= 50)
       ^^^ this line is always executed
        {
            printf ("\n\nMe
    You might have meant to put the "if" inside the braces of the "else". This means the (if meter > 50) code is only executed if the 'else' statement is true.

    If you google "nested if" you should find some resources. This page looked reasonable: Nested if and if-else statements

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Quote Originally Posted by smokeyangel View Post

    You might have meant to put the "if" inside the braces of the "else". This means the (if meter > 50) code is only executed if the 'else' statement is true.

    This is what i'd like to do, only have it execute if else is true, i've moved the open brace, but for the life of me I can't figure out where to put the close brace for that statement :S

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main (void)
    
    {
        char lights, inlet, pressure;
        int meter;
    	
          
        printf ("\nCheck Your Status Lights, are they Green, Amber, or Red?");
        printf ("\nEnter 'G' for GREEN, 'A' for AMBER, or 'R' for RED ==> \n");
        scanf("%c", &lights);
    
        if (lights == 'G' || lights == 'g')
        {               
            printf ("\nDo Restart Procedure!\n" ); 
        }
        else if (lights == 'A' || lights == 'a')
        {
            printf( "\nCheck Fuel line Service routine\n" );       
        }
        else 
        {
            printf ("\nShut off all input lines and check meter #3\n" );   
            printf ("\n\nPlease Enter the ammount on meter, to the nearest whole number ");
            scanf ("%i", &meter);
     
    		 if (meter >= 50)
        {
            printf ("\n\nMeasure Flow Velocity in inlet 2-B");
            printf ("\nIs inlet volocity High, Low or Normal?");
            printf ("\n\n PRESS 'H' for HIGH, 'L' for LOW, or 'N' for NORMAL==> ");
            scanf ("%c", &inlet);
    	  
            if (inlet == 'H' || inlet == 'h' || inlet == 'L' || inlet == 'l')
            {
                printf ("\nREFER UNIT FOR FACTORY SERVICE");
            }
        } 
        else if (inlet == 'N' || 'n')
        {
            printf ("\nRefer to Inlet Service Manual");
        }
    
    	else
        {
            printf ("\n\ncheck main line for test pressure");
            printf ("\nIs main line pressure High, Low or Normal?");
            printf ("\n\n PRESS 'H' for HIGH, 'L' for LOW, or 'N' for NORMAL==> ");
            scanf ("%c", &pressure);
    	  
            if (pressure == 'H' || pressure == 'h' || pressure == 'L' || pressure == 'l')
            {
                printf ("\nREFER TO MAIN LINE MANUEL");
    			_getch();
    			printf("\n\n\nPRESS ANY KEY TO EXIT");
            }
    		
            else
            {
                printf ("\nREFER TO MOTOR SERVICE MANUEL");
    			_getch();
    			printf("\n\n\nPRESS ANY KEY TO EXIT");
            }
        }
    
    	return 0;
    
    }

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    54

    Thats exactly it, thanks for the effort, i really appreciate it, and see where I made errors, you sir are a godsend!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    magestrium: << !! Posting Code? Read this First !! >>
    Then don't use void main: SourceForge.net: Void main - cpwiki
    Oh and basically, initialize means giving a variable an initial value (otherwise they have junk). So when the compiler says you're using a variable without initializing it, it means you try to read from a variable without first giving it an initial value (ie not initialized).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with if/else and switch commands please
    By masterofwar14 in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2009, 12:28 AM
  2. Noob to programming need help with a project
    By Wheelsonbus in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 03:46 AM
  3. Noob in need of help.
    By gec5741 in forum C++ Programming
    Replies: 18
    Last Post: 01-23-2009, 03:25 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 05:53 AM