This is my assignment:
Create a program that allows a user to enter up to 10 addresses of friends. Use a two dimensional array to store the address of friends’. After each address is entered, the user should have the option to enter another address or print out a report that shows each addresses entered thus far.
I get most of it but I am having my main issue with fgets. I keep getting this error:
warning: passing arg 1 of `fgets' makes pointer from integer without a cast
The code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char name[20]= {0};
char address[40]= {100};
int choice;
int i;
printf("Welcome to the Address Book!\n\n");
for (i=0;i<10;i++) //start of the array loop. should give an exit after each entry
{
printf("Would you like to (1)Enter an address, or (2)Print the address book?\n");
scanf("%i",&choice);
switch (choice)
{
case 1:
{
printf("Please enter a name...\n");
fgets(name[i],20,stdin);
printf("You entered %s .", name);
printf("Please enter an address...\n");
fgets(address[i],40,stdin);
printf("You enteres %s .", address);
}
break;
case 2:
for (i = 0; i<10; i ++)
{
printf("%s\n", name[i]);
printf("%s\n", address[i]);
}
break;
}
}
return (0);
}
Find the corrected code in below
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char name[10][20]; //Fix3
char address[10][40];//Fix4
int choice;
int i, j;
printf("Welcome to the Address Book!\n\n");
for (i=0;i<10;i++) //start of the array loop. should give an exit after each entry
{
printf("Would you like to (1)Enter an address, or (2)Print the address book?\n");
scanf("%i",&choice);
switch (choice)
{
case 1:
{
printf("Please enter a name...\n");
getchar(); //Fix1
fgets(name[i],20,stdin);
printf("You entered %s .", name);
printf("Please enter an address...\n");
//getchar();//Fix2
fgets(address[i],40,stdin);
printf("You enteres %s .", address);
}
break;
case 2:
{
for (j = 0; j<i; j ++)
{
printf("%s\n", name[j]);
printf("%s\n", address[j]);
}
}
break;
}
}
return (0);
}
The corrections are: