Sort Aphabetical (Saludo)

Hi! I'm having trouble with my program. It ddoesnt have error but It wont allow me to enter the first name. It always start asking in 2nd name. Help deeply appreciated. Here is my code :)

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <iostream.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
int i,j,n;
char name[5][20],temp[20];

cout<<"Enter Number of Elements : ";
cin>>n;

for(i=0;i<n;i++)
{
cout<<"Enter the name of person "<<(i+1)<<" :\t";
cin.getline(name[i],20);
}

cout<<"\n\nThe names in original order are:\n";

for(i=0;i<n;i++)
{
cout<<i+1<<". "<<name[i]<<" \n";

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

cout<<"\n\nThe names in alphabetical order are:\n";
for(i=0;i<n;i++)
{
cout<<i+1<<". "<<name[i]<<" \n";
}
getch();
}
Last edited on
Please use code tags.
http://www.cplusplus.com/articles/z13hAqkS/

1
2
3
	cout << "Enter Number of Elements : ";
	cin >> n;
	cin.ignore();//<<---add this 


this will ignore the newline left in the input stream after the call to cin >> n;

you should also have a using directive or namespace qualifiers, and should not be using header files with .h
Last edited on
Thank you :D It worked!
Topic archived. No new replies allowed.