Computer Science Canada guess the number on C++ |
Author: | rahzab [ Sat Jul 26, 2008 1:53 pm ] | ||
Post subject: | guess the number on C++ | ||
Alrite, I am a total beginner at programming C++. I just started learning the language and it's been about one week. I am just learning do loops and ifs, and im running into a problem Whenever i execute my code, it runs every if statement. Do i have to use a while loop? If it's possible could someone tell me what's wrong with my code? I use dev-C++ by bloodshed as my compiler
Another question; I understand that srand(time)0)) generates a number that is based on the current system time, however, what were to happen if i change the (time(0)) in the argument? I appreciate the help. MOD EDIT:Remember the syntax tags! |
Author: | btiffin [ Sat Jul 26, 2008 8:00 pm ] | ||||
Post subject: | RE:guess the number on C++ | ||||
Change the
Common problem. Catches even the most seasoned professional on occasion. For explanation your code is assigning num to ans (and unless the value is 0, will test as true everytime). Cheers |
Author: | wtd [ Sun Jul 27, 2008 12:01 am ] | ||||
Post subject: | RE:guess the number on C++ | ||||
Also:
Might better be:
|
Author: | md [ Sun Jul 27, 2008 1:17 am ] |
Post subject: | Re: guess the number on C++ |
rahzab @ 2008-07-26, 1:53 pm wrote: Another question; I understand that srand(time)0)) generates a number that is based on the current system time, however, what were to happen if i change the (time(0)) in the argument? I appreciate the help. MOD EDIT:Remember the syntax tags! srand() sets the random number generator seed, it does not return a random number. When you set the random number seed ot time you are setting it to something that you can be fairly certain is different every time you run your program, so the seed is always different. If you call srand(0) at the start of your program you will always get the same pattern of random numbers. Ideally you only call srand() once and then call rand() whenever you need a random number. It's quite possible for your program to run so fast that time(0) (which returns the current number of seconds since the epoch) will remain the same, which means that everytime you call srand() your resetting hte seed to the same number. |