Files/Functions-HW help

Problem: Programming Challenge #11 pg 489, Chapter 8, Using Files - String Selection Sort Modification in the Gaddis C++ Book.
You must first complete Programming Challenge #6 in Chapter 8.

Basically, I need to convert the list of names to a file. I've completed the program challenge #6 that is referenced and will include the code below, but I'm unsure where to go from here. I need to somehow make a file, which I'm not able to do and I need help making a function to count how many last names begin with each letter (continued directions below)Thanks so much for your help. I'm questioning if I'm cut out for programming at this point, I'm just not catching on to it like I had hoped..

Additional Requirements NOT in the book: Create a function to count how many last names begin with each letter and output the names and the total number of names each time (see Sample Output below). This function should be passed the array by value and the size of the array by value. The function should output the names that begin with a particular letter then output the total number of names. Do NOT output results for letters that do not have any names, i.e. for example ther are no names that begin with B, so no output should be generated for this letter.

Hint: Once you complete Programming Challenge #6, #11 is a slight modification. Chapter 5 covers reading data from a file (starting on pg. 275).
You can use Program 8-5 on page 471 to begin your project; you can easily modify this program to do a selection sort on strings instead of integers.

Constraints:

NO GLOBAL VARIABLES ALLOWED.
Read Names from an input file called names.txt.
Names data:
Collins, Bill
Smith, Bart
Allen, Jim
Griffin, Jim
Stamey, Marty
Rose, Geri
Taylor, Terri
Johnson, Jill Allison, Jeff
Looney, Joe
Wolfe, Bill
James, Jean
Weaver, Jim
Pore, Bob
Rutherford, Greg
Javens, Renee
Harrison, Rose
Setzer, Cathy
Pike, Gordon
Holland, Beth
Write the result of your selection sort to the screen.
Use functions to read in the names from the file, to display the names in the array and to sort the names in the array. In addtion create a function to count how many last names begin with each letter and output the results clearly!!!
WRITE THE PROGRAM: Write the program for the Problem above.

Hint: When reading in the names from the file you will want to use the getline() function. If you use the >> operator to read in the data from the file you will not be reading in the whole name (try it and see what happens).

Make sure you complete Programming Challenge #6 FIRST and get that program working completely before attempting to complete Programming Challenge #11.

Pseudo Code for main function:

Declare and Initialize Variables.
Call the function to Read in the names from the input file names.txt
cout << "Here are the unsorted names:\n";
cout << "--------------------------\n";
Call the function to display the array.
Call the function to sort the array.
cout<<"Here are the names sorted:\n";
cout << "--------------------------\n";
Call the function to display the array.
Call the function to count how many names begin with each letter.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include<iomanip>
using namespace std;

void selectionSort (string[],int);

int main ()
{
	const int NUM_NAMES=20;
	string names[NUM_NAMES]=
	{"Collins, Bill", "Smith, Bart", "Michalski, Jacob",
	"Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
	"Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
	"Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
	"Moretti, Bella", "Wu, Hong", "Patel, Renee",
	"Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
	"Kelly, Sean", "Holland, Beth" };

	cout<<"Here are the unsorted names:\n "<<endl;
	cout<<"___________________________\n"<<endl;

	for(int index=0;index<NUM_NAMES;index++)
		cout<<setw(15)<<names[index]<<endl;

	selectionSort(names,NUM_NAMES);

	cout<<"\nHere are the names sorted: \n "<<endl;
	cout<<"_______________________________\n"<<endl;

	for(int index=0;index<NUM_NAMES;index++)
		cout<<setw(15)<<names[index]<<endl;

	system ("pause");

	return 0;
}

void selectionSort (string names[], int size)
{
	int startScan;
	int minIndex;
	string minValue;

	for (startScan=0; startScan<(size-1);startScan++)
	{
		minValue=startScan;
		minValue=names[startScan];
		for (int index=startScan+1;index<size;index++)
		{
			if (names[index]<minValue)
			{
				minValue=names[index];
				minIndex=index;
			}
		}
		names[minIndex]=names[startScan];
		names[startScan]=minValue;
	}
	}
Topic archived. No new replies allowed.