0

I'm creating a piece of code which is supposed to simulate a guessing game in which I pull a random line from a text file, in this example "premier league football teams", then I have to take the random line and have it convert into the corresponding numbers in the alphabet, so in the example of "arsenal" I would have to output "1 18 19 5 14 1 12". as this is a game the user has to guess that the numbers mean "arsenal" and then type that in to gain a point and continue. so far I have been able to code a method of pulling a random line from the file, however I am unsure how I would be able to then convert that code into numbers without outputting the answer, as this is meant to be a guessing game.

this is the relevant section of text, but to keep this short I will not paste the full code

    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];

I am assuming that I will have to do a switch statement, but I do not know how to apply that case statement to the randomly selected line, and then have the user input the plain english text

NEW CODE WITH HELP FROM COMMENTS

#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.\n\n\n";
    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 letter\n\n";
    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
    }
Jack Lad
  • 1
  • 2

2 Answers2

0

You can declare a string like std::string alphabet="abcdefghijklmnopqrstuvwxyz"; and then for particular character if you want to find the equivalent position number you can use str.find(char); to get index of char in str and then add one to get its position number.

For eg;

#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
    }
}

[Note] : Just for example I have assigned random_number to 0 and made a sample vector lines so that you can have more clarity on how to use this method in your case.

Spidey13
  • 58
  • 6
  • No need for this. Also, this is impossible, an array must have a constant value. https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value –  Apr 14 '21 at 16:25
  • thank you this is incredibly useful, however how would I create it so that the randomly selected line is used, as there is no guarantee arsenal will be the random output. – Jack Lad Apr 14 '21 at 16:26
  • @Eduardo, thanks for pointing out. fixed it. But what did you mean by "No need". No need of what? And why? – Spidey13 Apr 14 '21 at 18:07
  • @Jack I edited my answer according to your query. – Spidey13 Apr 14 '21 at 18:07
  • im getting multiple errors because i have no idea how to integrate you code into my own :( any help in pointing out what exactly isn't working would be miraculous. I've edited my original comment and added a section with the new code – Jack Lad Apr 15 '21 at 17:18
0

the char datatype have a numerical value and are displayed as a character. We make use of this!

Lowercase a = 97, lowercase b = 98, etc. So if you subtract 96 and then cast it to an interger using (int), you will be all set :)

Pigskin
  • 1
  • 1