For some reason this isn't working and I don't know why. It seems to skip over 'c = getchar();' entirely.
--------------------------
Code:
while (flag_done==0 && max<30)
{
printf("Enter the person's first name: ");
scanf("%s", &person[max].firstname);
printf("Enter the person's last name: ");
scanf("%s", &person[max].lastname);
printf("Enter the person's age: ");
scanf("%d", &person[max].age);
printf("\n");
max++;
printf("Would you like to add another record? (Y/N)");
c = getchar();
printf("\n");
if(c=='n') {flag_done = 1;}
}
--------------------------
Here is the output...
--------------------------
Enter the person's first name: FirstName
Enter the person's last name: LastName
Enter the person's age: 20
Would you like to add another record? (Y/N)
Enter the person's first name:
--------------------------
And the entire program...
--------------------------
Code:
#include<stdio.h>
struct rec
{
char firstname[15], lastname[15];
int age;
} person[30], t;
void bubble_sort(int max)
{
int x,y;
for (x=0; x < max; x++)
{
for (y=0; y < max-x-1; y++)
if (person[y].lastname > person[y+1].lastname)
{
t=person[y];
person[y]=person[y+1];
person[y+1]=t;
}
}
}
int main()
{
int x, flag_done, max=0;
char c;
while (flag_done==0 && max<30)
{
printf("Enter the person's first name: ");
scanf("%s", &person[max].firstname);
printf("Enter the person's last name: ");
scanf("%s", &person[max].lastname);
printf("Enter the person's age: ");
scanf("%d", &person[max].age);
printf("\n");
max++;
printf("Would you like to add another record? (Y/N)");
c = getchar();
printf("\n");
if(c=='n') {flag_done = 1;}
}
printf("NAME\t\t\tAGE\n");
bubble_sort(max);
for (x=0; x<=max; x++)
{
printf("\n%s, %s\t\t%d", person[x].lastname, person[x].firstname, person[x].age);
}
return 0;
}
--------------------------