Home > Blockchain >  I am facing issue in do while in c language "undefined input"
I am facing issue in do while in c language "undefined input"

Time:08-24

I was trying to run do while loop for following function I want to make a loop which can run continue if user puts any wrong alphabet\number I have created entire code but I am facing a very small issue which is written as "undefined input" but I have declared the "input" word in my code Can you please tell me what is the problem and can you fix it will be very helpful for a beginner programmer like me and please tell me the answer in C.

#include <stdio.h>

float square(float side);
float circle(float radius);
float rectangle(float length, float breadth);

int main()
{

  do
  {

    char input;

    printf("Choose 's' for square\n");
    printf("Choose 'c' for circle\n");
    printf("Choose 'r' for rectangle\n");

    scanf("%c", &input);

    if (input == 's')
    {
      float side;
      printf("You have choosen sqaure \n");
      printf("Enter the value of length \n");
      scanf("%f", &side);

      printf("%f", square(side));
      printf("Thank You For using my program :) \n");
    }
    else if (input == 'r')
    {
      float a, b;
      printf("You have choosen rectangle \n");
      printf("Enter the value of length \n");
      scanf("%f", &a);
      printf("Enter the value of breadth \n");
      scanf("%f", &b);

      printf("%f", rectangle(a, b));
      printf("Thank You For using my program :) \n");
    }

    else if (input == 'c')
    {
      float rad;
      printf("You have choosen circle \n");
      printf("Enter the value of radius : \n");
      scanf("%f", &rad);

      printf("%f", circle(rad));
      printf("Thank You For using my program :) \n");
    }
    else
    {
      printf("Enter The Correct letter ! \n");
    }
  } while (input != 's' || 'c' || 'r');
  return 0;
}
float square(float side)
{
  return side * side;
}

float circle(float radius)
{
  return 3.14 * radius * radius;
}

float rectangle(float lenght, float breadth)
{
  return lenght * breadth;
}

CodePudding user response:

input is defined inside do {}, but you are using it outside that. Move the line char input; before do.

Additionally, your loop condition is incorrect. Use this:

while (input != 's' && input != 'c' && input != 'r');

CodePudding user response:

Define input outside of your do-while loop.

The while loop condition is incorrect as @VLL said.

I have rewritten your code to be better and more readable.

#include <stdio.h>

float square(float side);
float circle(float radius);
float rectangle(float length, float breadth);

int main() {
    char input;  // Keep input outside of the while
    while (1) {  // Loop forever until bad input
        printf("Choose 's' for square\n");
        printf("Choose 'c' for circle\n");
        printf("Choose 'r' for rectangle\n");
        scanf(" %c", &input);
        if (input == 's') {
            float side;
            printf("You have choosen sqaure \n");
            printf("Enter the value of length \n");
            scanf("%f", &side);
            printf("%f\n", square(side));
        } else if (input == 'r') {
            float a, b;
            printf("You have choosen rectangle \n");
            printf("Enter the value of length \n");
            scanf("%f", &a);
            printf("Enter the value of breadth \n");
            scanf("%f", &b);

            printf("%f\n", rectangle(a, b));
        } else if (input == 'c') {
            float rad;
            printf("You have choosen circle \n");
            printf("Enter the value of radius : \n");
            scanf("%f", &rad);

            printf("%f\n", circle(rad));
        } else {
            printf("Invalid input \n");
            continue;
        }
        printf("Thank you for using my program :)\n");
        break;  // If we don't go inside the "else", we exit the while loop
    }
    return 0;
}
float square(float side) { return side * side; }

float circle(float radius) { return 3.14 * radius * radius; }

float rectangle(float lenght, float breadth) { return lenght * breadth; }

Feel free to ask any additional questions

  • Related