Hello everyone.

I am working on my lab assignment and am somewhat stuck on how to proceed. We are studying using the book Starting out with C++: From Control Structures through Objects by Gaddis.

The lab assignment is as stated:

NamesOf1980s.txt is a text file from the SSA detailing the baby names. After the first three lines (which provide unimportant header information), each line details a rank and a pair of names with frequency: One for the boy name, one for the girl name. For example, the first two lines of data (the 4th–5th lines of the file) read as so (tabs separate the entries):

1 Michael 33316 Amanda 23744
2 Christopher 27697 Jennifer 22274

This indicates that the most popular boy name was Michael with 33,316 boys named that decade while the number 2 boyʼs name was Christopher with 27,697. Similarly, the most girls name was Amanda with 23,744 names with Jennifer following with 22,274.

For your assignment, you will create a program that will read all the names, ask a user for the beginning of a name (such as “R”, “Ro”, and “Rob”) and a gender, then print out all the names that start with that prefix and match the gender from most to least frequent (the order the file uses). You must validate all input (so no numbers or punctuation in a name, names must start with an uppercase letter, choose only valid gender). You may use either C-strings or the C++ string class.

The text file has over 1000 rows in it, and as you can see, about 5 columns (see attached file). I am certain that I will need to use a 2D array, and am thinking the easiest thing to use would be the C++ string class.

Here is what I have so far:

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

