I am having troubles getting my sort function to sort properly. As written below the code functions well, but organizes all sorts in descending order. If I flip the inequality to display in ascending order, the program more or less stops working. The print function will no longer work, as well as not being able to add anything back in to the phone-book(array).

A second problem I am having is with the menu function. The menu displays properly initially. Then after a selection has been made, and we return to the for loop and run menu again, a selection is made automatically and sent to the else - invalid input. The menu is then displayed again. This happens every time a selection is made.

Any help would be greatly appreciated.

Below is the code.

#include <stdio.h>
#include <string.h>

//Structure for entries.
struct entry
{
	char name[12];
	char surname[12];
	char number[12];
}entries[20];

//Menu for user entry.//
void menu()
{
	printf("Make a selection:\n\n");
	printf("-------------------------------\n");
	printf("  (a) - Add an Entry\n");
	printf("  (d) - Delete Entry\n");
	printf("  (s) - Sort Entries\n");
	printf("  (p) - Print the Phonebook\n");
	printf("  (q) - Quit\n");
	printf("-------------------------------\n");
}

//Add new entry to phonebook.
void add(int i)
{
	printf("Please enter the first name of your entry.\n");
	scanf("%s", entries[i].name);
	printf("Please enter the surname of your entry.\n");
	scanf("%s", entries[i].surname);
	printf("Please enter the phone number of your entry.\n");
	scanf("%s", entries[i].number);
}

//Delete entries from phonebook
void dlt()
{
	int entry, up;
	printf("Please enter the number of the entry you would like to delete.\n");
	scanf("%i", &entry);
	up=entry+1;
	for(entry; entry<20; ++entry, ++up){
		strcpy(entries[entry].name, entries[up].name);
		strcpy(entries[entry].surname, entries[up].surname);
		strcpy(entries[entry].number, entries[up].number);
	}
}

//Function to print the phonebook.
void print()
{
	int i;
	for ( i = 0 ; i < 20; ++i)
	{
		if (strcmp(entries[i].name, "") != 0)
			printf("[%i] First:  %s\n    Last:   %s\n    Number: %s\n\n", i, entries[i].name, entries[i].surname, entries[i].number);
		else
			break;
	}
}

//Initialize entries to be ' '
void init()
{
	int l;
	for (l=0; l<20; ++l);
		strcpy(entries[l].name, ""); 
}

//Sort entries based on given requirement.
void sort()
{
	int choice;
	int i=0, j=1;
	struct entry temp; 

	printf("Select how you would like to sort your phonebook:\n");
	printf(" (1) - sort by first name\n");
	printf(" (2) - sort by surname\n");
	printf(" (3) - sort by phone number\n");
	scanf("%i", &choice);
	print();

//Sort by first name.
	if( choice == 1 )
	{	
		for(i=0; i<19; ++i)
		{
				for(j=i+1; j<20; ++j)
				{
					if (strcmp(entries[i].name,entries[j].name) < 0 )
					{
					temp = entries[i];
					entries[i] = entries[j];
					entries[j] = temp;
					}
				}
		}
	}		
//Sort by surname.
	else if( choice == 2)
	{
		for(i=0; i<19; ++i)
		{
				for(j=i+1; j<20; ++j)
				{
					if (strcmp(entries[i].surname,entries[j].surname) < 0 )
					{
					temp = entries[i];
					entries[i] = entries[j];
					entries[j] = temp;
					}
				}
		}
	}	
//Sort by number.
	else if( choice == 3)
	{
		for(i=0; i<19; ++i)
		{
				for(j=i+1; j<20; ++j)
				{
					if (strcmp(entries[i].number,entries[j].number) < 0 )
					{
					temp = entries[i];
					entries[i] = entries[j];
					entries[j] = temp;
					}
				}
		}
	}
	print();
	return;		
	}

//Main function to call sub functions and organize program logic.
int main()
{
	char entry = 'z';
	int i;

	init();
	for (i=0; entry != 'q';)
	{
	menu();
	
	scanf("%c", &entry);
//add
	if (entry == 'a')
	{
		add(i);
		++i;
	}
//delete
	else if (entry == 'd')
	{
		dlt();
		--i;
	}
//display phonebook
	else if (entry == 'p')
		print();
//quit
	else if(entry == 'q')
		return 0;
//sort
	else if(entry == 's')
		sort();
	else
		printf("Invalid input");
	}
	return 0;
}

Just because entries has room for 20 items don't assume that all items were used. What happens to that sort algorithm is only 2 items were actually used?

You need another counter to keep track of the number of items actually used in that array. Then that sort() function will most likely work wither its sorted in ascending or descending order. Of course you will have to use that same counter in the other functions, such as print()

About first part, Ancient Dragon is right. You need to keep track of number of items in your list. And set "i = numOfItems". Otherwise either garbage is being sorted or you are accessing NULL which will halt the program.

For you second problem, you need to know that some characters may stay in your input buffer. So you need to FLUSH your buffer each time you get an input.

http://www.daniweb.com/forums/thread55844.html

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.