Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

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

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

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; } 
added 247 characters in body
Source Link
KChaloux
  • 4.1k
  • 7
  • 40
  • 54

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@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 also use fgetsfgets and atoiatoi to handle non-integer input better than scanfscanf 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; } 

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. You can also 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; } 

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; } 
added 247 characters in body
Source Link
KChaloux
  • 4.1k
  • 7
  • 40
  • 54

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. You can also 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: "); scanffgets("%i"input, &menu1256, stdin); menu1 = atoi(input);   if ((menu1 != 1) && (menu1 != 2)) printf("Invalid entry. "); } while ((menu1 != 1) && (menu1 != 2)); return 0; } 

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.

#include <stdio.h> int main(void) { int menu1 = 0; do { printf("Please enter 1 for area or 2 for surface area: "); scanf("%i", &menu1); if ((menu1 != 1) && (menu1 != 2)) printf("Invalid entry. "); } while ((menu1 != 1) && (menu1 != 2)); return 0; } 

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. You can also 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; } 
added 491 characters in body
Source Link
KChaloux
  • 4.1k
  • 7
  • 40
  • 54
Loading
Source Link
KChaloux
  • 4.1k
  • 7
  • 40
  • 54
Loading