1) Enter your name, student ID, platform and date Name: Chau Nguyen (0804707) Cl
ID: 3776605 • Letter: 1
Question
1) Enter your name, student ID, platform and date
Name: Chau Nguyen (0804707)
Class: CIS054 C/C++ Programming
Platform (Mac or PC): PC
Date:
2) PROJECT DESCRIPTION
Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration.
Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name. The file begins with:
Noah Emma
Liam Olivia
Mason Sophia
Jacob Ava
William Isabella
Ethan Mia
James Abigail
Write a program that asks the user to enter a name. The program then searches the file for the name and displays the position of the name in the file if it matches either a boy name or a girl name. The program also displays a message if the name could not be found. Sample outputs:
Enter a name to search: Daniel
Daniel is ranked 12 for boy names
Daniel was not found for girls
Enter a name to search: Cassandra
Cassandra is ranked 511 for girl names
Cassandra was not found for boys
Enter a name to search: Taylor
Taylor is ranked 76 for girl names
Taylor is ranked 462 for boy names
NOTE: You can use any method you wish to match the name input on the keyboard with the names in the file. You can compare two strings ignoring upper/lower case by using #include<cstring> in C++ or #include<string.h> in C and the following comparison if ( stricmp(string1,string2) == 0)
If your compiler does not have stricmp, you may need to use _stricmp or strcasecmp.
3) DISCUSSION
a) What did you do to develop the program? ("Followed the Directions" is not a complete description
b) What problems did you have and how did you overcome the problems?
4) PROGRAM OUTPUT
Submit seven screen shots:
1) search result for your first name
2) search result for a name used for both boys and girls, such as Taylor, or Dylan
3) search result for a name used only for boys
4) search result for a name used only for girls
5) search result for a name that at the very top of the file
6) search result for a name that is at the very bottom of the file
7) search result for a name that is not in the file
5) PROGRAM LISTING
Copy and paste YOUR code.
------------------------------------------------------------------------------------------------------
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
int rank1=0, rank2=0;
string name;
cout << "Please enter a name: ";
cin >> name;
string boy,girl;
string line;
string names[2];
ifstream myfile( "input.txt" );
if (myfile){
while (getline( myfile, line )){ // same as: while (getline( myfile, line ).good())
//cout << line << endl;
string myText(line);
istringstream iss(myText);
string token;
int i=0;
while ( getline(iss, token, ' ')){
names[i]=token;
i = i+1;
}
boy=names[0];
girl = names[1];
rank1=rank1+1;
rank2=rank2+1;
}
if(name.compare(boy)){
cout << boy <<"ranked " << rank1 << " among boys." << endl;
}else{
cout << boy <<"was not found for boys" << endl;
}
if(name.compare(girl)){
cout << girl <<"ranked " << rank1 << "among boys." << endl;
}else{
cout << girl <<"was not found for girls." << endl;
}
myfile.close();
}
return 0;
}
-----------------------------------------------
// ConsoleApplication16.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
int rank1 = 0, rank2 = 0;
string name;
cout << "Please enter a name: ";
cin >> name;
string boy, girl;
string line;
string names[2];
ifstream myfile("input.txt");
if (myfile) {
while (getline(myfile, line)) { // same as: while (getline( myfile, line ).good())
//cout << line << endl;
string myText(line);
istringstream iss(myText);
string token;
int i = 0;
while (getline(iss, token, ' ')) {
names[i] = token;
i = i + 1;
}
boy = names[0];
girl = names[1];
rank1 = rank1 + 1;
rank2 = rank2 + 1;
}
if (name.compare(boy)) {
cout << boy << "ranked " << rank1 << " among boys." << endl;
}
else {
cout << boy << "was not found for boys" << endl;
}
if (name.compare(girl)) {
cout << girl << "ranked " << rank1 << "among boys." << endl;
}
else {
cout << girl << "was not found for girls." << endl;
}
myfile.close();
}
return 0;
}
------------------------------------------------------------------------------------------
my output look like this one
Enter a name to search: Daniel
Daniel is ranked 12 for boy names
Daniel was not found for girls
Enter a name to search: Cassandra
Cassandra is ranked 511 for girl names
Cassandra was not found for boys
Enter a name to search: Taylor
Taylor is ranked 76 for girl names
Taylor is ranked 462 for boy names
Explanation / Answer
The code provided is a perfect solution for the problem described and works great. The output also depicts the exact results as mentioned.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.