Ive been asked to create a poem generator that creates 3 line stanzas. Im using a txt file that has all the words i want to use in it with one word per line. The poem should follow a general structure each time it runs. Ive got a loop up and running that goes through all the words and picks out 12 random ones which is what i need. Problem is i cant manage to save each of these words into an array so I can print them out later in a certain order.
Any help would be much appreciated! :)
// The program generates a poem using a .txt file to find different types of words
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h> //For random number generation
#include <stdlib.h> //For random number generation
using namespace std;
int main ()
{
string Filename;
ifstream inFile;
string inputString;
int i = 0;
int noun1,noun2,noun3,adj1,adj2,adj3,prep1,prep2,verb1,verb2,verb3,adverb1;
int lineno;
string array [12];
cout << "Please enter the filename that you want to open." << endl;
cin >> Filename; //takes in name of .txt file full of words
inFile.open (Filename.c_str()); //opens .txt file
getline(inFile, inputString);
noun1 = ((rand() % 50)); //generates random number between 1-50
noun2 = ((rand() % 50)); //and applies it to variable
noun3 = ((rand() % 50));
adj1 = (50 +(rand() % 50)); //generates random number between 51-100
adj2 = (50 +(rand() % 50)); //and applies it to variable
adj3 = (50 +(rand() % 50));
prep1 = (100 + (rand() % 45)); //generates random number between 101-145
prep2 = (100 + (rand() % 45)); //and applies it to variable
verb1 = (145 +(rand() % 50)); //generates random number between 146-195
verb2 = (145 +(rand() % 50)); //and applies it to variable
verb3 = (145 +(rand() % 50));
adverb1 = (195 +(rand() % 50)); //generates random number between 196-245 and applies it to variable
{for (lineno = 1; getline (inFile,inputString) && lineno <= 245; lineno++) //loops through the 245 words in the file
{ //looking for the numbers previously randomised
if (lineno == noun1 || lineno == noun2 || lineno == noun3
|| lineno == adj1 || lineno == adj2 || lineno == adj3 //if one of the numbers comes up which all 12
|| lineno == prep1 || lineno == prep2 //of them will then the next function is carried out
|| lineno == verb1 || lineno == verb2 || lineno == verb3
|| lineno == adverb1)