So I'm trying to read names from a file and output it to another file based on number of names I want but when the program is run I get no output. I am reading from a file "names.txt". Any reason why I dont get any output and how can I improve my code? I'm kinda new to c++ so any feedback is greatly appreciated.
//Purpose: Generate a file of students
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> // for rand()
#include <time.h>
void GenerateFile(std::fstream &, std::string *, const int,
std::string *, std::string *, std::string *);
void randomizeNames(std::string *, const int);
int main()
{
int numRecords = 0;
srand(time(NULL));
std::cout << "Please enter the number of student records you want in your file: ";
std::cin >> numRecords;
while(numRecords > 120 || numRecords < 1)
{
std::cout << "You cannot create more than 120 records!"
<< "\nThat exceeds the number of available trays\n";
std::cout << "Please enter the number of student records you want generated: ";
std::cin >> numRecords;
}
std::string Schools[] = {"Eastville", "Westburg", "Northton", Southport", "Jahunga", "Podunk"};
std::string Entrees[] = {"Chicken", "Fishsticks", "Lasagna"};
std::string Desserts[] = {"Cheesecake", "Pudding"};
char InFile[] = "names.txt";
char OutFile[] = "Students.txt";
std::fstream Input(InFile, std::ios::in);
std::fstream Output(OutFile, std::ios::out);
std::string names[120];
int i = 0;
std::string s;
while(!Input.eof())
getline(Input, names[i++]); // Discards newline char
randomizeNames(names, i);
GenerateFile(Output, names, numRecords, Schools, Entrees, Desserts);
Input.close();
Output.close();
std::cout << "The file " << OutFile << " was created for you.\n\n";
return 0;
}
void GenerateFile(fstream &Out, string* name, const int records,
string* School, string* Entree, string* Dessert )
{
int seed = rand();
Out << seed << std::endl;
for(int i = 0; i < records; i++)
{
Out << name[i] << std::endl;
Out << School[rand() % 6 ] << std:endl; // select a random school
Out << (rand() % 10) << std::endl; // select a random number of ounces between 0-10
Out << Entree[rand() % 3 ] << std::endl; // select random entree
Out << Dessert[rand() % 2 ] << std::endl; // select random dessert
}
Out << "Done" << std::endl;
}
void randomizeNames(string *arrayOfNames, const int arraySize)
{
std::string hold;
int randomPosition;
for(int i = 0; i < arraySize; i++)
{
randomPosition = rand() % arraySize;
hold = arrayOfNames[i];
arrayOfNames[i] = arrayOfNames[randomPosition];
arrayOfNames[randomPosition] = hold;
}
}