1

I have a program to write and it is giving me trouble.

It asks the user for inputs such as suggesting which shape they would like to know the area and/or surface are of. This element, I have mastered.

The problem is that when I try to add in while loops to say that the response is invalid, it messes the whole program up.

I need it to say, "Invalid response, please select option 1, 2 3 or 4 etc.

I am writing the loop as follows:

printf("Please enter 1 for area or 2 for surface area: "); scanf("%i",&menu1); while(menu1!=1&&menu1!=2) { printf("Invalid option. Please enter 1 for area or 2 for surface area: "); scanf("%i",&menu1); } 

The problem is now when the user makes a valid response, "Invalid response" appears. How to fix this?

5
  • 1
    the code snippet you posted won't even compile, please post the one that really compiles and runs with the problem you described Commented Feb 20, 2014 at 14:07
  • printf("Please enter 1 for area or 2 for surface area: "); scanf("%i",&menu1); while(menu1!=1&&menu1!=2) { printf("Invalid option. Please enter 1 for area or 2 for surface area: "); scanf("%i",&menu1); } Commented Feb 20, 2014 at 14:10
  • Apologies. That is the code copied from the program. Commented Feb 20, 2014 at 14:10
  • 1
    I assume there's something wrong with your keyboard. " " - here is a space character, so you can copy paste it into your code. Commented Feb 20, 2014 at 14:30
  • Always test the return value from scanf(); otherwise, you won't know when it tells you it can't interpret the input. if (scanf("%i", &menu1) != 1) { …error; could not read integer… }. If the user types a letter, for example, your code will be stuck in a loop for ever with scanf() reporting "I can't convert anything" and you telling it "have another go". Commented Feb 20, 2014 at 15:48

1 Answer 1

3

The following works for me:

#include <stdio.h> int main(void) { int menu1; // Make sure this is declared printf("Please enter 1 for area or 2 for surface area: "); scanf("%i", &menu1); while((menu1 != 1) && (menu1 != 2)) { printf("Invalid option. Please enter 1 for area or 2 for surface area: "); scanf("%i", &menu1); } return 0; } 

You didn't post the entirety of your code, but I'm assuming that you didn't declare menu1 before using it in scanf. That would certainly cause issues.

Update

As pointed out by @John in the comments, you can eliminate some redundancy by using a do-while to make sure the loop always runs at least once.

As @Cqnqrd notes, scanf can cause some infinite looping problems if you enter non-integral values, the reasons for which are detailed here. You can use fgets and atoi to handle non-integer input better than scanf does (preventing an infinite loop on an input of something like 'aa').

#include <stdio.h> #include <stdlib.h> int main(void) { int menu1 = 0; char input[256]; do { printf("Please enter 1 for area or 2 for surface area: "); fgets(input, 256, stdin); menu1 = atoi(input); if ((menu1 != 1) && (menu1 != 2)) printf("Invalid entry. "); } while ((menu1 != 1) && (menu1 != 2)); return 0; } 
Sign up to request clarification or add additional context in comments.

11 Comments

Would a do while loop be more beneficial here?
@John Probably. I tried to keep it as similar to the body of the question as possible, while still getting it to compile.
When entering an invalid choice (like 'aa'), my console is spammed with the printf statement.
@Cqnqrd Not being a C programming, I'm going to guess that's the fault of scanf being given a non-integer value?
Yes, see this answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.