Thread: Input Help

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Replace the 4/3 with 4.0/3.0

    4/3 will be done in integer arithmetic otherwise, which is a pretty inaccurate approximation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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$
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    hmm well this is what i got

    Input Help-wtf-jpg


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

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    what's a good program to use to run my code to check it?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I haven't used that particular debugger myself, but Pelle's C (click click) has a debugger with it.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  2. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  5. Help loading a vector with input from input stream
    By Bnchs in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 03:53 PM