Nov 3, 2013 at 3:56pm UTC
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 Nov 3, 2013 at 3:57pm UTC
Nov 3, 2013 at 6:07pm UTC
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 Nov 3, 2013 at 6:16pm UTC