NameGenerator.h assumes that you have already included fstream and iostream. A header file should include whatever it needs.
Code in the header file should be declared as inline. Otherwise if you include the header file from multiple compilation units, you'll get multiple definitions of that code.
firstAndLastName is unused.
In GenerateRandomName(): why do you randomly shuffle the name lists multiple times? And why do you care if isExportingNames is set? Are here we call that a "computed come-from" because the code has to know where it's being called from. And why is it called GenerateRandomName when it doesn't actually generate names at all? This should be called shuffleNames() and it should just shuffle the names.
Factor out common code in LoadNames. By doing this you can do away from ClearVectorInitValues():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
static void loadFromFile(const char *name, std::vector<std::string> &vec)
{
vec.clear();
std::ifstream theFile(name);
std::string str;
while (getline(theFile, str, '\n')) {
vec.push_back(str);
}
theFile.close();
}
void
NameGenerator::LoadNames()
{
loadFromFile("First Names.txt", firstNameList);
loadFromFile("Last Names.txt", lastNameList);
}
|
A function should do only one thing. ExportNames() should export whatever names are currently in the collection. It should not call LoadNames().
ExportNames should take a stream parameter. All the code that asks about the file name should be in the caller.
DisplayNames() is poorly named since it only displays one name.
main() line 39: how do you ever exit this loop?
main() line 59: Something went wrong? What went wrong? What can the user do to correct it? Try to get in the habit of reporting exactly what went wrong and what can be done.
The code could be made more efficient by reading the names just once.
Here is an alternative. This is all in one file, but it's easy enough to split it out.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#ifndef NAMEGENERATORCLASS_H_INCLUDED
#define NAMEGENERATORCLASS_H_INCLUDED
#include <fstream>
#include <iostream>
#include <algorithm>
class NameGenerator
{
public:
NameGenerator()
{
LoadNames();
}
~NameGenerator();
void shuffle();
void Display(std::ostream &);
private:
std::vector < std::string > firstNameList;
std::vector < std::string > lastNameList;
void LoadNames();
};
NameGenerator::~NameGenerator()
{
}
void
NameGenerator::shuffle()
{
std::random_shuffle(firstNameList.begin(), firstNameList.end());
std::random_shuffle(lastNameList.begin(), lastNameList.end());
}
static void loadFromFile(const char *name, std::vector<std::string> &vec)
{
vec.clear();
std::ifstream theFile(name);
std::string str;
while (getline(theFile, str, '\n')) {
vec.push_back(str);
}
theFile.close();
}
void
NameGenerator::LoadNames()
{
loadFromFile("First Names.txt", firstNameList);
loadFromFile("Last Names.txt", lastNameList);
}
void
NameGenerator::Display(std::ostream &os)
{
for (unsigned i = 0; i < firstNameList.size(); ++i) {
os << i+1 << ". " << firstNameList[i]
<< " "
<< lastNameList[i]
<< '\n';
}
}
#endif // NAMEGENERATORCLASS_H_INCLUDED
/*/////////////////////////
//Name Generator Program //
//6/15/2016 @ 9:51 PM //
//Written by Chay Hawk //
*//////////////////////////
#include <string>
#include <vector>
#include <stdlib.h>
#include <time.h>
// #include "NameGeneratorClass.h"
int
main()
{
std::cout << "Random Name Generator Version 2.2 - r38\n" << std::endl;
NameGenerator NG;
srand(time(0));
int choice = 0;
while (choice != 3) {
std::cout << "1. Randomize Names" << std::endl;
std::cout << "2. Display names" << std::endl;
std::cout << "3. Export names" << std::endl;
std::cout << "4. Exit" << std::endl;
std::cout << ">";
std::cin >> choice;
std::cin.clear();
std::cin.ignore(500, '\n');
switch (choice) {
case 1:
NG.shuffle();
break;
case 2:
NG.Display(std::cout);
break;
case 3:
{
std::ofstream exportNames("Exported Names.txt");
NG.Display(exportNames);
exportNames.close();
}
break;
case 4:
return 0;
break;
default:
std::cout << choice << "is not a valid choice" << std::endl;
}
}
return 0;
}
|