Objective:
I want to create a program that will output 10 random full names (and have it be different every time the program is run)
Info:
I have a file with a list of 10 first names ("firstnames.txt")
and a file with a list of 10 last names ("lastnames.txt")
Premise:
I'm a beginner and I/O really throws me. I feel this is a basic of enough program for me to learn from. Right now I'm just trying to get a randomly generated first name working because once I can do that I'm sure I can just copy/past the same code to get a last name.
Thanks!
Here's what I have so far (one of my attempts at least):
#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
usingnamespace std;
int main()
{
//Variable declarations
int a = 0;
int randNum;
ifstream myFile;
myFile.open("firstnames.txt");
string names[10]; // holds the 10 names in the file.
string randomNames[10];// holds the randomly generated names FROM the file.
while (myFile.good())
{
getline(myFile, names[a]); // reads the names from the text file, into an array named "names[]."
a++;
}
for (int i = 0; i < 10; i++) // makes this program iterate 10 times; giving you 10 random names.
{
srand( time(NULL) ); // seed for the random number generator.
randNum = rand() % 10 + 1; // gets a random number between 1, and 10.
randomNames[i] = names[randNum];
}
for (int i = 0; i < 10; i++)
{
cout << randomNames[i] << endl; // outputs all 10 names at once.
}
myFile.close();
return EXIT_SUCCESS;
}
Also my text files are formatted as such:
1 2 3 4 5 6 7 8 9 10
Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole
(Is this the easier way to format it? Or would it be better to have just one whitespace?)