lundi 28 septembre 2015

RNG in loop and printing word from string array

I'm learning C++ and trying to generate a new Random number between 0 and 49 for each pass of the for loop in this program. Which I then need it to print the corresponding word at that point in the string array. I'm having trouble getting it to make a new random number each loop, and also with how to get it to print the word at that point in the array, and not just the letter.

Thank you ahead of time for any suggestions

#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <random>

using namespace std;

int generateRandom() {
default_random_engine generator;
uniform_int_distribution<int> distribution(0, 49);
int random = distribution(generator);  // generates number in the range 0..49
return random;
}

int main() {
ofstream outFile;
ifstream inFile;
string words;
srand(time(NULL));

int Random2 = rand() % 7 + 1;
inFile.open("words.txt");
if (!inFile.is_open()) { //tests to see if file opened corrected
    exit(EXIT_FAILURE);
}
while (getline(inFile, words)) { //Puts file info into string
    inFile >> words;
}
for (int i = 0; i < Random2; i++) {

    cout << words[generateRandom()];
}

cin.get();
}




Aucun commentaire:

Enregistrer un commentaire