Hello Everyone,

I am working on a program that will write random numbers to a file. I am getting the following error on line 31 of the filer.cpp file.

"error: no match for 'operator<<' in 'outstream << srand((( srand(((unsigned int)time(Ou)))'"

I believe the code used to generate the random number is correct. Am I doing something wrong with the output?


//main

#include "utility.h"
#include "filer.h"

using namespace std;

int main()
{
    int n, range;
    bool random;
    string fileName;

    cout << "Hello User!" << endl;

    cout << "How many numbers do you want in the file?" << endl;
    cin >> n;

    cout << "How many digits per number?" << endl;
    cin >> range;

    cout << "Please enter 1 for random or 0 for pseudorandom." << endl;   //bool, 1 is TRUE, 0 False
    cin >> random;

    cout << "Please enter the desired file name." << endl;
    cin >> fileName;

    cout << endl; //System Pause



//=========== Test code ======================
    cout << n << endl;
    cout << range << endl;
    cout << random << endl;
    cout << fileName << endl;
//=========== Test code ======================

    filer filerObject;
    filerObject.makefile(n, range, random, fileName);



    return 0;
}

//filer.cpp

#include "utility.h"
#include "filer.h"

filer::filer()
{
    //ctor
}

filer::~filer()
{
    //dtor
}



//precondition: n, range, truly_random, and file_name have
//been set; truly_random is type bool, and determines whether
//the numbers are truly random, or pseudorandom
//postcondition: n random integers in the range 0-range,
//inclusive, have been written to a file named file_name,
//one per line;
void filer::makefile(int n, int range, bool truly_random, string file_name)
{
    ofstream outstream;
    outstream.open(file_name.c_str());

    if (!truly_random){
        outstream << rand() << endl;
    }
    else if (truly_random){
        outstream << srand(int(time(NULL))) << endl;             //ERROR HERE
    }



}

//filer.h

#ifndef FILER_H
#define FILER_H
//header file FILER.H for class filer
//a class object can create a file of a specified
//number of random integers in a specified range

class filer
{
public:
filer(); //NOT ORIGINAL
void makefile(int n, int range, bool truly_random, string file_name);
//precondition: n, range, truly_random, and file_name have
//been set; truly_random is type bool, and determines whether
//the numbers are truly random, or pseudorandom
//postcondition: n random integers in the range 0-range,
//inclusive, have been written to a file named file_name,
//one per line;

virtual ~filer(); //NOT ORIGINAL
private:
    int next_number(int range);
    //precondition: range has been set to a nonnegative value
    //postcondition: returns a random integer in the range
    //0-range, inclusive
};
#endif //FILER_H

That explains it. Thanks for the link.

Comments
Reading provided documentation and answering your own question. Nice. :)
This article has been dead for over six months. Start a new discussion instead.