I am working on an assignment for class:
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 have created a code that is coming up without errors, but i am not getting the desired results.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char name[10][10] = {0};
char address[10][10]= {100};
int choice;
printf("\n\n*****Welcome to the address book!*****\n\n");
printf("Please enter a name...\n");
scanf("%s",name[0]);
printf("Please enter an address...\n");
scanf("%s",address[0]);
printf("Would you like to (1)Enter another address, or (2)Print the address book?\n");
scanf("%i",&choice);
switch (choice)
{
case 1:
for (int i=0;i<10;i++) //start of the array loop. should give an exit after each entry
{
printf("Please enter a name...\n");
scanf("%s",name[i]);
printf("Please enter an address...\n");
scanf("%s",address[i]);
printf("Would you like to (1) Enter another address, or (2)Print the address book?\n");
scanf("%i",&choice);
if (choice == 1)
{
for (int i=0;i<10;i++) // a loop within a loop = loopception
{
printf("Please enter a name...\n");
scanf("%s",name[i]);
printf("Please enter an address...\n");
scanf("%s",address[i]);
printf("Would you like to (1) Enter another address, or (2)Print the address book?\n");
scanf("%i",&choice);
}
} else if (choice == 2)
{
for (int i = 0; i<10; i ++) //should print the array as is.
{
printf("%s\n", name[i]);
printf("%s\n", address[i]);
}
}
}
break;
case 2:
for (int i = 0; i<10; i ++)
{
printf("%s\n", name[i]);
printf("%s\n", address[i]);
}
break;
}
return (0);
}
My trouble is coming from the the output. I am able to fill the array but i am not able to print my desired results. Where am I losing it in the loop? Also after my first entry if i have space in the "address" input the program prints and ends.