I basically, for this assignment, have to make a mini-program that randomly generates a string of letters based on randomized numbers (for which 0 corresponds to A, 1 corresponds to B, 2 corresponds to C, and so on) alongside with a number n corresponding to the nth string generated. These strings and their corresponding numbering are to be printed out to a text file. For example, if my function was called myFunction
:
myFunction("file.txt", 3)
Produces a text file called "file.txt" with the following contents:
AAAAAAAA 1
IWFNWEFS 2
WEWEFCSD 3
Where the strings' letters are each supposed to be randomized.
However, for some reason each of my strings are identical for some reason. That is, for each row of string-number pairs, all the strings turn out to be the same (when I am supposed to make them all different/random). Why is this happening? Here is my code:
#include <iostream>
#include <vector>
#include <cstdlib>
#include "math.h"
#include <fstream>
using namespace std;
string key = ""; // Empty initial key for use in "makeKey".
// "makeKey" function to create an alphabetical key
// based on 8 randomized numbers 0 - 25.
void makeKey() {
int k;
for (k = 0; k < 8; k++) {
int keyNumber = (rand() % 25);
if (keyNumber == 0)
key.append("A");
if (keyNumber == 1)
key.append("B");
if (keyNumber == 2)
key.append("C");
if (keyNumber == 3)
key.append("D");
if (keyNumber == 4)
key.append("E");
if (keyNumber == 5)
key.append("F");
if (keyNumber == 6)
key.append("G");
if (keyNumber == 7)
key.append("H");
if (keyNumber == 8)
key.append("I");
if (keyNumber == 9)
key.append("J");
if (keyNumber == 10)
key.append("K");
if (keyNumber == 11)
key.append("L");
if (keyNumber == 12)
key.append("M");
if (keyNumber == 13)
key.append("N");
if (keyNumber == 14)
key.append("O");
if (keyNumber == 15)
key.append("P");
if (keyNumber == 16)
key.append("Q");
if (keyNumber == 17)
key.append("R");
if (keyNumber == 18)
key.append("S");
if (keyNumber == 19)
key.append("T");
if (keyNumber == 20)
key.append("U");
if (keyNumber == 21)
key.append("V");
if (keyNumber == 22)
key.append("W");
if (keyNumber == 23)
key.append("X");
if (keyNumber == 24)
key.append("Y");
if (keyNumber == 25)
key.append("Z");
}
return;
}
// "makeFile" function to produce the desired text file.
// Note this only works as intended if you include the ".txt" extension,
// and that a file of the same name doesn't already exist.
void makeFile(string fileName, int n) {
ofstream ourFile;
ourFile.open(fileName);
int k; // For use in below loop to compare with n.
int l; // For use in the loop inside the below loop.
for (k = 1; k <= n; k++) {
for (l = 0; l < 8; l++) { // To write to the file ONE key
ourFile << key[l]; // C++ only lets me do it this way...
}
ourFile << " " << k << "\n"; // Writes two spaces and the data value
}
}
// Primary function to create our desired file!
void mainFunction(string fileName, int n) {
makeKey();
makeFile(fileName, n);
}
int main() {
mainFunction("file.txt", 3); // To test program
cin.get();
return 0;
}
I tried using both srand ( time(NULL) );
and srand (1);
in the first part, but that didn't work for me either.