Thread: newbie here

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    2

    newbie here

    Hey, guys, I'm new to c programming. I seem to get the program working but for some reason, my else statement is showing up when I end the program. Any help is appreciated.

    Code:
    #include<stdio.h>
    int main( void )
    {
    int temp;
    
    
    do{
    printf("Enter the temperature(-99 to stop): ");
    scanf("%d",&temp);
    
    
    if(temp >= 85){
        printf("its warm\n");
    }
    else if(temp >= 60 && temp <= 84){
        printf("its pleasant\n");
    }
    else
    {
    printf("its a cold day\n");
    }
    }
    while (temp != -99);
    {
    printf("close");
    }
    }

  2. #2
    Guest
    Guest
    If you're asking why it prints "its a cold day" (that should be it's btw) when entering -99, that's because the else condition is reached (and matches) before the while statement.

    Make sure to format your code in the future for others to read, e.g.:
    Code:
    int main(void)
    {
        int temp;
        do {
            printf("Enter the temperature(-99 to stop): ");
            scanf("%d", &temp);
    
            if (temp >= 85) {
                printf("it's warm\n");
            } else if (temp >= 60 && temp <= 84) {
                printf("it's pleasant\n");
            } else {
                printf("it's a cold day\n");
            }
        } while (temp != -99);
    
        { // <-- superfluous braces
            printf("close");
        } // ^
    }

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    2
    Thank you, Adrian, That pointed me in the right direction. I will make sure to format my code better next time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello, I am a newbie and so I have a newbie question;)
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 12-20-2012, 10:59 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. Newbie help
    By J2ke in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2006, 05:11 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie need help
    By Sunset in forum C Programming
    Replies: 3
    Last Post: 09-08-2002, 03:32 PM

Tags for this Thread