Originally Posted by
ryansanford
This is what I have for a user-defined range and everything works properly until I upload it to the upload site.
What do you mean with "upload it to the upload site?
Originally Posted by
ryansanford
The first run works but the second run gives an infinite output of "Too Low". What am I doing wrong?
It is always easier for us if you tell us for which inputs the program doesn't work.
So my guess it that the second time you have inputted the higher limit before the lower one, right?
Code:
$ ./test
Enter 2 integers for the range
3 1
I'm thinking of a number between 3 and 1
Pick a number
2
Your guess is too low
Pick a number
3
Your guess is too low
Pick a number
Now look at your code:
Code:
unsigned int a,b,count=0,range;
unsigned int guess=0,answer;
printf("Enter 2 integers for the range\n");
scanf("%d %d", &a, &b);
printf("I'm thinking of a number between %d and %d\n", a, b);
range=b-a;
srand(min(a,b));
answer=(rand()%(range+1)+a);
You assume that "a" will always be the smaller number. But if "b" is the smaller one, "range" will be negative. Because "range" is unsigned, it's value is (b - a) + (UINT_MAX + 1) which is probably a very big number (e.g. in my example (1 - 3) = -2, adding UINT_MAX + 1 on my system (32bit integers) will be -2 + 4294967295 + 1 = 4294967294).
As a consequence "answer" will probably be a very big number too.
Another problem is how you use srand(). Since you seed the pseudorandom number generator with the smaller value of "a" and "b", you will always get the same "random" number if you play the game several times with the same input values.
Bye, Andreas