2 Replies - 289 Views - Last Post: 23 January 2013 - 08:45 PM Rate Topic: -----

#1 perryftw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-April 12

while loop not initializing

Posted 23 January 2013 - 08:26 PM

Hello all. Basically, I'm new to C programming but not necessarily to programming. I'm a college student and took a semester of Java, so I understand many of the fundamentals. However, I'm having issues with the while loop in my code not initializing if the condition is met.

#include <stdio.h>

#define PI 3.141593f

int main(void)
{
  float length = 0.0;
  float radius = 0.0;
  float check = 0.0;
  
  printf("ACME Truck Company\n");
  printf("This program will calculate the volume and surface area of the x-11 cylindrical tank.");
  
  printf("\nEnter the length of the tank (feet): ");
    scanf("%f", &length);
  printf("Enter the radius of the tank (feet): ");
    scanf("%f", &radius);
    
  while(length <= 10.0 && length >= 20.0) {
    printf("User input is invalid, please enter a value between 10 and 20: ");
      scanf("%f", &length);
      
    check = (length / 2);
    
    if(check <= radius) {
      printf("User input is invalid, please enter a value between %f and 20: ", length);
	scanf("%f", &length);
    }
  }
}



Obviously, there is more to the program, but this needs to be solved before I can move on. Some context: The idea of the program is to calculate the volume and surface area of a cylindrical tank with concave spheres on the ends. However, the length can only be between 10 and 20, and the radius between 3 and 6. Also, should the radius be more than half the length of the container, the tank would not be possible because the concave spheres would overlap.

I thank anyone in advance who can offer help. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: while loop not initializing

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: while loop not initializing

Posted 23 January 2013 - 08:42 PM

Well, if you note closely, you have used an && here and for && to be true, both the conditions should be satisfied.

Logically, there can be no possibility of the length being simultaneously less than 10 as well as greater than 20.

So, that is the reason why your while loop is not initializing.

You need to change your && to || and it should work.

regards,
Raghav

This post has been edited by raghav.naganathan: 23 January 2013 - 08:45 PM

Was This Post Helpful? 1
  • +
  • -

#3 perryftw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-April 12

Re: while loop not initializing

Posted 23 January 2013 - 08:45 PM

View Postraghav.naganathan, on 23 January 2013 - 08:42 PM, said:

Well, if you note closely, you have used an && here and for && to be true, both the conditions should be satisfied.

Logically, there can be no possibility of the length being simultaneously less than 10 as well as greater than 20.

So, that is the reason why your while loop is not initializing.

regards,
Raghav


realized this just now. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1