0

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.

4

1 回答 1

1

每次调用makeKey,它会在 末尾追加 8 个字符key,但不会清除之前的内容,调用 后makeKey会打印出前 8 个字符,这些字符没有变化。

我会摆脱全局变量key,并在每次调用时都makeKey返回一个(新创建的) 。std::string

FWIW,我还会重写makeKey以通过索引到字符数组来将数字转换为字母。

编辑(针对问题中的编辑):是的,您可能还想srand(time(NULL));在程序开始时使用。srand(1);就好像你根本不打电话一样srand。每次运行程序时,使用srand(time(NULL));通常应该给出不同的结果序列(除非你运行它两次的速度如此之快以至于系统时钟不会在两者之间改变——通常在同一秒内两次)。

现在你得到一个字符串重复 8 次。解决上面提到的问题会给你 8 个不同的字符串——但是如果没有类似的东西srand(time(NULL));,你每次运行程序都会得到相同的 8 个字符串组。如果你只是添加srand(time(NULL));而不修复其余部分,你会得到一个字符串 8 次,但每次运行程序都会是一个不同的字符串(而现在,即使你得到的一个字符串也是一样的每次运行程序时一样)。

于 2012-06-10T06:26:49.857 回答