// Main function
int main()
{

// Read from the file
ifstream inFile;    // Create an input file stream.
 inFile.open("NamesOf1980s.txt");  // Use it to read from a file
 in >> rank;    // Read the first item from the file into an integer variable x.
 in >> nameBoy;
 in >> numberBoy;  
 in >> nameGirl;  
 in >> numberGirl;  
 


const int SIZE=30;
char name[SIZE];
char gender[SIZE];
char array[1004][5]; // Text file has 1004 rows, 5 columns


inFile.getline(name, SIZE);

The example output screenshot shows the following:

Please enter the start of a name or full name: Ron
Please enter a (b)oy or a (g)irl: b
Ronald (2332)
Ronnie (538)
Ron (116)
Ronny (47)

I am unsure of how to proceed with the setup of this assignment. We have also only recently touched on the C++ string class, so I am not strong in this area.

Thanks for the help!

Attachments
Most Popular Names of the 1980's				
	Male		Female	
Rank	Name	Number	Name	Number
1	Michael	33316	Jessica	23744
2	Christopher	27697	Jennifer	22274
3	Matthew	22914	Amanda	18260
4	Joshua	19983	Ashley	17489
5	David	19556	Sarah	13676
6	Daniel	17520	Stephanie	10992
7	James	17505	Melissa	10952
8	Robert	16232	Nicole	10573
9	John	15928	Elizabeth	9987
10	Joseph	15058	Heather	9716
11	Jason	14679	Tiffany	8021
12	Justin	14513	Michelle	7835
13	Andrew	14159	Amber	7730
14	Ryan	13872	Megan	7626
15	William	12371	Rachel	7403
16	Brian	11792	Amy	7376
17	Jonathan	11731	Lauren	7362
18	Brandon	11462	Kimberly	7180
19	Nicholas	11079	Christina	7173
20	Anthony	10665	Brittany	7147
21	Eric	10303	Crystal	6979
22	Adam	9673	Rebecca	6800
23	Kevin	9510	Laura	6761
24	Steven	9086	Emily	6544
25	Thomas	8866	Danielle	6523
26	Timothy	8577	Samantha	6185
27	Richard	7775	Angela	5754
28	Jeremy	7675	Erin	5744
29	Kyle	7234	Kelly	5596
30	Jeffrey	7133	Sara	5190
31	Benjamin	7114	Lisa	5065
32	Aaron	7096	Katherine	4991
33	Mark	6550	Andrea	4831
34	Charles	6541	Mary	4826
35	Jacob	6243	Jamie	4816
36	Stephen	5812	Erica	4723
37	Jose	5654	Courtney	4204
38	Patrick	5512	Kristen	4112
39	Scott	5321	Shannon	3919
40	Paul	5262	April	3867
41	Nathan	5235	Maria	3617
42	Sean	5156	Kristin	3568
43	Zachary	5075	Katie	3552
44	Travis	5036	Lindsey	3532
45	Dustin	4860	Alicia	3406
46	Gregory	4675	Vanessa	3357
47	Kenneth	4452	Lindsay	3306
48	Alexander	4304	Christine	3282
49	Jesse	4294	Allison	3195
50	Tyler	4245	Kathryn	3110
51	Bryan	4126	Julie	3057
52	Samuel	3702	Tara	2860
53	Derek	3569	Anna	2781
54	Bradley	3435	Natalie	2770
55	Chad	3419	Kayla	2750
56	Shawn	3267	Victoria	2641
57	Edward	3120	Jacqueline	2640
58	Jared	3108	Monica	2624
59	Juan	3022	Holly	2511
60	Luis	2952	Cassandra	2415
61	Cody	2929	Patricia	2328
62	Jordan	2894	Kristina	2298
63	Peter	2889	Catherine	2185
64	Carlos	2626	Cynthia	2176
65	Corey	2573	Brandy	2164
66	Keith	2545	Whitney	2143
67	Donald	2495	Chelsea	2131
68	Marcus	2490	Veronica	2116
69	Joel	2350	Brandi	2110
70	Ronald	2332	Leslie	1998
71	Phillip	2303	Kathleen	1962
72	George	2285	Stacy	1927
73	Cory	2184	Diana	1916
74	Antonio	2163	Erika	1888
75	Shane	2138	Natasha	1887
76	Douglas	2113	Meghan	1881
77	Raymond	2070	Dana	1864
78	Brett	2036	Carrie	1841
79	Alex	1999	Krystal	1836
80	Gary	1986	Karen	1822
81	Nathaniel	1923	Jenna	1816
82	Craig	1919	Leah	1803
83	Derrick	1912	Melanie	1776
84	Casey	1836	Valerie	1759
85	Ian	1826	Alexandra	1742
86	Philip	1813	Brooke	1689
87	Gabriel	1760	Jasmine	1677
88	Victor	1753	Julia	1667
89	Erik	1737	Alyssa	1648
90	Christian	1735	Caitlin	1631
91	Frank	1647	Hannah	1609
92	Evan	1635	Brittney	1579
93	Jesus	1601	Stacey	1578
94	Larry	1593	Sandra	1556
95	Seth	1577	Margaret	1531
96	Austin	1550	Susan	1531
97	Dennis	1548	Candice	1509
98	Vincent	1544	Bethany	1473
99	Brent	1532	Casey	1465
100	Jeffery	1514	Katrina	1456
101	Wesley	1508	Latoya	1446
102	Randy	1505	Tracy	1444
103	Todd	1499	Misty	1412
104	Curtis	1488	Kelsey	1402
105	Miguel	1472	Kara	1389
106	Jeremiah	1470	Nichole	1358
107	Adrian	1414	Alison	1351
108	Jorge	1402	Molly	1328
109	Alan	1396	Tina	1317
110	Angel	1366	Denise	1312
111	Mario	1357	Heidi	1302
112	Luke	1351	Alexis	1300
113	Russell	1348	Jillian	1296
114	Jerry	1329	Brenda	1270
115	Trevor	1318	Candace	1256
116	Carl	1295	Nancy	1253
117	Ricardo	1280	Rachael	1246
118	Johnny	1243	Pamela	1244
119	Blake	1242	Morgan	1233
120	Lucas	1237	Renee	1227
121	Shaun	1229	Gina	1217
122	Mitchell	1224	Jill	1207
123	Tony	1224	Kendra	1199
124	Cameron	1212	Teresa	1192
125	Terry	1209	Sabrina	1179
126	Francisco	1178	Miranda	1169
127	Martin	1173	Krista	1157
128	Troy	1160	Felicia	1153
129	Allen	1138	Kristy	1145
130	Devin	1135	Anne	1139
131	Johnathan	1121	Robin	1134
132	Manuel	1110	Monique	1129
133	Henry	1093	Linda	1122
134	Bobby	1081	Desiree	1119
135	Andre	1080	Theresa	1119
136	Kristopher	1065	Wendy	1115
137	Ricky	1060	Joanna	1111
138	Jimmy	1059	Tanya	1099
139	Marc	1056	Melinda	1097
140	Billy	1047	Lori	1092
141	Garrett	1028	Jaclyn	1088
142	Hector	1025	Tamara	1080
143	Caleb	1002	Alisha	1074
144	Danny	998	Kelli	1069
145	Roberto	996	Dawn	1063
146	Lance	994	Colleen	1059
147	Albert	985	Marissa	1059
148	Randall	974	Lacey	1039
149	Lee	971	Christy	1030
150	Lawrence	967	Abigail	1028
151	Jonathon	960	Angelica	1000
152	Taylor	957	Jenny	982
153	Mathew	930	Barbara	954
154	Isaac	923	Tabitha	950
155	Micheal	917	Caroline	940
156	Clinton	909	Kari	937
157	Jamie	907	Meredith	937
158	Walter	894	Kristi	918
159	Javier	881	Ebony	915
160	Rodney	877	Deanna	910
161	Louis	871	Rebekah	899
162	Edwin	870	Carolyn	894
163	Roger	863	Marie	877
164	Willie	855	Ana	870
165	Colin	849	Michele	864
166	Jon	845	Tonya	858
167	Alejandro	841	Angel	851
168	Clayton	841	Sharon	847
169	Oscar	836	Bridget	846
170	Omar	833	Tasha	844
171	Chase	829	Sheena	835
172	Rafael	829	Cristina	832
173	Pedro	822	Brianna	829
174	Ross	815	Priscilla	819
175	Gerald	805	Deborah	813
176	Jack	789	Cassie	812
177	Bruce	782	Ashlee	811
178	Arthur	780	Carmen	809
179	Joe	778	Meagan	805
180	Roy	778	Ann	799
181	Spencer	772	Cindy	794
182	Ruben	768	Stefanie	785
183	Darren	766	Tammy	773
184	Jay	760	Dominique	771
185	Maurice	752	Carla	767
186	Wayne	745	Virginia	766
187	Calvin	742	Regina	761
188	Drew	736	Jaime	758
189	Grant	721	Adrienne	745
190	Brendan	718	Mallory	741
191	Fernando	718	Audrey	729
192	Brad	713	Beth	727
193	Dylan	695	Olivia	725
194	Eduardo	689	Katelyn	721
195	Jaime	680	Latasha	718
196	Raul	666	Cheryl	710
197	Darrell	663	Taylor	707
198	Julian	650	Jordan	706
199	Sergio	641	Janet	704
200	Logan	640	Kristine	686
201	Frederick	632	Jacquelyn	685
202	Levi	632	Mandy	684
203	Emmanuel	628	Aimee	678
204	Jermaine	626	Claudia	675
205	Noah	617	Autumn	661
206	Terrance	615	Trisha	659
207	Edgar	612	Bianca	657
208	Ivan	612	Cara	657
209	Dominic	610	Martha	656
210	Geoffrey	608	Rosa	656
211	Jerome	607	Suzanne	642
212	Reginald	603	Haley	637
213	Alberto	602	Nina	633
214	Eddie	602	Carly	631
215	Theodore	601	Mindy	630
216	Tyrone	598	Kate	629
217	Neil	597	Donna	628
218	Marvin	587	Jodi	616
219	Armando	584	Karla	615
220	Julio	583	Shawna	615
221	Ramon	582	Yolanda	614
222	Ernest	573	Bonnie	613
223	Steve	566	Summer	612
224	Micah	555	Evelyn	597
225	Jake	548	Adriana	595
226	Eugene	547	Grace	593
227	Jessie	547	Kaitlin	591
228	Ronnie	538	Ruth	588
229	Darryl	536	Toni	588
230	Glenn	536	Mayra	580
231	Andres	535	Alexandria	570
232	Karl	519	Lydia	568
233	Leonard	517	Abby	566
234	Ethan	514	Kaitlyn	566
235	Nicolas	514	Janelle	564
236	Dale	512	Robyn	564
237	Kurt	511	Briana	562
238	Tommy	511	Gabrielle	560
239	Terrence	506	Joy	556
240	Devon	505	Sheila	553
241	Melvin	501	Paula	550
242	Bryce	498	Diane	545
243	Barry	495	Kellie	541
244	Cesar	493	Gloria	535
245	Marco	493	Melody	530
246	Bryant	492	Naomi	527
247	Preston	492	Jessie	526
248	Stanley	487	Sophia	522
249	Clifford	485	Daisy	520
250	Kelly	483	Raquel	519
251	Harold	479	Christie	518
252	Jarrod	479	Rose	518
253	Erick	477	Krystle	514
254	Orlando	467	Amelia	513
255	Alexis	458	Paige	513
256	Nelson	455	Shanna	513
257	Byron	452	Sonia	512
258	Shannon	450	Randi	509
259	Dwayne	448	Johanna	507
260	Francis	445	Kasey	506
261	Max	445	Frances	502
262	Andy	439	Claire	501
263	Xavier	436	Nikki	501
264	Alfredo	435	Ellen	500
265	Josue	435	Hillary	498
266	Enrique	432	Sasha	498
267	Gerardo	425	Yesenia	497
268	Joey	424	Jeanette	494
269	Tyson	420	Ashleigh	492
270	Cole	419	Emma	491
271	Dean	419	Marisa	483
272	Ralph	419	Traci	473
273	Clint	417	Charlene	470
274	Abraham	412	Shana	470
275	Tristan	408	Shelly	470
276	J	407	Roxanne	469
277	Ray	407	Jocelyn	468
278	Clarence	405	Sylvia	468
279	Franklin	405	Kelley	463
280	Gilbert	403	Carol	460
281	Arturo	400	Justine	460
282	Marshall	399	Britney	459
283	Warren	394	Rachelle	457
284	Damien	392	Charity	455
285	Kelvin	392	Keri	455
286	Cedric	391	Kirsten	453
287	Alvin	388	Yvonne	453
288	Earl	384	Debra	452
289	Branden	382	Christa	449
290	Harry	379	Shauna	447
291	Colby	377	Sonya	446
292	Elijah	376	Chelsey	440
293	Marcos	374	Esther	434
294	Brady	373	Charlotte	433
295	Terrell	373	Anita	431
296	Alfred	370	Savannah	430
297	Rene	369	Sierra	428
298	Beau	367	Angelina	424
299	Jamal	365	Brianne	424
300	Salvador	364	Sherry	423
301	Israel	361	Gabriela	420
302	Demetrius	360	Leigh	419
303	Kirk	357	Rhonda	418
304	Nickolas	356	Stacie	418
305	Wade	355	Kerry	417
306	Darius	354	Karina	416
307	Ernesto	353	Annie	415
308	Isaiah	350	Kristie	415
309	Leon	348	Kerri	414
310	Felix	346	Carissa	412
311	Lorenzo	344	Eva	412
312	Morgan	339	Shelby	412
313	Courtney	338	Lacy	411
314	Dane	337	Alissa	406
315	Antoine	335	Tia	406
316	Daryl	334	Laurie	403
317	Angelo	331	Tracey	403
318	Emanuel	329	Janice	401
319	Clifton	328	Alice	400
320	Stuart	327	Elise	396
321	Darnell	326	Miriam	395
322	Kenny	325	Elisabeth	392
323	Howard	324	Yvette	386
324	Bernard	322	Helen	385
325	Brock	320	Terri	385
326	Johnathon	317	Latisha	383
327	Roderick	317	Tricia	382
328	Allan	313	Maggie	380
329	Lamar	311	Breanna	376
330	Noel	311	Camille	375
331	Damian	307	Leticia	373
332	Pablo	307	Annette	372
333	Chris	306	Tamika	371
334	Trent	304	Jane	369
335	Quentin	303	Maureen	369
336	Dallas	297	Sandy	368
337	Gavin	295	Hilary	367
338	Collin	294	Katharine	361
339	Miles	294	Tabatha	360
340	Trenton	294	Keisha	356
341	Mason	293	Rochelle	355
342	Hunter	292	Aubrey	354
343	Bret	291	Allyson	353
344	Eli	291	Elisa	351
345	Leroy	291	Lesley	350
346	Norman	291	Antoinette	348
347	Rodolfo	290	Cecilia	348
348	Zachery	290	Lynn	348
349	Damon	286	Elaine	347
350	Lewis	281	Irene	347
351	Charlie	278	Staci	347
352	Tanner	277	Jana	346
353	Terence	277	Chasity	345
354	Jayson	274	Dorothy	343
355	Landon	274	Faith	342
356	Vernon	274	Hope	342
357	Desmond	273	Lorena	342
358	Heath	272	Alana	338
359	Gustavo	270	Marilyn	338
360	Don	269	Elena	334
361	Glen	268	Jade	334
362	Lonnie	266	Bobbie	333
363	Kent	264	Brandie	333
364	Neal	264	Kathy	333
365	Ashley	261	Maribel	332
366	Maxwell	261	Cortney	331
367	Ismael	257	Clarissa	330
368	Gordon	256	Rita	330
369	Dominique	255	Shelley	329
370	Graham	255	Juanita	328
371	Guille

Before you start storing and analysing, make sure you can actually read the file successfully.

Say by being able to print out the following as each line is read.
Rank = 1, bName=Michael, count=33316, gName=Amanda, count=23744

And for each invalid line (you might have to add some yourself to test with), print out a suitable message.

When that's done, then you can think about storing the data, then implementing the search/lookup functions.

Thanks, Salem. I still am having a hard time beating out how that would translate into code. Would I be using something like

inFile.substr(

to return a substring of the array? Thanks for any help.

Files don't have substr methods.

Have you done the first step - reading in the file?

Post some code.

Okay, I am able to read into the file and pull out the contents of the entire file by using this code:

// Include Libraries
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;


int main () 
  {
  // This will contain the data read from the file
  string line; 
  // Opening the file
  ifstream myfile ("NamesOf1980s.txt"); 
  
   //if the file is open
  if (myfile.is_open()) 
  
    {
      //while the end of file is NOT reached
      while (! myfile.eof() ) 
      {
        // Get one line from the file
        getline (myfile,line); 
        //and output it
        cout << line << endl; 
      }
    //closing the file
    myfile.close(); 
    
    }
  //if the file is not open output this:
  else cout << "Unable to open file"; 
  
  
  return 0;
  
  }

How would I proceed? Thanks so much for the help - I really appreciate it!

First, a small mistake

while (! myfile.eof() ) 
      {
        // Get one line from the file
        getline (myfile,line);

Should just be

// Get each line from the file
      while ( getline (myfile,line) ) 
      {

Next thing you need is to store the lines.
I would suggest std::vector< std::string > lines; http://www.cppreference.com/wiki/stl/vector/start
Look up the push_back() initially.

When you've read the file, practice printing out the lines by accessing the vector.

This article has been dead for over six months. Start a new discussion instead.