Thread: Else If Statements & Conditionals

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    6

    Else If Statements & Conditionals

    Hello, everyone! I'm new to C and am trying to make a program test certain conditions before printing certain text (see below). The question is, how and where do I add "else" statement if a user:

    - Types in a number that is not in range as shown, or they type in a letter or symbol instead of a number

    Obviously, my coding could be better. If there's a faster way to do what I want the program to do, then please let me know.

    Any help would be greatly appreciated.

    Code:
    #include <stdio.h>
    
    int main() {
    
    int timeOfDay, skyCondition; 
    
    printf("\nWhat time of day is it now?\n"); 
    printf("1 = Morning, 2 = Noon, 3 = Afternoon, 4 = Night\n"); 
    scanf(" %d", &timeOfDay); 
    
    printf("\nWhat's the sky condition right now?\n"); 
    printf("1 = Bright, 2 = Cloudy, 3 = Rainy\n"); 
    scanf(" %d", &skyCondition); 
    
    // Morning Choices
    if (timeOfDay == 1 && skyCondition == 1) {
    printf("\nWe recommend using the following: Morning 1\n\n”); 
    }
    else if (timeOfDay == 1 && skyCondition == 2) {
    printf("\nWe recommend using the following: Morning 2\n\n”); 
    }
    else if (timeOfDay == 1 && skyCondition == 3) {
    printf("\nWe recommend using the following: Morning 3\n\n”); 
    }
    /* else {
    printf("You did not make a selection. Try again.\n"); 
    } */
    
    // Noon Choices
    if (timeOfDay == 2 && skyCondition == 1) {
    printf("\nWe recommend using the following: Noon 1\n\n”); 
    }
    else if (timeOfDay == 2 && skyCondition == 2) {
    printf("\nWe recommend using the following: Noon 2\n\n”); 
    }
    else if (timeOfDay == 2 && skyCondition == 3) {
    printf("\nWe recommend using the following: Noon 3\n\n”); 
    }
    /* else {
    printf("You did not make a selection. Try again.\n"); 
    } */
    
    // Afternoon Choices
    if (timeOfDay == 3 && skyCondition == 1) {
    printf("\nWe recommend using the following: Afternoon 1\n\n”); 
    }
    else if (timeOfDay == 3 && skyCondition == 2) {
    printf("\nWe recommend using the following: Afternoon 2\n\n”); 
    }
    else if (timeOfDay == 3 && skyCondition == 3) {
    printf("\nWe recommend using the following: Afternoon 3\n\n”); 
    }
    /* else {
    printf("You did not make a selection. Try again.\n"); 
    } */
    
    // Night Choices
    if (timeOfDay == 4 && skyCondition == 1) {
    printf("\nWe recommend using the following: Night 1\n\n”); 
    }
    else if (timeOfDay == 4 && skyCondition == 2) {
    printf("\nWe recommend using the following: Night 2\n\n”); 
    }
    else if (timeOfDay == 4 && skyCondition == 3) {
    printf("\nWe recommend using the following: Night 3\n\n”); 
    }
    /* else {
    printf("You did not make a selection. Try again.\n"); 
    } */
    
    return 0; 
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Without turning your code on its head, you might want to test the choices as the user enters them rather than waiting until the end (it's rather annoying to the user to be told the first question was answered incorrectly after he's answered additional questions)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    The scanf functions are hard to use correctly, and even then, they don't protect against numeric overflow.

    Code:
    int myNumber;
    int valid = 0;
    do {
      int r = scanf("%d",&myNumber);
      if ( r == EOF ) break;
      if ( r == 1 ) {
        // read an integer successfully
        if ( myNumber >= 1 && myNumber <= 4 )
          valid = 1;  // success
      } else {
        int ch;
        // throw away junk until the next newline or EOF
        while ( (ch=getchar()) != EOF && ch != '\n' );
      }
    } while ( !valid );
    It's a lot of code, so make it a function of some sort.
    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. Need some help about if statements and conditionals
    By aether in forum C Programming
    Replies: 4
    Last Post: 07-26-2015, 12:57 AM
  2. Constructors and Conditionals
    By marcoesteves in forum C++ Programming
    Replies: 4
    Last Post: 07-14-2014, 04:34 AM
  3. conditionals
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 07:29 AM
  4. circle and conditionals
    By a1pro in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2005, 02:05 AM
  5. Conditionals
    By uniqueniq in forum C Programming
    Replies: 6
    Last Post: 02-13-2003, 06:20 PM

Tags for this Thread