Hi guys/gals,

Boy what an experience it has been searching and reading through these forums. I have learned so much!

I was given the task of pulling information from a database and entering it into a program that would sort the names.

I was able to get that step down using the bubble sort - my problem now is that I need to use arrays and modular programming to get this code to work properly.

I've linked a little bit below a posting of what I think will work and want to run it by you guys before I mess the code up ;)

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void getName(string[], int&);
void ascending(string[], int);
void output(string[], int);


int main()
{
	string name[100];
	int    item;
	//	char choice;

	// choice = getChoice();
	// if (choice == 'a')
	// ascending(name, item)
	// if (choice == 'd')
	// decending(name, item);

	getName(name, item);
	ascending(name, item);
	output(name, item);
}


void getName(string name[], int& item)
{
	ifstream fin;  
	string   temp;

	fin.open("d:\\p9.txt");

	item = 0;

	while (!fin.eof())
	{
		fin  >> name[item];
		item++;
	}
}

void ascending(string name[], int item)
{
	int    i, j;
	string temp;

	for(j=0; j<item-1; j++)
		for(i=0; i<item-1; i++)
			if(name[i] > name[i+1])
			{
				temp      = name[i];
				name[i]   = name[i+1];
				name[i+1] = temp;
			}
}

void output(string name[], int item)
{
	int i;
	for(i=0; i<item; i++)
		cout << name[i] << endl;

	cout << endl << endl;
}

I was able to get that step down using the bubble sort - my problem now is that I need to use arrays and modular programming to get this code to work properly.

Why not a std::vector and other standard functions such as std::sort ?

And you may have missed [post=155265]this[/post].

[edit]Oh, and it might help if you post a small snippet of your input file.

Unfortunately we're not quite there yet so this is what I knew how to work with. I'm going to research the link posted and get back to this thread. Hopefully I can figure it out.

oh yeah and the file is

just 6 names. Any names...

A string is an array of char (teminated with a null), so an array of strings needs to be an array of arrays of char. And this leads to all sorts of fun with choosing a maximum size (of both each string and the total number of strings possible). So you'd need to let us in on such things to help you with the C way.

The other link just says it's bad to use eof for loop control. Check for success rather than one of several possible failures.

[edit]

just 6 names. Any names...

...With embedded whitespace or no? These input files would have different results:

Dave Sinkula
Julienne Walker

Dave
Julienne

[Oops, they are strings and not char arrays.]

The code appears to work as intended and seems to meet your requirements. I'd do things differently, but if that's getting ahead of the game, I'll try to relax my comments.

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