Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I have a very easy question for you guys out there. It's been a while since I took my programming courses (switched majors) but am trying to get my feet wet again.

Objective:

##Objective: II will be creating a program that will output 10 random full names (and have it be different every time the program is run). Before I complete it, I'd like some feedback on the first iteration, which just chooses random first names.

Info:

##Info: II have a file called firstnames.txt, containing a list of 10 first names.

Premise:

##Premise: I'mI'm a beginner and I/O really throws me. I feel this is a basic enough program for me to learn from, and with your advice, I'll be able to copy/paste the same code to get a last name.

Thanks!

Here's what I have so far:

#include <windows.h>
#include <cstdlib>
#include <iostream> 
#include <string> //random headers because it's been awhile :P
#include <fstream>
#include <time.h>
using namespace 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;
}

My firstnames.txt looks like:

Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole

My lastnames.txt will look similar:

Johnson
Lewis
Walker
Robinson
Wood
Thompson
White 
Watson
Jackson
Wright

(Another question I guess would be is the way I formatted the names (each name on a new line) a bit over complicated in the text file? Like would it be better/shorter to just separate each name by a space or something?)

Thanks again!

I have a very easy question for you guys out there. It's been a while since I took my programming courses (switched majors) but am trying to get my feet wet again.

##Objective: I will be creating a program that will output 10 random full names (and have it be different every time the program is run). Before I complete it, I'd like some feedback on the first iteration, which just chooses random first names.

##Info: I have a file called firstnames.txt, containing a list of 10 first names.

##Premise: I'm a beginner and I/O really throws me. I feel this is a basic enough program for me to learn from, and with your advice, I'll be able to copy/paste the same code to get a last name.

Thanks!

Here's what I have so far:

#include <windows.h>
#include <cstdlib>
#include <iostream> 
#include <string> //random headers because it's been awhile :P
#include <fstream>
#include <time.h>
using namespace 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;
}

My firstnames.txt looks like:

Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole

My lastnames.txt will look similar:

Johnson
Lewis
Walker
Robinson
Wood
Thompson
White 
Watson
Jackson
Wright

(Another question I guess would be is the way I formatted the names (each name on a new line) a bit over complicated in the text file? Like would it be better/shorter to just separate each name by a space or something?)

Thanks again!

I have a very easy question for you guys out there. It's been a while since I took my programming courses (switched majors) but am trying to get my feet wet again.

Objective:

I will be creating a program that will output 10 random full names (and have it be different every time the program is run). Before I complete it, I'd like some feedback on the first iteration, which just chooses random first names.

Info:

I have a file called firstnames.txt, containing a list of 10 first names.

Premise:

I'm a beginner and I/O really throws me. I feel this is a basic enough program for me to learn from, and with your advice, I'll be able to copy/paste the same code to get a last name.

Thanks!

Here's what I have so far:

#include <windows.h>
#include <cstdlib>
#include <iostream> 
#include <string> //random headers because it's been awhile :P
#include <fstream>
#include <time.h>
using namespace 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;
}

My firstnames.txt looks like:

Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole

My lastnames.txt will look similar:

Johnson
Lewis
Walker
Robinson
Wood
Thompson
White 
Watson
Jackson
Wright

(Another question I guess would be is the way I formatted the names (each name on a new line) a bit over complicated in the text file? Like would it be better/shorter to just separate each name by a space or something?)

Thanks again!

Post Reopened by Loki Astari, Toby Speight, mdfst13, Deduplicator, Simon Forsberg
Made the title match the actual code, rather than the future code
Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325

Random First + Last Name Generator? (C++, I/O) Randomly pick a first name from a file

Reduced the description to be clear about what's actually implemented and working
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325

I have a very easy question for you guys out there. It's been awhilea while since I took my programming courses (switched majors) but am trying to get my feet wet again.

Objective: ##Objective: I want to createwill be creating a program that will output 10 random full names (and have it be different every time the program is run). Before I complete it, I'd like some feedback on the first iteration, which just chooses random first names.

Info ##Info: I have a file with a list of 10 first namescalled ("firstnames.txt") and a file withfirstnames.txt, containing a list of 10 lastfirst names ("lastnames.txt")

Premise: ##Premise: I'm a beginner and I/O really throws me. I I feel this is a basic of enough program for me to learn from. Right now I'm just trying, and with your advice, I'll be able to get a randomly generated first name working because once I can do that I'm sure I can just copy/pastpaste the same code to get a last name.

Here's what I have so far (one of my attempts at least):

Also my text files are formatted as suchMy firstnames.txt looks like:

"firstnames.txt"

Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole
Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole

"lastnames.txt"My lastnames.txt will look similar:

Johnson
Lewis
Walker
Robinson
Wood
Thompson
White 
Watson
Jackson
Wright
Johnson
Lewis
Walker
Robinson
Wood
Thompson
White 
Watson
Jackson
Wright

Thanks again, also I came here because it seems more friendly and lively than cplusplus.com!

I have a very easy question for you guys out there. It's been awhile since I took my programming courses (switched majors) but am trying to get my feet wet again.

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.

Here's what I have so far (one of my attempts at least):

Also my text files are formatted as such:

"firstnames.txt"

Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole

"lastnames.txt"

Johnson
Lewis
Walker
Robinson
Wood
Thompson
White 
Watson
Jackson
Wright

Thanks again, also I came here because it seems more friendly and lively than cplusplus.com

I have a very easy question for you guys out there. It's been a while since I took my programming courses (switched majors) but am trying to get my feet wet again.

##Objective: I will be creating a program that will output 10 random full names (and have it be different every time the program is run). Before I complete it, I'd like some feedback on the first iteration, which just chooses random first names.

##Info: I have a file called firstnames.txt, containing a list of 10 first names.

##Premise: I'm a beginner and I/O really throws me. I feel this is a basic enough program for me to learn from, and with your advice, I'll be able to copy/paste the same code to get a last name.

Here's what I have so far:

My firstnames.txt looks like:

Ryan
John
Daniel
Gavin
Frank
Danielle
Lucy
Sarah
Katie
Nicole

My lastnames.txt will look similar:

Johnson
Lewis
Walker
Robinson
Wood
Thompson
White 
Watson
Jackson
Wright

Thanks again!

Post Closed as "Not suitable for this site" by pacmaninbw , alecxe, πάντα ῥεῖ, t3chb0t, mdfst13
Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /