Thread: program ends after else statement is run. Help a beginner.

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    6

    program ends after else statement is run. Help a beginner.

    In my code, if it runs an else statement, then the code ends despite there being other code below that I want to run. This only happens when it runs else statements, not if and else if statements. Is there any way to fix or stop this from happening?

    code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    	printf ("the numbers are %d, %d, %f \n" ,3,4,4.5);
    	printf ("the character is %c \n", 'h');
    	int age;
    		printf ("what is your age? \n");
    		scanf ("%d",&age);
    	if (age == 17) {
    		printf ("You are as old as me! \n");
    	
    	}
    	else if (age < 17) 
    	{
    		printf ("You are younger than me! \n");
    			if (age == 16)
    			{
    				printf ("You can drive. \n");
    			}
    					else 
    					{
    						printf ("You are younger than me and cant drive.");
    					}
    	}
    	else if (age > 100)
    	{
    		printf ("Youre probally lying. \n");
    	}
    	else {
    		printf ("Your older than me! \n");
    		if (age < 21)
    		{
    			printf ("youre older than me, but not 21 \n");
    		
    		}
    			else 
    			{
    				printf (" Your older than me and 21 years! \n");
    			}
    	int a;
    		printf (" Enter an integer between 1 and 10. \n");
    		scanf ("%d", &a);
    	int b;
    		printf ("Enter another number between 1 and 10. \n");
    		scanf ("%d", &b);
    	int c;
    		c= (a>b) ? a : b;
    	printf ("c = %d \n", c);
    	}
    	return 0;
    }
    Last edited by Salem; 01-05-2018 at 01:25 AM. Reason: Added code tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    Well fixing your indentation might bring enlightenment.
    It's an eyesore.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what codes that ends a program in c language?
    By endrick in forum C Programming
    Replies: 2
    Last Post: 01-08-2016, 07:26 AM
  2. Replies: 4
    Last Post: 10-14-2012, 03:15 AM
  3. Want User Input but Program Ends
    By completenewbieu in forum C Programming
    Replies: 5
    Last Post: 08-29-2011, 04:10 PM
  4. Program ends unexpectedly
    By keelhauled in forum C Programming
    Replies: 3
    Last Post: 10-12-2007, 05:41 PM
  5. program ends immediately
    By willc0de4food in forum Windows Programming
    Replies: 11
    Last Post: 09-04-2005, 06:52 PM

Tags for this Thread