# Input Help

This is a discussion on Input Help within the C Programming forums, part of the General Programming Boards category; hi everyone! i'm trying to do some homework and this program i wrote that's supposed to read in numbers and ...

1. ## Input Help

hi everyone! i'm trying to do some homework and this program i wrote that's supposed to read in numbers and do some calculations isn't working right. The problem right now is that no matter what number i put in for radius it gives me the invalid number result, please help me and thanks in advance.

Code:
```#include <stdio.h>

#define PI 3.141593

int main (void)
{
double len, rad, SA, vol;

printf("Enter the length of the tank\(feet\)\n");
scanf("%lf", &len);
while(len<10 || len>20)
{
printf("Invalid length, please try again.\n");
scanf("%lf", &len);

}

printf("Enter the radius of the tank\(feet\)\n");
scanf("%lf", &rad);
while(rad<3 || rad>6)
{
printf("Invalid radius, please try again.\n");
scanf("%lf", &rad);

}

while(2*rad>=len)
{
printf("Non-real tank,Invalid input, please try again.\n");
printf("Enter the length of the tank\(feet\)\n");
scanf("%lf", &len);
while(len<10 || len>20)
{
printf("Invalid input, please try again.\n");
scanf("%lf", &len);

}

printf("Enter the radius of the tank\(feet\)\n");
scanf("%lf", &rad);
while(rad<3 || len>6)
{
printf("Invalid input, please try again.\n");
scanf("%lf", &rad);
}

}

vol = (PI*rad*rad*len)-(4/3*PI*rad*rad*rad);
SA = (2*PI*rad*len)+(4*PI*rad*rad);
printf("A cylindrical tank of length %.3lf and radius %.3lf with inverted spherical \ncaps has\nvolume: %.3lf\nand\nsurface area: %lf",len, rad, vol,
SA);
return 0;
}```

2. Replace the 4/3 with 4.0/3.0

4/3 will be done in integer arithmetic otherwise, which is a pretty inaccurate approximation.

3. okay will do, but when i execute it the main problem is the radius not being ever considered valid
i don't know if it's my compiler or my code

4. Well a minor point is that \( \) are not necessary in C strings.
Code:
```\$ gcc -Wall foo.c
foo.c:9:10: warning: unknown escape sequence '\)'
foo.c:18:11: warning: unknown escape sequence '\)'
foo.c:30:16: warning: unknown escape sequence '\)'
foo.c:39:12: warning: unknown escape sequence '\)'```
Other than that, I get these results.
Code:
```\$ ./a.out
Enter the length of the tank(feet)
15
Enter the radius of the tank(feet)
5
A cylindrical tank of length 15.000 and radius 5.000 with inverted spherical
caps has
volume: 654.499
and
surface area: 785.398250\$```

5. i don't know if it's my compiler or my code
Just like on House it's never lupus, it's never the compiler!*

*except in an extremely small number of cases, usually involving something far more complicated than any student is attempting

6. hmm well this is what i got

so....i want to say it's me but...

7. what's a good program to use to run my code to check it?

8. I haven't used that particular debugger myself, but Pelle's C (click click) has a debugger with it.

Quzah.

9. Well guessing from your image, you're using this -> USF Polytechnic: CLUE

If you haven't done so already, make a backup of your work.

First thing to do (in explorer) is navigate to your project directory and verify that the .exe is newer than all your C files. If it isn't, then you're running an old copy. Make sure you "save" and then "build all" to be sure. If the build environment has a "clean" option as well, then doing "clean" before "build all".

If you still can't make your program work, then it looks like a bug in that CLUE environment, in which case I think you should raise a bug.

IMO, you should be using an environment that is mature, stable, and commonly used throughout the world.
Not some bug-ridden "prototype" research project (you're just a lab-rat in someone else's experiment).

C is hard enough, without having to second-guess what the tools are doing.

10. okay so i'm using a different C IDE to finish this, but now when i make the input fail the "while(2*rad>=len)" when i try to input a new number for the radius it never says it's a valid input.

Code:
```#include <stdio.h>

#define PI 3.141593

int main (void)
{
double len, rad, SA, vol;

printf("Enter the length of the tank(feet)\n");
scanf("%lf", &len);
while(len<10 || len>20)
{
printf("Invalid length, please try again.\n");
scanf("%lf", &len);

}

printf("Enter the radius of the tank(feet)\n");
scanf("%lf", &rad);
while(rad<3 || rad>6)
{
printf("Invalid radius, please try again.\n");
scanf("%lf", &rad);

}

while(2*rad>=len)
{
printf("Non-real tank,Invalid input, please try again.\n");
printf("Enter the length of the tank(feet)\n");
scanf("%lf", &len);
while(len<10 || len>20)
{
printf("Invalid input, please try again.\n");
scanf("%lf", &len);

}

printf("Enter the radius of the tank(feet)\n");
scanf("%lf", &rad);
while(rad<3 || len>6)
{
printf("Invalid input, please try again.\n");
scanf("%lf", &rad);
}

}

vol = (PI*rad*rad*len)-(4.0/3.0*PI*rad*rad*rad);
SA = (2*PI*rad*len)+(4*PI*rad*rad);
printf("A cylindrical tank of length %.3lf and radius %.3lf with inverted spherical \ncaps has\nvolume: %.3lf\nand\nsurface area: %.3lf",len, rad, vol,
SA);
return 0;
}```

11. Why are you doing everything twice? You ask for len. You get one ranging from 10 to 20, then you ask for rad ranging from 3 to 6. Then you do both again.

Quzah.

Popular pages Recent additions