The trouble is, when I attempt to write the array to a text file, it ends up with only the last string in the array actually in the text file.
Here is the important code:
if(timesToRepeat>1)
{
//Create an array to output PRODUCT to, with a maximum to provide enough leeway for any user.
string outputarray[100];
outputarray[currposition] = product;
//increment currposition for next write
currposition++;
//To check for completion, create a check integer based on timesToRepeat
int checkint;
checkint = timesToRepeat -1;
//If completed, write to file
if(currposition==checkint)
{
fstream loopout(text,ios::app);
loopout<<outputarray[100];
loopout.close();
}
And the context:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main(int argc, char *argv[])
{
//"Database" for random combinations. IMPORTANT: All strings MUST have a
//space after them as none are added later
//Be sure that the AMOUNT OF STRINGS matches the table's RAND value. See below, after FINAL
string title[] = {"A Study On ", "On the Proceedings Of ", "In-Depth Analysis Of ",
"Analysis Of ", "Notes On ", "The Present-Day Worth Of ",
"Recent Reasearch On ", "Refinement Of ", "Efficient Use Of ",
"The Effects Of ", "Towards ", "A Visualization Of ",
"A Case For ", "Present-Day Application Of ", "Improvement Of ",
"Analyzing ", "Reinforcing the Use Of ", "An Exploration Of ",
"Comparing ", "Contrasting ", "A Methodology For ",
"Constructing ", "On the Design Of ", "Adapting Society for the Use Of ",
"The Impact Of ", "Discussion On ", "Improving ",
"Improving the Use Of ", "Investigating ", "An Investigation Of ",
"Experimentation On ", "On The Use Of ", "Hypotheses On "};
//TITLE's CURRENT AMOUNT OF STRINGS: 33. PER-LINE: 3
string conjunction[] = {"The "};
//CONJUNCTION's CURRENT AMOUNT OF STRINGS: 2
string adjective[] = {"Present-Day ", "Northern ", "Eastern ", "Western ",
"Southern ", "Northwestern ", "Northeastern ", "Southeastern ",
"Southwestern ", "Postwar ", "Intentional ", "Non-Intentional ",
"Disruptive ", "Non-Disruptive ", "Subtle ", "Problematic ",
"Exclusive ", "Varied ", "Differring ", "Yielding ",
"Contrastable ", "Comparable ", "Scarred ", "Cellular ",
"Ocular ", "Aboriginal ", "Cardiovascular "};
//ADJECTIVE's CURRENT AMOUNT OF STRINGS: 27 PER-LINE: 4
string noun[] = {"Bullfrog ", "Tadpole ", "Farmer ", "Genealogy ", "Bacteria ",
"Bacillus ", "Cell ", "Dissection ", "Aborigine ", "Native People ",
"Surgery ", "Clinic ", "Eyewear ", "Westerner ", "Hand ",
"Sense ", "Hair ", "Follicle ", "Clock ", "Radiation ",
"Glass ", "Computer ", "Electronic Device ", "Device ", "Interface ",
"Photograph ", "Disease ", "Bomb ", "Videotape ", "Building Material ",
"Box ", "Bar Graph ", "Pie Chart ", "Line Graph ", "Graph ",
"Chart ", "Plot ", "Box-and-Whisker Plot ", "Memory ", "Brain ",
"System ", "Ecosystem ", "Hydraulic Power Plant ", "Power Plant ", "Neutral Zone ",
"Aurora Borealis ", "Latitude Indicator "};
//NOUN's CURRENT AMOUNT OF STRINGS: 47 PER-LINE: 5
//NOUN must be SINGULAR
string joiner[] = {"Of ","From ","Originating In ",
"Caused By Actions in the Vicinity of "};
//JOINER's CURRENT AMOUNT OF STRINGS: 4 PER-LINE: 3
string final[] = {"Alaska ", "Montana ", "Canada ","Berlin, Germany "};
//FINAL's CURRENT AMOUNT OF STRINGS: 4 PER-LINE: 4
//Repeat times variable and default
int timesToRepeat;
timesToRepeat = 1;
cout<<"Please enter how many times you would like to generate a title. Maximum 100. All will be written to a file.";
cin>>timesToRepeat;
/*Create a variable to track array output in the multiple-times loop and avoid overwrites.
Start it at position 0.*/
int currposition;
currposition = 0;
//Create a variable to store the decision to display the results automatically or keypress spaced
//And create a while loop ensure valid input
int automatic;
automatic = 0;
while(automatic>2||automatic<1)
{
cout<<"Do you want this to be 1: automatic or 2: keypress spaced?";
cin>>automatic;
}
//Since fstream only accepts char* as filenames, declare a char* and name it output.txt
char* text;
text = "output.txt";
cout<<"The output file will be output.txt and reside in the folder where this executable is located. Press any key to generate...";
cin.get();
//Added to seed to achieve different random numbers each run
int addtoseed;
addtoseed = 0;
while(timesToRepeat>0)
{
//Seed RAND() with time, added with our incremented addtoseed to create different random numbers each loop
int seeder;
seeder = time(0) + addtoseed;
srand(seeder);
//Generate random numbers for use in word selection,
//FORMAT: <array name>rand = rand()%<amount of strings in array>
int titlerand = rand()%33;
int conjunctionrand = rand()%1;
int adjectiverand = rand()%27;
int nounrand = rand()%47;
int joinerrand = rand()%4;
int finalrand = rand()%4;
//Create the finished PRODUCT -a gibberish title string with random content
//and a newline, print to screen
string product = title[titlerand] + conjunction[conjunctionrand] + adjective[adjectiverand] + noun[nounrand] + joiner[joinerrand] + final[finalrand] + "\n";
cout<<product;
if(timesToRepeat>1)
{
//Create an array to output PRODUCT to, with a maximum to provide enough leeway for any user.
string outputarray[100];
outputarray[currposition] = product;
//increment currposition for next write
currposition++;
//To check for completion, create a check integer based on timesToRepeat
int checkint;
checkint = timesToRepeat -1;
//If completed, write to file
if(currposition==checkint)
{
fstream loopout(text,ios::app);
loopout<<outputarray[100];
loopout.close();
}
}else{
fstream fileout(text,ios::out);
fileout<<product;
fileout.close();
}
//Decrement timesToRepeat to track how many times this loop has been repeated.
timesToRepeat--;
//Increment addtoseed to get different random number next time
addtoseed++;
if(automatic==2)
{
cout<<"Press ENTER to continue.";
cin.get();
}
}
//exit sequence
cout<<"Press any key to exit...";
cin.get();
return EXIT_SUCCESS;
}

New Topic/Question
Reply




MultiQuote




|