Skip to content

Instantly share code, notes, and snippets.

@blast-hardcheese
Created August 8, 2013 09:38
Show Gist options
  • Save blast-hardcheese/6183216 to your computer and use it in GitHub Desktop.
Save blast-hardcheese/6183216 to your computer and use it in GitHub Desktop.
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#define MNAMES 4
#define FNAMES 4
#define LNAMES 4
using namespace std;
int ranNum(int min, int max)
{
int x = rand() % max + min;
return x;
}
string getName(int whichKind)
{
string line = "";
string fname = "";
int maxLines = 0;
if (whichKind == 0)
{
fname = "maleNames.txt";
maxLines = MNAMES;
}
else if (whichKind == 1)
{
fname = "femaleNames.txt";
maxLines = FNAMES;
}
else if (whichKind == 2)
{
fname = "lastNames.txt";
maxLines = LNAMES;
}
cout << "Opening file: " << fname << endl;
ifstream infile(fname.c_str());
if (!infile)
{
cout << "Could not open file: " << fname << endl;
}
else
{
vector<string> lines;
int lineNo = ranNum(1, 5);
while (lineNo != 0) //Skips to the correct line to get name.
{
lines.push_back(line);
lineNo--;
}
getline(infile, line);
if (line == "")
{
cout << "Error: Could not get name." << endl;
}
infile.close();
}
return line;
}
int main(int argc, char argv[]) {
string myName = getName(0);
cout << myName << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment