Hey there,

I'm having a problem and I tried several times to solve this problem maybe you could help.
I need to modify the program below so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)

Here's what I have so far but i'm getting stuck:

#include <stdio.h>

int main()
{
    int number, x=0, counter = 0;

    printf("Enter a number: ");
    scanf("%d", &number);
    printf("Please enter a number other than %d\n", number);

    while (number!=x)
    {
        scanf("%d", &x);
        while (x!=counter)
        {
            printf("Enter a number other than %d\n", x);
            scanf("%d", &counter);
            if (counter==x)
            {
                printf("wrong\n");
                break;
            }
        }
        if (number==x)
        {
            printf("wrong\n");
            break;
        }
    }

    return 0;
}

any help?

Recommended Answers

All 3 Replies

I haven't gone through this code thoroughly, but a couple things caught my eye.

First, does this program compile?
If so, what is it doing wrong?

I don't think you need the break on line 27. If number==x the program should go back up to line 11 and break out of the loop there anyhow.

I don't see counter increment. How are you keeping track of the number of times the user has been asked to enter a number?

Seems like there are too many while loops. Do you really need a nested while loop?

okay so what i need to do is have a program that does the following
gets a number from the user an asks to put a number different from the first that he inputed for example:
enter a number: 5
enter a number other than 5: 6
enter a number other than 6: 1
enter a number other than 1: 9
etc...
let me know if i didn't explained it very well

You shouldn't need 2 nested loops, I single for loop should be enough for this.

If you are testing just the last input value then you shouldn't need 3 variables, 2 should be enough.

Is this allowable?
enter a number: 5
enter a number other than 5: 6
enter a number other than 6: 5
enter a number other than 5: 6
etc
Or do you need to test all previously entered numbers? If you do need to test all previously entered numbers your data is not sufficient because you need a way to store all input values.

Finally have you considered what happens if the user enters 0 as there first number?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.