7-17 Name Search. NEED HELP WITH CODE

I need help with a code. I am new to C++ and need help with this code so I can better understand the language. I have been stuck on this code and any help would be great...

To start, I was given two .txt files to use for this code:

* GirlNames.txt - This file contains a list of the 200 most popular names given to girls born in the United States from 2000 to 2009.
* BoyNames.txt - This file contains a list of the 200 most popular names given to boys born in the United States from 2000 to 2009.

Write a program that reads the contents of the two files into two separate arrays or vectors. The user should be able to enter a boy's name, a girl's name, or both, and the application should display messages indicating whether the names were among the most popular.
What exactly do you need help with? Are you stuck on using files, arrays, vectors, or something else? It's easier for us to help when we you have a specific issue you need help with. Nonetheless, here is a simple example to help get you going and if you have specific issues please post them here.


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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main() {
	std::vector<std::string> items;
	std::ifstream data("ExampleData.txt", std::ifstream::in);
	
	if (data.fail()) {
		return EXIT_FAILURE;
	}

	do {
		items.emplace_back();
	} while (std::getline(data, items.back()));
	items.pop_back();

	std::cout << "ExampleData contains the following:\n";
	for (const auto& item : items) {
		std::cout << item << '\n';
	}
	
	std::cin.get();
	return EXIT_SUCCESS;
}


exampleData.txt
Line one
Line two
Something else
Another one
Finally top it off
Last edited on
Where are the files GirlNames.txt and BoyNames.txt? And have you written a function main yourself?
ill post them in a sec. I am getting confused on the order to put everything in. I understand the basics of it, but our teacher is so fast and hard to reach and I'm in the middle of moving so I am trying to catch up and understand everything. I have also been having problems getting files to display using C++. even after using codes in my book it still wont work. not sure if its my comp. or program. I'm using Microsoft visual studios. I will post both files below.
GirlNames.txt:

input[pb-role]::-ms-clear{ display:none;
}Emily
Madison
Emma
Olivia
Hannah
Abigail
Isabella
Samantha
Elizabeth
Ashley
Alexis
Sarah
Sophia
Alyssa
Grace
Ava
Taylor
Brianna
Lauren
Chloe
Natalie
Kayla
Jessica
Anna
Victoria
Mia
Hailey
Sydney
Jasmine
Julia
Morgan
Destiny
Rachel
Ella
Kaitlyn
Megan
Katherine
Savannah
Jennifer
Alexandra
Allison
Haley
Maria
Kaylee
Lily
Makayla
Brooke
Mackenzie
Nicole
Addison
Stephanie
Lillian
Andrea
Zoe
Faith
Kimberly
Madeline
Alexa
Katelyn
Gabriella
Gabrielle
Trinity
Amanda
Kylie
Mary
Paige
Riley
Jenna
Leah
Sara
Rebecca
Michelle
Sofia
Vanessa
Jordan
Angelina
Caroline
Avery
Audrey
Evelyn
Maya
Claire
Autumn
Jocelyn
Ariana
Nevaeh
Arianna
Jada
Bailey
Brooklyn
Aaliyah
Amber
Isabel
Danielle
Mariah
Melanie
Sierra
Erin
Molly
Amelia
Isabelle
Madelyn
Melissa
Jacqueline
Marissa
Shelby
Angela
Leslie
Katie
Jade
Catherine
Diana
Aubrey
Mya
Amy
Briana
Sophie
Gabriela
Breanna
Gianna
Kennedy
Gracie
Peyton
Adriana
Christina
Courtney
Daniela
Kathryn
Lydia
Valeria
Layla
Alexandria
Natalia
Angel
Laura
Charlotte
Margaret
Cheyenne
Mikayla
Miranda
Naomi
Kelsey
Payton
Ana
Alicia
Jillian
Daisy
Mckenzie
Ashlyn
Caitlin
Sabrina
Summer
Ruby
Rylee
Valerie
Skylar
Lindsey
Kelly
Genesis
Zoey
Eva
Sadie
Alexia
Cassidy
Kylee
Kendall
Jordyn
Kate
Jayla
Karen
Tiffany
Cassandra
Juliana
Reagan
Caitlyn
Giselle
Serenity
Alondra
Lucy
Kiara
Bianca
Crystal
Erica
Angelica
Hope
Chelsea
Alana
Liliana
Brittany
Camila
Makenzie
Veronica
Lilly
Abby
Jazmin
Adrianna
Karina
Delaney
Ellie
Jasmin
BoyNames.txt:

