基本上,对于这个作业,我必须制作一个小程序,根据随机数随机生成一串字母(其中 0 对应 A,1 对应 B,2 对应 C,等等)以及对应于生成的第 n 个字符串的数字 n。这些字符串及其相应的编号将被打印到一个文本文件中。例如,如果我的函数名为 myFunction
:
myFunction("file.txt", 3)
生成一个名为“file.txt”的文本文件,内容如下:
AAAAAAAA 1
IWFNWEFS 2
WEWEFCSD 3
每个字符串的字母都应该是随机的。
但是,由于某些原因,我的每个字符串都相同。也就是说,对于每一行字符串数字对,所有字符串结果都是相同的(当我应该让它们全部不同/随机时)。为什么会这样?这是我的代码:
#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;
}
我尝试在第一部分中同时使用 srand ( time(NULL) );
和 srand (1);
,但这对我也不起作用。
最佳答案
每次你调用makeKey
,它都会在key
的末尾追加8个字符,但是你永远不会清除之前的内容,并且在调用makeKey之后
,你打印出前 8 个字符,没有改变。
我会去掉全局变量 key
,让 makeKey
每次都返回一个(新创建的)std::string
它被称为。
FWIW,我还会重写 makeKey
以通过索引到 char 数组将数字转换为字母。
编辑(回应问题中的编辑):是的,您可能还想在程序开头使用 srand(time(NULL));
。 srand(1);
与您根本不费心调用 srand
是一样的。使用 srand(time(NULL));
通常应该在每次运行程序时给出不同的结果序列(除非你运行它两次太快以至于系统时钟在两者之间没有变化 -通常在同一秒内两次)。
现在您得到一个字符串重复 8 次。解决上面提到的问题会给你 8 个不同的字符串——但是如果没有像 srand(time(NULL));
这样的东西,你每次运行程序时都会得到相同的 8 个字符串组。如果你只是添加 srand(time(NULL));
并且不修复其余部分,你将得到一个字符串 8 次,但每次运行程序时它都会是不同的(而现在,即使是您获得的一个字符串也将与您每次运行该程序时相同)。
关于C++ 打印随机数据对的小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966742/