Will someone please help me with this C++ problem. I attempted it a few times bu
ID: 3685031 • Letter: W
Question
Will someone please help me with this C++ problem. I attempted it a few times but it doesn't seem to be working out. Thanks.
NOTE: This is what I have so far. I made 5 the most popular times a name is repeated. So if a name the user selected is repeated 5 times or more in the array, then that must be a popular name. However, it appears that both boy and girl names arrays don't have a name that is repeated more than once, so there never can be a popular name. Am I right, or am I just doing this wrong? Also, I tried to get the most repeated element in the array to compare against the user's choice instead of an arbitrary number 5, but it was not working out because I can't get each name in the array to compare to ALL the other names in the array, not just those moving forward. I used a nested loop for this. Here is what I have so far:
/*Benjamin Dreher
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
// Name Search
//Global Constants
//Prototypes
const int NAMES = 200; //holds number of names
int main() {
//Declare Variables
string boy_names[NAMES]; //boy array
string girl_names[NAMES];//girl array
ifstream inFile1;
ifstream inFile2;//import two files
int count1 = 0;
int count2 = 0;
//Read data into boy and girl arrays
inFile1.open("C:\Users\spike\Desktop\BoyNames.txt");
inFile2.open("C:\Users\spike\Desktop\GirlNames.txt");
if (inFile1 && inFile2)
{
while (count1 < NAMES && inFile1 >> boy_names[count1])
{
count1++;
}
while (count2 < NAMES && inFile2 >> girl_names[count2])
{
count2++;
}
inFile1.close();
inFile2.close();
string user_most_boy_name;
string user_most_girl_name;
int user_total_of_most_boy_names = 0;
int user_total_of_most_girl_names = 0;
int most_popular_names = 5;
cout << "Please enter a boy's name, girl's name, or both: ";
getline(cin, user_most_boy_name);
getline(cin, user_most_girl_name);
cout << endl;
int most_boy_names = 0;
for (int i = 0; i < NAMES; i++)
{
if (user_most_boy_name == boy_names[i])
{
most_boy_names++;
}
}
cout << most_boy_names;
if (most_boy_names >= most_popular_names)
{
cout << user_most_boy_name << " is a popular name." << endl;
}
else
{
cout << user_most_boy_name << " is not a popular name." << endl;
}
int most_girl_names = 0;
for (int i = 0; i < NAMES; i++)
{
if (user_most_girl_name == girl_names[i])
{
most_girl_names++;
}
}
cout << most_girl_names;
if (most_girl_names >= most_popular_names)
{
cout << user_most_girl_name << " is a popular name." << endl;
}
else
{
cout << user_most_girl_name << " is not a popular name." << endl;
}
}
else
{
cout << "File was not found. " << endl;
}
return 0;
}
BoyNames.txt:
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
GirlNames.txt:
Explanation / Answer
//you should use ".compare()" method to compare strings not "=="
//look at the following modified code
//and also check the code that i have added to find most occurred boy name.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include<stdlib.h>
using namespace std;
// Name Search
//Global Constants
//Prototypes
const int NAMES = 200; //holds number of names
int main() {
//Declare Variables
string boy_names[NAMES]; //boy array
string girl_names[NAMES];//girl array
ifstream inFile1;
string popular_name="";
ifstream inFile2;//import two files
int count1 = 0;
int count2 = 0;
//Read data into boy and girl arrays
inFile1.open("BoyNames.txt");
inFile2.open("GirlNames.txt");
if (inFile1 && inFile2)
{
while (count1 < NAMES && inFile1 >> boy_names[count1])
{
count1++;
}
while (count2 < NAMES && inFile2 >> girl_names[count2])
{
count2++;
}
inFile1.close();
inFile2.close();
string user_most_boy_name;
string user_most_girl_name;
int user_total_of_most_boy_names = 0;
int user_total_of_most_girl_names = 0;
int most_popular_names = 5;
cout << "Please enter a boy's name, girl's name, or both: ";
getline(cin, user_most_boy_name);
getline(cin, user_most_girl_name);
cout << endl;
int most_boy_names = 0;
for (int i = 0; i < NAMES; i++)
{
if (user_most_boy_name.compare(boy_names[i]))
{
most_boy_names++;
}
}
cout << most_boy_names;
if (most_boy_names >= most_popular_names)
{
cout << user_most_boy_name << " is a popular name." << endl;
}
else
{
cout << user_most_boy_name << " is not a popular name." << endl;
}
int most_girl_names = 0;
for (int i = 0; i < NAMES; i++)
{
if (user_most_girl_name.compare(girl_names[i]))
{
most_girl_names++;
}
}
cout << most_girl_names;
if (most_girl_names >= most_popular_names)
{
cout << user_most_girl_name << " is a popular name." << endl;
}
else
{
cout << user_most_girl_name << " is not a popular name." << endl;
}
int ct=0,prev=0;
for (int i = 0; i < NAMES; i++)
{
for (int j = 0; j < NAMES; j++)
{
if(boy_names[i].compare(boy_names[j]))
ct++;
}
if(ct>prev)
{
popular_name=boy_names[i];
prev=ct;
}
ct=0;
}
cout << "popular boy" <<popular_name<< endl;
}
else
{
cout << "File was not found. " << endl;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.