Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Can't figure out why this while loop runs infinitely

$
0
0
the program is supposed to take input and then validate it with a while loop however, the validation loop prints infinitely and i'm not sure why.

#include <stdio.h>

#define PI 3.141593 

int main()
{
  int length;
  int radius;
  double surfaceArea;
  double volume;
  
  printf("ACME Truck Company \n");
  
  printf("This program will calculate the volume and surface area of the X-11\n");
  printf("cylindrical tank.\n");
  
  printf("\nEnter the length of the tank (feet):  \n");
  scanf("%d", &length);
  
  while (length > 20 || length < 10)
  {
      printf("Invalid input for length.\n");
      scanf("Input length again: %d\n", &length);
  }
  
  printf("Enter the radius of the tank (feet):  \n");
  scanf("%d", &radius);
  
  while (radius > 6 || radius < 3 || 2*radius >= length)
  {
      printf("Invalid input for radius.\n");
      scanf("Input radius again: %d\n", &radius);
  }
  
  surfaceArea = (2*PI*radius*length) + (4*PI*radius*radius);
  volume = (PI*radius*radius*length) - ((4*PI*radius*radius*radius)/3);
  
  printf("\nA cylindrical tank of length %d and radius %d with inverted spherical\n", length, radius);
  printf("caps has\n");
  printf("Volume:  %3.3f\n", volume);
  printf("and\n");
  printf("Surface Area:  %3.3f\n", surfaceArea);
  
  return 0;
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>