Thread: Problem with this function

  1. #1
    Registered User
    Join Date
    Sep 2008

    Problem with this function

    Good day. I am rather new to these forums and was wondering if I could obtain some assistance with this project. Here is the code and what is happening is that an infinite loop is happening. Any help would be much appreciated for this problem, as I was able to get it to work in php.

    #include <stdio.h>
    int main() {
    int x, y, div, i, newx, newy;
    i = 1;
    printf("Type in a fraction: #/#\n");
    scanf("&#37;d/%d", &x, &y);
    if (x < 0 || y < 0) { printf("No negative numbers\n "); return 1;}
    else if (y % x == 0)
    while (i <= x)
            if (x % i == 0 && y % i == 0){
            div = i;
    x = x / div;
    y = y / div;
    printf("In lowest terms: %d/%d\n", x, y);
    return 0;
    Thanks again.
    Last edited by Salem; 09-28-2008 at 03:53 PM. Reason: Added code tags - learn to use them

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Nothing happens to change i or x in your while (i<=x) loop, hence once you get in you can never get out. (If you want the i++ to be in there too, you're going to need some braces.)

  4. #4
    Registered User
    Join Date
    Sep 2008
    Ok, I was able to correct the error. Thank you for your help and I'll remember the code tag for next time.

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    You need to learn to indent.
    And you need to learn that indentation is ignored by the compiler, so you need { and }.
    What IDE / editor do you use?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    enter, x = 10, y = 0 and see what happens

    ie, 10/0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM