Szymon Zmudzki
Szymon Zmudzki

Reputation: 89

C: My loop doesn't seem to work

Im new to C, and today I encountered a problem I haven't managed to figure out, so I need some help. We've been given this task:

'Write a program that demonstrates a ‘while’ loop that uses the ‘read-ahead’ technique: it asks the user to enter numbers between (and including) 10 and 100, entering a zero terminates the loop. If numbers less than 10 or larger than 100 are entered an error message is shown (e.g. “Error: 250 is not allowed”). After termination of the loop the program prints the amount of numbers that was entered.'

The issue I have is that once I type in a valid number (between 10-100) the program sits still, it doesn't terminate nor loop. On the other hand, if I type in a non valid number like 8, 102 it loops the printf("Error, %d is not allowed\n", num);

here's the code:

#include <stdio.h>


main(void)

{

int num;
int counter=1;

printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);

if((num <10) || (num >100))
{
    printf("Error, %d is not allowed\n", num);
}

while (num > 0)
{
    counter++;

    if((num <10) || (num >100))
    {
        printf("Error, %d is not allowed\n", num);
    }
    counter++;
}


printf("%d numbers were entered!\n", counter);

}

Upvotes: 2

Views: 2199

Answers (6)

Justin Cole
Justin Cole

Reputation: 1

The answers already shared are correct, you need to ask for input within the loop. Also, you need to give a way for the user to exit out of the loop (a sentinel for example) and if you know the inputs are going to be non-negative, then you can define your variables as "unsigned int" instead of just "int". See below for a way.

    #include <stdio.h>

    int main(void)
    {
       unsigned int num = 0, counter = 0;

       while (num != -1) // need to control with a sentinel to be able to     exit
    {
        printf("Please type in your number between (and including) 10-100. Enter \"-1\" to exit.\n");
        scanf("%d", &num);
        counter++; // increase the count by one each successful loop

        // set the non-allowed numbers
        if((num <10) || (num >100))
        {
            printf("Error, %d is not allowed\n", num);
            counter--; // Need to subtract 1 from the counter if enter a non-allowed number, else it will count them
        }

    } // end while loop

       printf("%d numbers were entered!\n", counter); // Show the use how many correct numbers they entered


    } // end main

Upvotes: 0

exilit
exilit

Reputation: 1176

This is because you have to get the number from input (scanf) inside the loop. Otherwise the loop loops forever ( if the fitst number entered was >0). And if it is not valid you see the output.

Upvotes: 0

user2972135
user2972135

Reputation: 1461

#include <stdio.h>

void main(void)
{
    int num = 0, counter = 0;

    printf("Please type in your number between (and including) 10-100\n");

    do
    {
        printf("? ");
        scanf("%d", &num);
        if (!num)
            break;
        else if((num < 10) || (num > 100))
            printf("Error, %d is not allowed\n", num);
        counter++;
    } while (num);

    printf("%d numbers were entered!\n", counter);

}

Upvotes: 1

Alexander
Alexander

Reputation: 632

Your while loop is caught in an infinite loop. You set the condition that "WHILE (num > 0)", but then you never actually change it. That's why the code gets caught when your num is in bounds.

Upvotes: 1

Paul92
Paul92

Reputation: 9062

The problem is that you should read inside the loop:

while (num != 0)
{
    printf("Please type in your number between (and including) 10-100\n");
    scanf("%d", &num);

    if((num <10) || (num >100))
    {
        printf("Error, %d is not allowed\n", num);
    }
    counter++;
}

There is also no need for you to increment counter 2 times.

Note that this snippet will also increment counter for 0. If this is not wanted, then the number of numbers excepting 0 is counter - 1.

Upvotes: 1

Alexander L. Belikoff
Alexander L. Belikoff

Reputation: 5711

You have to ask for a number inside the loop:

printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);

And please check the return code from scanf to detect errors.

Finally, you increment your counter twice inside the loop.

Upvotes: 2

Related Questions