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
c++:

// Guess the number
// By: rahzab

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    //Variables
    int ans;
    int count = 0;
       
    cout << "\tGuess the number game!";
    cout << "\nAlrite, I am going to choose a number between 1-20.";
    cout << "\nYou have to guess it within 5 tries.";
    cout << "\n\nOk Guess the number: ";
   
    // Getting the random number
    srand(time(0));
    int randInt = rand();
    int num = (randInt % 20) + 1;
   
    // The loop that checks whether or not you've guessed the right number
    do
    {
           count +=1;
           cin >> ans;
           if (ans != num)
           {
                   cout << "Oh, wrong number!";
           }
           if (ans = num)
           {
                   cout << "Right on!";
                   break;
           }
    } while (count < 5);
     
     cout << "\nOh you lost!!";
     cout << "\nPress any key to exit";
     std::cin.ignore(std::cin.rdbuf()->in_avail() +2);
     return 0;
}

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
code:

if (ans = num)
to
code:

if (ans == num)

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:

code:
           if (ans != num)
           {
                   cout << "Oh, wrong number!";
           }
           if (ans = num)
           {
                   cout << "Right on!";
                   break;
           }


Might better be:

code:
           if (ans != num)
           {
                   cout << "Oh, wrong number!";
           }
           else
           {
                   cout << "Right on!";
                   break;
           }

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.


: