My program generates a list of random or pseudo random numbers between a certain range specified by the user through the console. My problem is when the user, or in this case myself, tries to generate a random number list specified by entering true(1) in the console the output is always false(0). I cant seem to figure this out. Any help would be great.

The program has four files. utility.h just contains all the include statements, filer.h, filer.cpp has the code to make the file of random numbers, and test.cpp has the main function to test the code with the console input.

Here is the application code.

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

/***
 Created by Andrew Smith on 8/25/11
 This program tests the filer class by asking the
 user to define the number generation criteria.
 It prints the numbers to a file specified by
 the user
***/

using namespace std;

int main(){

    int num;
    int range;
    bool randNum;
    string fileName;
    filer file; //create new filer object

    //get file name
    cout << "File name?" << endl;
    cin >> fileName;
    //get how many numbers to generate
    cout << "How many number?" << endl;
    cin >> num;
    //get the range
    cout << "What is the range of numbers?" << endl;
    cin >> range;
    //random or pseudo random
    cout << "Random/Pseudo (true/false)" << endl;
    cin >> randNum;

    //make the file
    //file.makefile(num, range, rand, fileName);

    return 0;
}

Here is my second class filer.cpp

#include "utility.h"
#include "filer.h"
/***
 Created by Andrew Smith on 8/25/11
 This program takes input from the user and generates
 numbers to a file specified by the user.
 ***/

using namespace std;

void filer::makefile(int n, int range, bool truly_random, string file_name){
    int temp;
    truly_random = NULL;
    ofstream outFile;
    outFile.open(file_name.c_str()); //open the file

    //seed if value is true
    if(truly_random == 1){
        srand(int(time(NULL)));// seed the random number generator
    }// end if

    //generate the numbers
    for(int i = 0; i < n; i++){
        temp = rand() % range; //generate a number from 0 to range inclusivly
        outFile << temp << endl;
    }//end for
}// end makefile()

Look at the opportunities for buffer overrun before it actually asks for the randNum bool.
Move it higher in the program and you will probably see that it's the "range" value that is overflowing into the randNum causing it to be something other than 1.

I think your right about the buffer overflow. If I move it up in the program then the other cin statements start acting weird. Any suggestions about how i could fix it?

In filer::makefile() , why are you setting a boolean value truly_random to NULL? It's boolean. Values are true and false.

You also test it for 1. Again, true and false.

And based on your code, you will never call srand() since truly_random will never be 1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.