input[pb-role]::-ms-clear{ display:none;
}Jacob
Michael
Joshua
Matthew
Daniel
Christopher
Andrew
Ethan
Joseph
William
Anthony
David
Alexander
Nicholas
Ryan
Tyler
James
John
Jonathan
Noah
Brandon
Christian
Dylan
Samuel
Benjamin
Zachary
Nathan
Logan
Justin
Gabriel
Jose
Austin
Kevin
Elijah
Caleb
Robert
Thomas
Jordan
Cameron
Jack
Hunter
Jackson
Angel
Isaiah
Evan
Isaac
Mason
Luke
Jason
Gavin
Jayden
Aaron
Connor
Aiden
Aidan
Kyle
Juan
Charles
Luis
Adam
Lucas
Brian
Eric
Adrian
Nathaniel
Sean
Alex
Carlos
Bryan
Ian
Owen
Jesus
Landon
Julian
Chase
Cole
Diego
Jeremiah
Steven
Sebastian
Xavier
Timothy
Carter
Wyatt
Brayden
Blake
Hayden
Devin
Cody
Richard
Seth
Dominic
Jaden
Antonio
Miguel
Liam
Patrick
Carson
Jesse
Tristan
Alejandro
Henry
Victor
Trevor
Bryce
Jake
Riley
Colin
Jared
Jeremy
Mark
Caden
Garrett
Parker
Marcus
Vincent
Kaleb
Kaden
Brady
Colton
Kenneth
Joel
Oscar
Josiah
Jorge
Cooper
Ashton
Tanner
Eduardo
Paul
Edward
Ivan
Preston
Maxwell
Alan
Levi
Stephen
Grant
Nicolas
Omar
Dakota
Alexis
George
Collin
Eli
Spencer
Gage
Max
Cristian
Ricardo
Derek
Micah
Brody
Francisco
Nolan
Ayden
Dalton
Shane
Peter
Damian
Jeffrey
Brendan
Travis
Fernando
Peyton
Conner
Andres
Javier
Giovanni
Shawn
Braden
Jonah
Cesar
Bradley
Emmanuel
Manuel
Edgar
Erik
Mario
Edwin
Johnathan
Devon
Erick
Wesley
Oliver
Trenton
Hector
Malachi
Jalen
Raymond
Gregory
Abraham
Elias
Leonardo
Sergio
Donovan
Colby
Marco
Bryson
Martin
everything from the third line in the last two posts are the two separate .txt files. Even when I use codes from the book with reading from a .txt file, it doesn't seem to work. not sure why. I also am struggling with the order that everything goes in, especially with the arrays and vectors, and also adding new functions to the code. Theres some that I get, and some that I'm confused about. Our teacher goes threw it so fast its hard to keep track or get one on one time with him.
I tried the code that Rabster posted and created a .txt file on my desktop with the same name, ExampleData.txt and it shows nothing. I copied and posted the lines that your txt file said it contained into that txt file and saved it, and even tried to put the whole location in the quotation marks where it displays the file, and it still does not show anything when I run it. not sure why it will not work.
I started over, so far this is what I got but I need to figure out how to put each text file in a separate array and then match the user input or user guess to one of the names in either array or text file...

//test for 7-17
# include <iostream>
# include <fstream>
# include <string>
# include <vector>
# include <iterator>

using namespace std;

int main()
{
ifstream inputFile;
ifstream inputFileTwo;
string userInput;
string nameMatch;
vector<string> boyNames;
vector<string> girlNames;

//Open the files
inputFile.open("C:\\Users\\dtrav\\Desktop\\GirlNames.txt");
inputFileTwo.open("C:\\Users\\dtrav\\Desktop\\BoyNames.txt");

//Ask user to enter name
cout << "Please enter a name to check and see if it is one of the most popular: ";
cin >> userInput;

//Copy files to vector string variable
copy(istream_iterator<string>(inputFile),
istream_iterator<string>(),
back_inserter(boyNames));
copy(istream_iterator<string>(inputFileTwo),
istream_iterator<string>(),
back_inserter(girlNames));

//Read the names to check if there is a match
cout << girlNames.at(20) << "\n";
cout << boyNames.at(12) << "\n";

//Close the files
inputFile.close();
inputFileTwo.close();

//Display whethere there is a match in names or not
cout << "The name you picked, " << userInput << " was found on the list of most popular names!" << endl;

return 0;
}
Last edited on
now all I need it to do is match the entire name that the user enters, to either one of the boy or girl names in the lists.... The code above picks a name out with similar letters or something, I need some type of loop or something to finish this code off.... ive been at this all day reading my book and looking online.. if anyone could help it would be appreciated.
My final code. It seems to check to see if the name entered is on the girl list, then the boy list, and if its not on either list, it lets you know. Still gonna try to figure out how to tell if its on both lists... its due tomorrow so I may just turn it in how it is.

//Ch7 HW Program Challenge 17
# include <iostream>
# include <fstream>
# include <string>
# include <vector>
# include <iterator>

using namespace std;

int main()
{
ifstream inputFile;
ifstream inputFileTwo;
string userInput;
vector<string> boyNames;
vector<string> girlNames;

//Open the files
inputFile.open("C:\\Users\\dtrav\\Desktop\\GirlNames.txt");
inputFileTwo.open("C:\\Users\\dtrav\\Desktop\\BoyNames.txt");

//Ask user to enter name
cout << "Please enter a name to check and see if it is one of the most popular: ";
cin >> userInput;
cout << endl;

//Copy files to vector string variable
copy(istream_iterator<string>(inputFile),
istream_iterator<string>(),
back_inserter(girlNames));
copy(istream_iterator<string>(inputFileTwo),
istream_iterator<string>(),
back_inserter(boyNames));

//Read the names to check if there is a match, then display the results
if (find(boyNames.begin(), boyNames.end(), userInput) != boyNames.end())
cout << "The name you chose, " << userInput << ", is among the most popular boy names.\n";
else
if (find(girlNames.begin(), girlNames.end(), userInput) != girlNames.end())
cout << "The name you chose, " << userInput << ", is among the most popular girls names.\n";
else
cout << "The name you chose, " << userInput << ", is not among either list of popular names.\n";

//Close the files
inputFile.close();
inputFileTwo.close();

return 0;
}
Topic archived. No new replies allowed.