Thread: While loop does not run

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    While loop does not run

    Here is a simple program to compute cube roots.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    double x, r=1, rr=0, hi, lo=0, count=0;
    
    printf ("Enter a number to find the cube root:\n");
    scanf("%d", &x);
    
    hi=x;
    
    while ( fabs(rr-x) > 0.01)
    {
    count+=1;
    printf("count = %g\n", count);
    r=(hi+lo)/2;
    rr=pow(r,3);
    if (rr > x)
    hi=r;
    if (rr < x)
    lo=r;
    }
    
    printf("The cube root of %d is:\n%d\n", x, r);
    
    
    return 0;
    }
    For some reason my 'while' loop does not seem to execute. Can anyone tell me why this happens? Below is the output of the program
    Code:
    $ ./a.out
    Enter a number to find the cube root:
    88
    The cube root of 88 is:
    134514048
    
    $

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
       scanf("%lf", &x);
    Will help enter the loop.
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Okay. That seemed to work.

    What difference do the %[character] statements make? Specifically, what difference does it make when using the scanf function?
    Last edited by thetinman; 01-20-2006 at 12:59 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    http://www.die.net/doc/linux/man/man3/sscanf.3.html
    Look in the Conversions section.
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Turn up the warnings
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c -lm
    foo.c: In function ‘main’:
    foo.c:9: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘double *’
    foo.c:25: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    foo.c:25: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘double’

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Here is a simpler program to calculate cube roots:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        double x;
    
        printf ("Enter a number to find the cube root:\n");
        scanf("%lf", &x);
    
        printf("The cube root of %f is:\n%f\n", x, pow(x,1/3.0));
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. Program will not run thorugh the loop
    By Babs21 in forum C Programming
    Replies: 6
    Last Post: 11-10-2007, 12:59 PM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM