| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with the rand function
Hi, I am trying to write a program that generates a random number between 1 & 100 and I have 10 chances to guess what the number is before it tells me.
I think I can write all of it, with the exception of the rand function (the initialising of it), from there all you have to do is compare the unknown value with your guess (correct me if I am wrong) I am using C If possible could anyone give me any pointers please? Thanks |
|
#2
|
|||
|
|||
|
Here's how to get a random number:
Code:
// generate a random number
#include <iostream>
#include <windows.h>
int main()
{
int randomNum = rand();
srand( GetTickCount() );
randomNum = rand() % 100; // from 0 to 99 - one less than the value you enter so if you put 20 it will be 0 to 19
std::cout << "Random number is " << randomNum;
return 0;
}
Then just compare randomNum with your guess. |
|
#3
|
|||
|
|||
|
Thanks, is this basically the same for C?
I have been trying to use just the following commands #include <stdio.h> #include <stdlib.h> rand() srand Is this possible with a similar structure? Thanks again |
|
#4
|
|||
|
|||
|
You can use stdio.h and stdlib.h, but you also need windows.h for the GetTickCount()
srand( GetTickCount() ); int randomNum = rand() % 100; Then just compare it. Without using windows.h it won't work. |
|
#5
|
|||
|
|||
|
If you don't want to use windows.h, include time.h instead and replace GetTickCount() with clock().
|
|
#6
|
|||
|
|||
|
Thanks, just wrote it in C, changing the bits that apply (ie - cout to printf etc)
but get the error .eh is for C++ Which part does this refer to? I tried double clicking on the error and it takes me to a fuction list Thanks |
|
#7
|
|||
|
|||
|
Nearly cracked it, just one final thing I noticed when I was debugging, if I put 2 wrong answers in, then enter the right answer on the third go, it will tell me I have had too many goes and kick me out, but if I increase the count condition by one, it keeps telling me that my third answer is always correct, even if it isn't.
I have already tried moving the increment value to outside the loop, but obviously after the first loop, the value will not change. Could someone have a look and see if it can be rectified or suggest any ideas? Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Guess a number 1-6 with 3 tries
void main()
{
int a, ans;
int count = 0;
srand((unsigned) time (NULL));
ans = ((rand()%6)+1);
printf("%d\n", ans);
printf("Three chances to to guess a number between 1 - 6: ");
scanf("%d", &a);
while ((a != ans) && (count < 2))
{
count++;
printf("Wrong! Please try again: Number of goes so far (%d): ", count +1);
scanf("%d", &a);
}
if (count >=2 )
{
printf("Too many goes. End of game. The number was %d\n", ans);
}
else
{
printf("Correct!\n");
printf("You had %d goes.\n", count + 1);
}
}
|
|
#8
|
|||
|
|||
|
First gripe: main() is, by definition and requirement by the standard, always an int function, declared int main() or int main(int argc, char **argv), NOT void main(). And return an int at the end of main() to tell the OS whether your program succeeded (as a general rule 0 means success and 1 means failure).
Now, about the counting answers, this is what the program says: keep taking answers, as long as they're wrong and the count stays less than two. Then, when the count is equal to two, yell about too many tries. -->If you change that to >=3, then after the while loop which exited when count==2, it sees that count < 3, so it goes to the else, which says correct, no matter what a was. The way I fixed this was to declare another int variable called correct. (I would have used bool, but C doesn't have a bool type.) After each scanf, i set Code:
correct = (a == ans); Then I changed the first loop from (a != ans) to (!correct) and the if statement from (count >= 2) to (!correct). |
|
#9
|
|||
|
|||
|
Thanks mate, now I see that you need anoother check statement before you enter the loop, instead of just checking within the loop
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with the rand function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|