Thread: Help, program stuck in a while loop

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    Help, program stuck in a while loop

    Hi. I'm writing a calculator application in C. The source can be found here.

    The program right now is going to a function that will get input from the user on what math operation they wish to use, and should be returning it to the main function again.
    But the thing is that after input is given it loops back around and asks again.
    I'm a somewhat new programmer, and this is driving me nuts. Help is appreciated.

    Thanks in advance.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The only apparent loop is the one which checks to make sure that the user input is between 1 and 5 inclusive. Are you typing something outside that range?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    I'm typing numbers inside the allowed range, yet it always performs the loop again.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Your do while loop:
    Code:
    while (choice < 1 || choice > 5);
    should be:
    Code:
    while (choice < 1 || choice > 5)
    That should stop it getting stuck

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Hehe, guess I got a bit overzealous with the semicolon usage.
    It works now, thanks!

    If anyone feels like nitpicking for bad programming habits, feel free to do so! I would like to become a clean and efficient programmer one day.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Your do while loop:
    >should be:
    A do..while loop is required to end with a semicolon. Removing it should cause a syntax error. The problem is something else, most likely an input error that forces the program into an infinite loop (such as if you type a letter rather than a number).

    >If anyone feels like nitpicking for bad programming habits, feel free to do so!
    Okay.

    >system("clear");
    I don't particularly like this, but I've been told I have an extreme opinion on clearing the screen. At the very least, I'll mention that this is a security risk because I can replace the "clear" program with my own malicious program in a more local location and have it run instead.

    >scanf("%d", &choice);
    Always check your input for success and validate it. scanf returns the number of items successfully read, so you can do this:
    Code:
    if ( scanf ( "%d", &choice ) != 1 ) {
      /* Handle the error */
    }
    Aside from that, I don't see any huge problems. It's a good idea always to use void for parameter lists if there aren't any parameters. Also, personally I wouldn't return a dummy value from a function if all the function does is have a side effect, and I'd recommend always having a default case in your switches.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Thanks for the advice.

    I was testing the code on two different platforms.
    I had changed the do... while statement to just a while statement and tested it on my computer. That worked on my computer, but when I tried using it on my Nintendo DS (using DSLinux) it turns out to be still broken. I'm asking for support there to see if I'm doing anything that the distro doesn't like.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    A do..while loop is required to end with a semicolon.
    Oh.. My bad then.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Splint solved it. I Love *lint programs. In case anyone was interested...:

    Code:
    main.c:59:15: Format argument 1 to scanf (&#37;d) expects int * gets unsigned short
                     int *: &choice
    It's all good this time, for real.
    Last edited by DSman; 09-24-2007 at 01:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Stuck on a program
    By Northstar in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 03:02 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM