#Grabbing a random line seperated by comma's

7 messages · Page 1 of 1 (latest)

viscid hemlock
#

My code:
std::string word = "";
std::string letterAmount = "";
std::string descriptionWord = "";
if (file2.is_open()) {

  std::getline(file2, line);
  int commaCount = 0;

  for (int i = 0; i < line.length(); i++) {
    
    std::cout << "line: " << line[i] << std::endl;
    if (line[i] == ',') {
      commaCount++;
    } else {
      if (commaCount == 0) {
        word += line[i];
      } else if (commaCount == 1) {
        letterAmount += line[i];
      } else if (commaCount == 2) {
        if (line[i] != '$') {
          descriptionWord += line[i];
        }
      }
    }
  
    
    
  }

   
  SimonSaysText->label("The " + letterAmount +
                       " letter word I am thinking of is\n " +
                       descriptionWord);

  file2.close();
  std::cout << "2" << line << std::endl;
}

Ok so I have this lab due in a week and I want my code to grab a random line from my file but the lines are like this
Rain,4,Water falling from the sky.$
Book,4,A set of written or printed pages.$
Tree,4,A tall plant with branches and leaves.$
I want my code to grab a random line from one these three using the "descriptionWord" and "letterAmount " as hints for the user so when the user puts their answer in the input box it should match with the "word"

torpid hedgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

midnight linden
#

!code

torpid hedgeBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
midnight linden
#

Might be a lot more efficient ways of doing this, but what I'm thinking to grab the random line is to add all the lines to a vector<vector<string>> and that might make it easier

viscid hemlock
#

I was able to figure it out but thanks anyway

#

!solved