将文本文件中的随机行转换为数字,然后输出数字,用户必须猜测原始行



我正在创建一段代码,它应该模拟一个猜谜游戏,在这个游戏中,我从文本文件中随机抽取一行,在这个例子中"英超足球队";,然后我必须取随机线并将其转换为字母表中的相应数字;阿森纳;我将不得不输出";1 18 19 5 14 1 12";。由于这是一个游戏,用户必须猜测数字的意思是";阿森纳;然后输入以获得一分并继续。到目前为止,我已经能够编写一种从文件中随机抽取一行的方法,但我不确定如何在不输出答案的情况下将代码转换为数字,因为这是一场猜谜游戏。

这是文本的相关部分,但为了简短起见,我不会粘贴完整的代码

vector<string> lines;
srand(time(NULL));
//ensures random selection on each new code run
ifstream file("Premier_League_Teams.txt");
//pulls the file from the directory
int total_lines = 0;
while (getline(file, line))
//this function counts the number of lines in a text file
{
total_lines++;
lines.push_back(line);
}
int random_number = rand() % total_lines;
//selects a random entry from the text file
//end of adapted code
cout << lines[random_number];

我假设我必须做一个switch语句,但我不知道如何将该case语句应用于随机选择的行,然后让用户输入纯英文文本

来自注释的帮助下的新代码

#include <fstream>
//fstream included to pull answers from category files
#include <string>
#include <vector>
#include <list>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
cout << "Richard Osman's house of Games: This round is in code.nnn";
cout << "The objective of this round is to unscramble the coded word, you will be given the category as the clue and you have to type out what you belive the answer to be, with a capital letternn";
cout << endl;
string Name;
cout << "But First, Enter your first name.n";
cin >> Name;
cout << "Welcome contestant " << Name << " Are you ready to begin the quiz?n";
// this is to add a level of immersion to the game, by allowing the code to recall the players name so that they have a personalised experience
string respond;
cout << "please type Yes or No (case sensitive).n";
cin >> respond;
if (respond == "Yes")
{
cout << "nGood luck!";
}
else
{
cout << "Maybe next time!";
return 0;
}
{   cout << "The first category is..." << endl;
cout << "Premier League Football Teams!" << endl;
//code adapted from Ranjeet V, codespeedy
string line;
vector<string> lines;
srand(time(NULL));
//ensures random selection on each new code run
ifstream file("Premier_League_Teams.txt");
//pulls the file from the directory
int total_lines = 0;
while (getline(file, line))
//this function counts the number of lines in a text file
{
total_lines++;
lines.push_back(line);
}
int random_number = rand() % total_lines;
//selects a random entry from the text file
//end of adapted code
int random_number = 0;
vector<string> lines;
lines.push_back("abc arsenal");
string alpha = "abcdefghijklmnopqrstuvwxyz";
string word = lines[random_number];
//line from lines vector for which we will calculate numbers
int* codes;
codes = new int[word.length()];
//array in which we will store codes for letters
for (int i = 0; i < word.length(); i++) {
//iterate over each characte of string word
if (word[i] != ' ')
//as we dont want to replace spaces with numbers or somethings, but it will automatically become 0 as it is default
codes[i] = alpha.find(word[i]) + 1;
//assign codes correspoding element to (index of char)+1
}
for (int i = 0; i < word.length(); i++) {
if (codes[i] == 0) {
continue;
//do not print codes' element if it is zero because it will become zero for spaces
}
cout << codes[i] << " ";
//outputting each codes element with a space in between
}

char数据类型有一个数值,并显示为字符。我们利用这个!

小写a=97,小写b=98,等等。所以,如果你减去96,然后用(int(将其强制转换为整数,你就会被设置为:(

您可以声明一个字符串std::string alphabet="abcdefghijklmnopqrstuvwxyz";,然后对于特定的字符,如果你想找到等效的位置号,你可以用str.find(char);得到charstr中的索引,然后加一得到它的位置号。

例如;

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int random_number=0;
vector<string> lines;
lines.push_back("abc arsenal");
string alpha="abcdefghijklmnopqrstuvwxyz";
string word=lines[random_number]; //line from lines vector for which we will calculate numbers
int* codes;
codes=new int[word.length()]; //array in which we will store codes for letters
for(int i=0;i<word.length();i++) { //iterate over each characte of string word
if(word[i]!=' ') //as we dont want to replace spaces with numbers or somethings, but it will automatically become 0 as it is default
codes[i]=alpha.find(word[i])+1; //assign codes correspoding element to (index of char)+1
}
for(int i=0;i<word.length();i++) {
if(codes[i]==0) {
continue; //do not print codes' element if it is zero because it will become zero for spaces
}
cout<<codes[i]<<" "; //outputting each codes element with a space in between
}
}

[注意]:例如,我将random_number赋值为0,并制作了一个样本向量lines,这样您就可以更清楚地了解如何在您的案例中使用此方法。

最新更新