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?
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 withscanf()reporting "I can't convert anything" and you telling it "have another go".