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; }