The question is:

A file contains a list of names in the form:

Jones, Frederick M
Brennan, Claire

Your program should alphabetize the list. Use the C++ string class. Also, extract names from the input stream with getline().

What i know so far:

main() should declare a char array and to open a requested file from the user

extractNames() should print out what file is being alphabetized and use cin.getline() to stream in the file into my char array, line by line.

swapNames() should use a sorting method, maybe bubble sort, to pass in the char array as an int array to make it easier, and alphabetize each line according to the algorithm suggested for alphabetizing..

Question 1.will cin.getline()only stream in 1 line, instead of cin >> file, and if so,how do you go about this with alphabetizing.. What I am thinking is: cin.getline() will stream in 1 line, maybe at a time, and then you can call the swapNames function after the first cin.getline(), and if that second line's first int is smaller than the first lines, switch the second line to the first.

Question 2. How do i go about converting the char array that has the text file in it, into integers. I've never had to do this, and I've heard it makes the process easier using int.

Here is my CODE:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

	//open file	(each name is on its own line)
	fstream in;
	char fileNames[256];
	in.open(fileNames);

	return 0;
}

void extractNames(fstream& in, char fileNames[]){
	
	cout<<"Please enter a file name to be Alphabetized: ";
	cin.getline(fileNames, 255, '\n');
	cout << "I will Alphabetize file name " << fileNames << endl;
	in.open(fileNames);
	/*
   swap(fileNames, intitialNames,swappedNames)	//puts word into the array
   */
}

void swapNames(char fileNames[], string intialNames[], string swappedNames[]){
	
	//use bubble sort to sort the strings by last name (works fine with strings)


}

Any help would be grateful! Thanks!

new code, less sloppy..

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

	//open file	(each name is on its own line)
	fstream in;
	char word[256];
	in.open("TextFile1.txt");
	cout << "I will Alphabetize file TextFile1.txt" << endl;

	return 0;
}

void extractNames(fstream& in, char word[]){
	
	while(!in.eof()){
	cin.getline(word, 255, '\n');
	
	}
	/*
   swap(fileNames, intitialNames,swappedNames)	//puts word into the array
   */
}

void swapNames(char words[], string intialNames[], string swappedNames[]){
	
	//use bubble sort to sort the strings by last name (works fine with strings)


}

Question 1.will cin.getline()only stream in 1 line, instead of cin >> file, and if so,how do you go about this with alphabetizing.. What I am thinking is: cin.getline() will stream in 1 line, maybe at a time, and then you can call the swapNames function after the first cin.getline(), and if that second line's first int is smaller than the first lines, switch the second line to the first.

How about read in each line and sort the lines using the bubble sort. You already suggested it.

Question 2. How do i go about converting the char array that has the text file in it, into integers. I've never had to do this, and I've heard it makes the process easier using int.

Good question. What integer would you convert "Jones, Frederick M" into?

And stop thinking in terms of character arrays. You already posted that you should "use the C++ string class." That solves all of your questions in one C++ type.

Also, extract names from the input stream with getline()

Be careful, std::cin.getline() is different to getline() .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.