VB icon

Sort a List of 20 Names

Email
Submitted on: 1/1/2015 4:16:00 PM
By: Dfuse (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 1527
 
     The user enters 20 names (or whatever) into an array and the program outputs the 20 inputs alphabetically. The program also stops and sorts what the user has entered so far if the user does not enter anything.

 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Sort a List of 20 Names
//**************************************
#include <iostream.h>
#include <string.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Sort a List of 20 Names
// Description:The user enters 20 names (or whatever) into an array and the program outputs the 20 inputs alphabetically. The program also stops and sorts what the user has entered so far if the user does not enter anything.
// By: Dfuse (from psc cd)
//
// Assumes:My code only include <iostream.h> and <string.h>. I also did not use the qsort since it doesn't help explain the principal of sorting.
//**************************************

/* By Dfuse
http://dfuse.netfirms.com
molsoncanadians@hotmail.com
*/
#include<iostream.h>
#include <string.h>
void getName(char* name)
{ cout<<"Enter a Name: ";
 cin.getline(name, 20, '\n');
 if(strlen(name)==19)
cin.ignore(100,'\n');
} //getName
readAllNames(char list [] [20])
{ 	int i=0;
	getName(list[i]);
	while((i<19)&&(strlen(list[i])>0))
	{ i++;
	 getName(list[i]);
	} //while 
return i+1;
}//readAllNames
	
void printAll(char list[][20],int c)
{ for (int i=0;i<c;i++)
cout<<list[i]<<"\n";
} //printAll
void swap(char *s1, char *s2)
{char* temp;
 strcpy(temp,s1);
 strcpy(s1,s2);
 strcpy(s2,temp);
} //swap
sort(char list[][20], int n)
{ int i, smallPos, pass;
char temp;
for (pass=0; pass<n; pass++)
{ smallPos = pass; 
		for (i=pass; i<n; i++)
			{ if (stricmp (list[i], list[smallPos])< 0)
			 smallPos = i; 
			} //for
swap(list[smallPos], list[pass]);
} //for
return;
}
main()
{ char names[20][20];
 int count=0;
 count=readAllNames(names);
//cout<<"Count= "<<count<<"\n";	
 sort(names,count);
 printAll(names,count);
 return 0;
} //main


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.