Using while produces an infinite loop...

This is a discussion on Using while produces an infinite loop... within the C Programming forums, part of the General Programming Boards category; This is my practice program for finding the resultant force in statics. When I run the program it will run ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Using while produces an infinite loop...

    This is my practice program for finding the resultant force in statics. When I run the program it will run through the loop once fine, but the second time it skips the scanf command and prints the printf commands in an infinite loop. I have been trying to figure out why this is and would really appreciate your help. Thanks!

    [code]
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>

    int main(){
    float xr = 0, fr = 0, num = 0;
    float force = 0, x = 0, centroid = 0;
    num = 0;

    while (xr >= 0){
    printf("enter Force and centriod separated by a comma,\n");
    printf("enter 0 for centroid to finish.\n");
    scanf("%f %f", &fr, &xr);

    force = force + fr;
    x = x + xr;
    xr = 0;
    centroid = (force * x) / force;

    }

    printf("\tResultant force is %f\n", &force);
    printf("\tCentroid is %f\n", &centroid);

    return 0;
    }
    [\code]

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The "f" in "scanf" stands for formatted. If you tell people to enter numbers separated by a comma, then the format you use to read in the numbers had better be two number separated by a comma.

    (You are also highly unlikely to want the print the location of force and centroid, rather than their values.)

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You're using 'while( xr >= 0 )'. This means it will execute while xr is greater than or equal to 0. You are setting xr to 0 at the end of every loop, this means that xr will be greater than or equal to 0, so it will always execute.

    Also you aren't using an array or anything, so if someone does enter more than one case, it will only print the last one. Try moving your printf statements into the loop, and as tabstop said, you don't want to be supplying the address of your variables to printf, just the value.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    pkr
    Posts
    32

    Think this must be solving the problem

    while (xr >= 0){//if xr>=0 here
    printf("enter Force and centriod separated by a comma,\n");
    printf("enter 0 for centroid to finish.\n");
    scanf("%f %f", &fr, &xr);

    force = force + fr;
    x = x + xr;
    xr = 0; //why xr equal zero here
    centroid = (force * x) / force;

    }

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Thanks!

    After your suggestions, I have found many more errors, including math errors. The program is working perfect now, though, so thank you. I was wondering why the scanf command didn't execute after the first loop, though. I understand that my syntax was wrong and what it should have been, but I do not understand the behavior that resulted. You guys are a great help and I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21