Need help with loops and arrays please

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)


//{array [i] = inputString;
//cout << array[i];
//i++;
//}


cout << inputString << endl;



}
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main(){

	string s1 = "Hello";
	string s2 = " Mom";
	string s3 = " World";
	string s4[2];

	s4[0] = s4[1] = s1;

	s4[0].append(s2);

	s4[1].append(s3);

	for (int i = 0; i < 2; i++){
		cout << s4[i] << endl;
	}

	return 0;
}
Last edited on
Thanks Mobotus big help :) How do you put the code into that blue box? i realise mine looks very untidy!
Topic archived. No new replies allowed.