I'm new here, and very new to programming, and for all the time I spent scouring the internet and this site, I can't seem to find an answer to this question of mine. I need to randomly pick a single element out of a list of characters and print it, and then repeat this a user-entered number of times. I'm trying to make a text generator for typing practice with individual keys, not words. (Yes, I know I am so very late to the "typing correctly" party, but my forays into coding made me see it was a more useful skill than I thought.)
So far, I have mostly found answers that use an iterator to print all the elements of a list in a row, which isn't what I want. I also keep coming across people saying to use a vector instead of a list, but my C++ book I started learning from doesn't go into a lot of things that are "too advanced", so I'm stuck there. Using a list seems like a good idea to me though, but it could just be my beginner-ness talking. An array would be too rigid for what I want to accomplish, because if the user could enter in the keys they want in a later version of this program, and not just code them in like I did for the first try, it would be more ideal.
But anyway, my program looks like this now:
// Version 1
// Text Generator for Typing Practice with Individual Keys:
// For inputting into typing practice programs/sites with "insert your text" fields.
// Generates a random list of letters from a list of known keys,
// that only displays up to a user-entered amount of characters,
// separated into paragraphs.
#include <iostream>
#include <cstdlib>
#include <list>
#include <ctime>
using namespace std;
int main () {
//Declare variables used below
int x = 0;
int list_size = 0;
int p = 0;
// Add new letters to the list below as you learn them!
list <char> Keys_Known = {' ', ' ', ' ', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 'e', 'r', 'u', 'i'};
// Define function(s) used below
char get_Text(); {
for (int i = 0; i <= x; ++i) { // For x amount of times
p = srand(time(nullptr)) % list_size;
// Randomly choose a character from the list by its index,
// limited to the total number of elements in the list,
cout << Keys_Known[p];
// and finally output the letter value of the index.
}
}
// Get and set list_size here
list_size = Keys_Known.size();
cout << "Enter total number of characters wanted: " << endl;
cin >> x;
// Set x to the total number of characters wanted in your practice text.
// Maybe about 100 is good? ((Try with 10 for testing!))
cout << get_Text() << "/n" << get_Text() << "/n";
// Print two blocks of random text separated into paragraphs.
return 0;
}
I keep getting errors on the types of int p and int list_size not working with %, which is how I usually limit a random number range. Eclipse also tells me that I can't say 'Keys_Known []' anything because it has no idea what [] is supposed to mean. I even tried putting a 0 in there but it still acts like I can't use indexes. Am I missing an '#include' header?
The exact errors I get are:
"invalid operands of types 'void' and 'int' to binary 'operator%' line 29 C/C++ Problem"
and:
"no match for 'operator[]' (operand types are 'std::__cxx11::list' and 'int') line 33 C/C++ Problem"
Any help would be appreciated, thanks!