所以,我试图创建一个可以写入并用于播种随机数生成器的文本文件,但我需要能够在所有函数中写入并调用该文件。该文件必须以用户的名字和姓氏命名,所以我找到了一些代码,可以创建一个文本文件并将其重命名,我在其他函数中调用该文件时遇到了麻烦。我在创建文本文件的代码之后包含了其他一个函数的开头部分。我需要向文件发送时间戳,并使用该时间为我的RNG播种。
int user_file_name() {
string tstamp = get_timestamp();
//Creating input/output file using user's name
ofstream user_file;
string filename;
cout << "What is your first and last name?\n" << endl;
getline(cin, filename);
filename += ".txt";
cout << "Thank you, " << filename << "." << endl << endl;
user_file.open(filename.c_str());
user_file << tstamp;
user_file.close();
return 0;
}
int addition() {
char DIFFICULTY;
difficulty_menu();
cin >> DIFFICULTY;
get_timestamp();
string tstamp = get_timestamp();
答案 0 :(得分:0)
为什么不让user_file_name
返回string
文件名而不仅仅是int?然后,您可以将该文件名存储在某处,以便程序的其他区域可以访问它。