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?

Orkay
  • 11
  • 3
  • 1
    the code snippet you posted won't even compile, please post the one that really compiles and runs with the problem you described – gnat Feb 20 '14 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); } –  Feb 20 '14 at 14:10
  • Apologies. That is the code copied from the program. –  Feb 20 '14 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. – Karoly Horvath Feb 20 '14 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". – Jonathan Leffler Feb 20 '14 at 15:48

1 Answers1

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;
}
Community
  • 1
  • 1
KChaloux
  • 3,918
  • 6
  • 37
  • 52
  • 2
    Would a do while loop be more beneficial here? – John Odom Feb 20 '14 at 14:32
  • @John Probably. I tried to keep it as similar to the body of the question as possible, while still getting it to compile. – KChaloux Feb 20 '14 at 14:33
  • When entering an invalid choice (like 'aa'), my console is spammed with the `printf` statement. – bagage Feb 20 '14 at 14:36
  • @Cqnqrd Not being a C programming, I'm going to guess that's the fault of `scanf` being given a non-integer value? – KChaloux Feb 20 '14 at 14:39
  • Yes, see [this answer](http://stackoverflow.com/questions/1716013/why-is-scanf-causing-infinite-loop-in-this-code) – bagage Feb 20 '14 at 14:44
  • I have declared menu1 at the start. The program worked until I started to add the while loops. It is about 160 lines of code. Should I post that here? I didn't want to bore people. – Orkay Feb 20 '14 at 14:46
  • @Cqnqrd Fiddled with the solution a bit, gave one that uses fgets that shouldn't cause the loop. – KChaloux Feb 20 '14 at 14:49
  • @Orkay We can't do much more for you unless you post the error that the compiler is giving you, along with the associated code at that line number. Consider appending it to the bottom of your question. – KChaloux Feb 20 '14 at 14:50
  • @KChaloux I'm sorry. The compiler isn't giving an error, it's running and doing things that I can't understand. I'm finding it hard to explain the issue due to my noobiness. I understand what you mean, you can't help without any context. This box won't allow me to show you the full code. – Orkay Feb 20 '14 at 14:56
  • @Orkay you could try posting it on http://pastebin.com/ if it's all in a single file, then linking to it. Or do a git gist. – KChaloux Feb 20 '14 at 14:59
  • @Orkay Your logic is weird. Your second while-loop checks for submenu2 to be set, but it won't be if the user selects "1" to input area (instead of surface area). That loop shouldn't even be hit if the user is working with area, I don't think. You should really clean up your formatting and code, move things out into appropriate functions where possible, and go through the logic to make sure it all makes sense. It's getting a bit outside of the scope of an SO question I think. – KChaloux Feb 20 '14 at 15:26