Search code examples
c++srand

Problems with srand(), C++


I'm trying to write a program that generates a pseudorandom numbers using a seed. However, I'm running into problems.

I get this error

39 C:\Dev-Cpp\srand_prg.cpp void value not ignored as it ought to be 

Using this code

#include <iostream>
#include <iomanip>
#include <sstream> 
#include <limits>
#include <stdio.h>

using namespace std ;

int main(){
    int rand_int;
    string close ;

    close == "y" ;

    cout << endl << endl ;
    cout << "\t ___________________________________" << endl ;
    cout << "\t|                                   |" << endl ;
    cout << "\t|   Pseudorandom Number Game!       |" << endl ;
    cout << "\t|___________________________________|" << endl ;
    cout << endl << endl ;

    while ( close != "y" ){

        rand_int = srand(9);
        cout << rand_int << endl ;

        cout << "  Do you wish to exit the program? [y/n]     " ;
        cin >> close ; }

}

Solution

  • srand doesn't return a random number, it just reseeds the random number generator. Call rand afterwards to actually get a number:

    srand(9);
    rand_int = rand();