I wrote a program to accept an array of names and ages, then sort the list in ascending order, but I can't seem to get my gets function to work. I think the issue is in like 33, but I can't be sure. When I compile, it doesn't accept the string, and immediately prints the "Age: ".
Any help would be greatly appreciated!
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
//definitions
char names[100][100], *name_ptr;
int ages[100];
int i, k, size, age;
printf(" Enter Name and Age \n");
printf("\n=============================\n");
//define array size by number of desired names
printf(" Number of names: ");
scanf("%d", &size);
//validate entry
if ( (size <= 0) || (size > 10) )
{
printf("Invalid Size\n");
printf("Number of names: ");
scanf("&d", &size);
}
printf("\n=============================\n");
for (i=0; i<size; i++)
{
//get names
printf("Name: ");
name_ptr = gets(names[i]);
printf("\n");
//get ages
printf("Age: ");
scanf("%d", &age);
//validate input
if ((age <= 0) || (age > 150))
{
printf("Invalid Input\n");
printf("Reinput Age: ");
scanf("&d", &age);
}
ages[i] = age; //assign age to the ages array
printf("\n\n");
}
printf("\n=============================\n");
//scan through ages array for every age possible (1-150)
//if the value of ages array is eaqual to the age,
//put that name and age
for (i=1; i<150; i++)
{
for (k=0; k<size; k++)
{
if (ages[k] = i)
{
puts(names[k]);
puts(ages[k]);
}
}
}
return 0;
}