简体   繁体   中英

“invalid operands to binary expression” error?

C++ newbie here, basically I am trying to print out a list of random characters based off user input. I know my error involves the "m <= num" statement, but not sure how to go about fixing that. Or am I going about this completely wrong? Explanations are greatly appreciated!

Note My logic is that 'm' is the list of characters so for 'num' I am trying to print out a list of characters based on the value of 'num'. For example if the user selects 2 for list and 5 for elements I would want the output to be.... list 1: "wtwef" list 2: "wopoe"

#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;


int main() {
  srand (time(NULL));
  int L;

  string alpha = "abcdefghijklmnopqrstuvwxyz";
  list<string> m;

  cout << "Give the number of lists: ";
  cin >> L;

  for (int i = 1; i <= L; i++){
    cout << "Give the number of elements for list " << L << " : "<< endl;
    int num; cin >> num;

    for (int k = 0; m <= num; k++){
      cout << alpha[rand() %100] << " " << endl;
      m.push_back(alpha[rand() %100]);

}

  cout << "Give a word: " << endl;
  cin >> s;

  }

The issue with the expression "m <= num" is because m is of type std::list while num is of type int. It doesn't make sense to compare a list with an int, which is the real world equivalent of comparing a shopping list with 4, it just doesn't mean anything. You may have intended "k <= num".

The line cin >> s is also an error, s isn't defined.

You also have a line m.push_back(alpha[rand() % 100]); that will be an error, m has a type of string while the using the [] operator on alpha will return a character, see code below for solution.

You use the modulus operator in two places, where you mod by 100. If you want a character at a random index you should, in this case, do alpha.substr((double) rand() / RAND_MAX * alpha.length(), 1) . Note that RAND_MAX is a constant that should be predefined, you shouldn't need to set it. I have a feeling what you're trying to accomplish in the inner loop looks like:

    string randomCharacter = alpha.substr((double) rand() / RAND_MAX * alpha.length(), 1)
    cout << randomCharacter << " " << endl;
    m.push_back(randomCharacter);

You are also missing a left brace in the code somewhere (may be due to copy-paste) which would be another reason it wouldn't compile.

If what you want is to be able to type in the terminal how many sequences of random characters followed by how many characters are in each sequence, you would want to re-write the inner loop to look like:

    string randomCharacters = "";
    for (int k = 0; k <= num; k++){
        string randomLetter = alpha.substr((double) rand() / RAND_MAX * alpha.length(), 1);
        cout << randomLetter << " " << endl;
        randomCharacters += randomLetter;
    }
    m.push_back(randomCharacters);

This will fill the list with strings filled with random characters from array alpha, which you can use later. You can remove the cout if you don't want to see all the characters printed to the terminal as they are randomly chosen.

After your edit to the question: The solution should look like:

    srand(time(NULL));
    int L;

    string alpha = "abcdefghijklmnopqrstuvwxyz";
    vector<string> m;

    cout << "Give the number of lists: ";
    cin >> L;


    cout << "Give the number of elements for the lists: ";
    int num; 
    cin >> num;
    cout << endl;
    for (int i = 1; i <= L; i++){

        string randomCharacters = "";
        for (int k = 0; k < num; k++){
            string randomLetter = alpha.substr((double) rand() / RAND_MAX * alpha.length(), 1);
            randomCharacters += randomLetter;
        }
        m.push_back(randomCharacters);
    }
    for(int i = 0; i < m.size(); i++)
    {
        cout << "list" << (i+1)<< ": " << m[i] << endl;
    }

Note, this solution switched list for vector as it's simpler in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM