5 Replies - 305 Views - Last Post: 28 January 2013 - 11:29 AM Rate Topic: -----

#1 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Can't figure out why this while loop runs infinitely

Posted 28 January 2013 - 10:57 AM

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



Is This A Good Question/Topic? 0
  • +

Replies To: Can't figure out why this while loop runs infinitely

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: Can't figure out why this while loop runs infinitely

Posted 28 January 2013 - 11:09 AM

Which while loop seems to be the problem?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Can't figure out why this while loop runs infinitely

Posted 28 January 2013 - 11:13 AM

View Postjimblumberg, on 28 January 2013 - 11:09 AM, said:

Which while loop seems to be the problem?

Jim


Both while loops are actually doing the same thing. I will input a value that is invalid and it will keep printing the printf line infintely.
Was This Post Helpful? 0
  • +
  • -

#4 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: Can't figure out why this while loop runs infinitely

Posted 28 January 2013 - 11:27 AM

I don't think you can have a sentence in the scanf format.

scanf("Input length again: %d\n", &length); 
/***shoul be****/
//printf("Input length again: \n")
//scanf("%d", &length);


Was This Post Helpful? 0
  • +
  • -

#5 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Can't figure out why this while loop runs infinitely

Posted 28 January 2013 - 11:28 AM

nvm i figured out you cannot print out stuff with scanf, just had to change it to scanf("%d", &length);

View Postmojo666, on 28 January 2013 - 11:27 AM, said:

I don't think you can have a sentence in the scanf format.

scanf("Input length again: %d\n", &length); 
/***shoul be****/
//printf("Input length again: \n")
//scanf("%d", &length);



ya you posted just as i saw that and fixed it lol thanks
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: Can't figure out why this while loop runs infinitely

Posted 28 January 2013 - 11:29 AM

The scanf() function is meant for input, not printing out information. So when your scanf() looks like:
      scanf("Input length again: %d\n", &length);

It will expect you to type in everything in the format string into the variable, but since the specifier and variable are for a numeric type the scanf() will fail, you can't enter a character ('I') into a number.

You need to put the string "Input length again: " in a printf() then call scanf():

printf("Input length again: ");
scanf("%d", &length);



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1