Issues with loops

This is a discussion on Issues with loops within the C Programming forums, part of the General Programming Boards category; I'm currently learning C, and this assignment has come up that has me completely baffled. I know I shouldn't be, ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    28

    Issues with loops

    I'm currently learning C, and this assignment has come up that has me completely baffled. I know I shouldn't be, as it looks like the simplest thing in the world, but I just can't seem to get this right.

    I need an input between 1 and 10. If any other input is received, there is supposed to be an error message before the user is prompted again. After that, I ask for a second input between 1 and 4. Again, if any other input is received, there is supposed to be an error message, followed by a reprompt.

    I tried an else-if series, but that didn't function as a loop. Alternatively, my switch statement got stuck in a loop. I've tried every variation of for, while, and do, and none of them seem to be working properly. If someone could give me a hint, or at least tell me which loop type to use and why it should work, I would highly appreciate it.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    I usually use a do loop when dealing with user input, since you always want to ask them at least once. Remember, a do loop always executes at least once, a for or while loop may never execute. Something like:
    Code:
    do {
        prompt user
        read input
    } while input is not valid
    If you get stuck after that, post your code and tell us what specifically you are having trouble with, and we can take it from there.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    So I tried this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n, k;
    
       do {
        printf("Enter the number of items in the list (n):");
        scanf("%d", &n);
       }     while (n > 0 && 10 >= n); {
                printf("?Invalid input: Number must be between 1 and 10\n");
            }
    
    
        printf("Enter the number of items to choose (k):");
        scanf("%d", &k);
        return 0;
    }
    If I type between 1 & 10, it just re-prompts me with "Enter the number of items in the list (n):" However, if I type anything out of range, it outputs "?Invalid input: Number must be between 1 and 10" once before moving to "Enter the number of items to choose (k):".

    However, if I reverse the > signs, it outputs "?Invalid input: Number must be between 1 and 10" regardless of what I type and then moves on.

    What am I missing?

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    Well, I figured it out. I made a for statement with an if-else condition that was somehow different from the four variations I had already tried.

    Thanks for the help, though. I don't think I would have gotten there without it.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    Glad you got it worked out. You had your loop condition backwards, but didn't fix it properly, and the placement of your "invalid input" message is incorrect. Take a look at what you wrote:
    Code:
    do {
        printf("Enter the number of items in the list (n):");
        scanf("%d", &n);
    } while (n > 0 && 10 >= n); {
    printf("?Invalid input: Number must be between 1 and 10\n");
    }
    do loops don't have "else" type clauses. The invalid input statement would always be printed once the loop exits.

    As for when the loop exits, remember, in C, your loops have "keep going" condition, i.e. the loop repeats so long as the loop condition is true. So your loop repeats while n is greater than 0 and less than or equal to 10. That means if the user inputs a valid number, from 1-10, you repeat and ask for more. The loop I gave was do...while input is not valid. You did do...while input is valid.

    So then you switched the direction of the < and > signs, which doesn't give exactly the opposite logic. Switching the signs says repeat the loop while n is less than 0 and greater that or equal to 10. That is impossible. What you wanted was this:
    Code:
    do {
        prompt user
        read input
        if (n <= 0 || n > 10)
            print "invalid input..."
    } while (n <= 0 || n > 10);
    When you invert a < you get >=, and when you invert a <=, you get >. Inverting > and >= work similarly. Also, when you invert a logical statement with an && or an ||, you have to invert the logic of each half of the && or ||, and switch && for || or vice versa. This is called DeMorgan's Law. An example would probably be simpler:
    Code:
    // Assume I have a statement like this one
    A && B
    // if I want to invert it, I can do
    !(A && B)
    // but sometimes that is hard to read, so the following is more clear
    !A || !B
    // note that A and B are individually inverted, and that the && changed to an ||

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 08:45 PM
  2. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  3. cpp issues
    By nime in forum C Programming
    Replies: 9
    Last Post: 09-27-2010, 07:44 AM
  4. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  5. Issues with variables and for loops
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 01-10-2004, 07:48 AM

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