Thread: program error!

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    program error!

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	char a, b;
    	clrscr();
    
    	printf("LATEST  NEWS \n type W for weather or S for Sports or L for local");
    	scanf("%c", &a);
    	
    	if (a == 'W')
    		{	printf("\nSunny!");
    	}
    	else if (a == 'L')
    	{
    		printf("\nMan gets head stuck in rabbit hole");
    	}
    	else (a == 'S')
    	{
    		printf("\nNo matches currently. \nPress y to check\n");
    		scanf("%d",&b);
    		if (b == 'y')
    		printf("Don't be impatient!");
    			
    	}
    
    }

    I keep getting errors. Is the if...else statement wrong? If you have any ideas or answers, please help.
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,738
    The final else doesn't have a condition.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Okay, so I've changed the final else to else if but it still doesn't print "Don't be impatient " when I hit y. Is it because I used a if statement within a if... else?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by [David] View Post
    Okay, so I've changed the final else to else if but it still doesn't print "Don't be impatient " when I hit y. Is it because I used a if statement within a if... else?
    show code

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you're telling the user to enter a letter, but you're attempting to read a number (%d) from the user. that's not going to work.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Gotcha thanks! Check out the updated code.

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    Updated code

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	char a, b,c;
    	clrscr();
    
    	printf("LATEST  NEWS \n type W for weather or S for Sports or L for local\n");
    	scanf("%c", &a);
    	
    	if (a == 'W' || a == 'w')
    		{	printf("\nSunny!");
    	}
    	else if (a == 'L' || a=='l')
    	{
    		printf("\nMan gets head stuck in rabbit hole");
    	}
    	else if (a == 'S' || a=='s')
    	{
    		printf("\nNo matches currently. \nPress y to check\n");
    		scanf("%c",&b);
    		if (b == 'y' || b=='Y' )
    		printf("Don't be impatient!");
    			
    	}
    	printf("\nDo you want to check the News again?(y/n)");

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,496
    You probably should add a space in front of your input to ignore any leading whitespace characters.

    Code:
    scanf(" %c", &b); // Note the space before the "%c".
    Jim

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    Your problem should be solved by the above answers. I just wanted to add a little extra by saying that you should probably add an else statement at the end in case the user enters a key that is "invalid".

    Code:
        char a, b;    
        clrscr();
    
    
        printf("LATEST  NEWS \nType W for weather or S for Sports or L for local\n");
        scanf("%c", &a);
        getchar();
    
    
        if (a == 'W' || a == 'w')
            {   printf("\nSunny!");
        }
        else if (a == 'L' || a == 'l')
        {
            printf("\nMan gets head stuck in rabbit hole");
        }
        else if (a == 'S' || a == 's')
        {
            printf("\nNo matches currently. \nPress y to check\n");
            scanf("%c",&b);
            if (b == 'Y' || b == 'y')
                printf("Don't be impatient!");
    
    
        }
        else
            printf("Invalid input!\n");
    
    }
    This makes the program more robust because you never know what the user inputs. Its best to cover all grounds.

  10. #10
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Thanks @Salem, @Elkvis, @africanwizz, @jimblumberg and @Kotik. You guys are swell! The program works just about perfect. Just one more idea to implement: I tried using a while loop to give the user a chance to repeat the program but it didn't work. I'll post the code...

  11. #11
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	char a, b,c;
    	clrscr();
    
    
    	printf("LATEST  NEWS \n type W for weather or S for Sports or L for local\n");
    	scanf("%c", &a);
    	
    	if (a == 'W' || a == 'w')
    		{	printf("\nSunny!");
    	}
    	else if (a == 'L' || a=='l')
    	{
    		printf("\nMan gets head stuck in rabbit hole");
    	}
    	else if (a == 'S' || a=='s')
    	{
    		printf("\nNo matches currently. \nPress y to check\n");
    		scanf(" %c",&b);
    		if (b == 'y' || b=='Y' )
    		printf("Don't be impatient! This isn't a game you know!");
    			
    	}
    	else 
    	printf("\nInvalid input!!!");
    	printf("\n\nDo you want to check the News again?(y/n)\n");
    	scanf(" %c",&c);
    
    }
    The last scanf is to accept input to maybe run a loop through the whole program again.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    i don't see any while loops there , nevertheless i think a do-while loop will be a better option in this case. You should also take a look at this

  13. #13
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Here it is:
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	char a, b,c,d;
    	clrscr();
    
    do {
    	
    
    	printf("LATEST  NEWS \n type W for weather or S for Sports or L for local\n");
    	scanf("%c", &a);
    	
    	if (a == 'W' || a == 'w')
    	
    		{	printf("\nSunny!");
    	}
    	
    	else if (a == 'L' || a=='l')
    	
    	{
    		printf("\nMan gets head stuck in rabbit hole");
    	}
    	
    	else if (a == 'S' || a=='s')
    	
    	{
    		printf("\nNo matches currently. \nPress y to check\n");
    		scanf(" %c",&b);
    		
    		if (b == 'y' || b=='Y' )
    		
    		printf("Don't be impatient! This isn't a game you know!");
    			
    	}
    	else 
    	
    	printf("\nInvalid input!!!");
    	
    	printf("\n\nDo you want to check the News again?(y/n)\n");
    scanf(" %c",&c);
    } while (c=='y' || c=='Y');
    
    }
    The variable c accepts 'y' or 'n' and the do loop runs through the block again but the value given for var c is taken for var a also and so I get "Invalid input!!! "
    Is it possible to reset the var a and var c?

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by [David] View Post
    The variable c accepts 'y' or 'n' and the do loop runs through the block again but the value given for var c is taken for var a also and so I get "Invalid input!!! "
    Is it possible to reset the var a and var c?
    You are misunderstanding what is happening. If you were to print a and c when it said invalid input, you would see they are not the same.

    You forgot the space before the "%c" on line 12, so the newline in the input buffer after they answer Y/N to check the news again is still there, and that is what is being read into a. Add that space and it should fix it.

    EDIT: Please make sure you post your code with decent indentation/formatting in the future, it makes it much easier for us to read/follow it and thus help you.
    Last edited by anduril462; 03-31-2014 at 08:17 PM.

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    replace your line 12 with
    Code:
    scanf(" %c", &a);
    notice the space before %c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  3. C Program... Program or Compiler error?
    By Zemira in forum C Programming
    Replies: 13
    Last Post: 12-02-2009, 08:59 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. i'm trying to fix an error with my error messaging on my program
    By RancidWannaRiot in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2003, 01:02 PM

Tags for this Thread