1

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;
    }
}
Hexx
  • 11
  • 2
  • Unrelated: There is no need to `char InFile[] = "names.txt";` and then `std::fstream Input(InFile, std::ios::in);`. You could `std::fstream Input("names.txt", std::ios::in);`. You can also `std::ifstream Input("names.txt");` Because a `ifstream` is always input. It also eliminates the possibility of accidentally trying to write to the stream because the compiler won't let you. – user4581301 Dec 03 '19 at 22:44
  • Probably unrelated: `while(!Input.eof())` is one of those things in life that sounds simple and logical until you stop and think about it: How can you tell if you're reached the end of the file BEFORE you look and see? It fools just about everyone at the beginning. See [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) for more information and common solutions to the problem. – user4581301 Dec 03 '19 at 22:47
  • 1
    Don't forget to prefix everything that needs it with `std::` – user4581301 Dec 03 '19 at 22:51
  • 1
    One of these strings, `"Eastville", "Westburg", "Northton", Southport", "Jahunga", "Podunk"` is not like the others. – user4581301 Dec 03 '19 at 22:54
  • 1
    On the more "advanced" front, try to avoid `srand` and `rand`. Back in days when men killed one another over half a K of RAM and a single MHz of CPU clocking power was a treasure beyond rubies, `rand` was the best we could do. These days prefer to use [the library](https://en.cppreference.com/w/cpp/numeric/random), `std::uniform_int_distribution` in this case. – user4581301 Dec 03 '19 at 23:01
  • 1
    Rather than a fixed-size array like `std::string names[120];`, [look into using `std::vector`](https://en.cppreference.com/w/cpp/container/vector) – user4581301 Dec 03 '19 at 23:04
  • To find your actual problem, consider manufacturing a [mcve]. If you still have the problem once you've finished with the [mcve], and you usually don't, post the [mcve] and we can help you out better. – user4581301 Dec 03 '19 at 23:06
  • You may want to use const pointers: `void GenerateFile(fstream &Out, string* name, const int records, string* School, string* Entree, string* Dessert )` Also, the scope resolution operator is only one : at one place! – MiCo Dec 03 '19 at 23:21
  • In line with what others are saying, don't roll your own random picker. Use something like [`std::sample`](https://en.cppreference.com/w/cpp/algorithm/sample) (C++17). – tadman Dec 03 '19 at 23:54
  • Unrelated: Include the C++ headers `` and `` instead of the C headers - or better, use `` as already suggested. – Ted Lyngmo Dec 04 '19 at 00:25
  • Your program has typos and missing lines and can't be compiled. Fix that and put the updated code in the question. – Ted Lyngmo Dec 04 '19 at 00:30
  • I didn't even know `std::sample` existed. Truly the Standard Library is a thing of wonders. – user4581301 Dec 04 '19 at 17:43

0 Answers0