I am new to C and I am trying to store the input of a user so that if they make the selection to display all of the contacts that are entered. I can't seem to get anything that is entered to be stored into an array. Any help would be appreciated Thanks
/*Program is an extremely simple address book
The Purpose of this program is to keep a list (an array) of 10 contact records (struct). Each record has a 10 character name field (char[]) and an age field (int).
The functionalities of the program should be made avaliable for end users through a menu that gives the following options(1. List Contacts, 2.Add a Contact, 3.Search by name, 4.Search by age, 5.Exit
When the user selects option 1, the program should list all cotacts in the array
When te user selects option 2, if the list is not full, the program should ask the user to input contact name then contact age. Otherwise, a message will be displaed informing the user that the list is full
For option 3 and 4, the program will ask the user to enter a name or age and list all contacts matching the given piece of data.
Unless the user chooses option 5, the program should keep running all the time.
*/
#include <stdio.h>
struct contact
{
char name[10];
int age;
};
struct addressbook
{
struct contact address[105];
int concount;
int i;
char select;
};
void Add_a_Contact()
{
struct addressbook A;
struct contact c;
for(A.concount = 0; A.concount <= 10; A.concount++)
{
printf("\nDo you want to add a contact (Y or N)?");
scanf(" %c", &A.select );
if(A.select == 'n')
break;
else
{
printf("Enter the Name of the Contact: ");
scanf("%s", c.name ); /*Reads the Contacts Name*/
printf("Enter the age of the Contact: ");
scanf("%d", c.age); /*Reads the Contacts Age*/
}
}
}
void List_Contacts()
{
int i;
struct addressbook A;
for(i=0; i<=A.concount; i++)
{
printf("\n%s is %d years old", A.address[i].name, A.address[i].age);
}
printf("%d",A.address[0]);
}
void Search_by_name()
{
}
void Search_by_age()
{
}
int main()
{
int select = 0;
while(select != 5)
{
printf("Select one of the following:\n");
printf("1. List Contacts\n");
printf("2. Add a contact\n");
printf("3. Search by name\n");
printf("4. Search by age\n");
printf("5. EXIT\n");
printf("Enter the number for what you wish to do: ");
scanf("%d", &select);
if(select == 1)
{
}
if(select == 2)
{
Add_a_Contact();
}
if(select == 3)
{
}
if(select == 4)
{
}
if(select == 5)
{
break;
}
}
return 0;
}
Question
mtber
I am new to C and I am trying to store the input of a user so that if they make the selection to display all of the contacts that are entered. I can't seem to get anything that is entered to be stored into an array. Any help would be appreciated Thanks
/*Program is an extremely simple address book The Purpose of this program is to keep a list (an array) of 10 contact records (struct). Each record has a 10 character name field (char[]) and an age field (int). The functionalities of the program should be made avaliable for end users through a menu that gives the following options(1. List Contacts, 2.Add a Contact, 3.Search by name, 4.Search by age, 5.Exit When the user selects option 1, the program should list all cotacts in the array When te user selects option 2, if the list is not full, the program should ask the user to input contact name then contact age. Otherwise, a message will be displaed informing the user that the list is full For option 3 and 4, the program will ask the user to enter a name or age and list all contacts matching the given piece of data. Unless the user chooses option 5, the program should keep running all the time. */ #include <stdio.h> struct contact { char name[10]; int age; }; struct addressbook { struct contact address[105]; int concount; int i; char select; }; void Add_a_Contact() { struct addressbook A; struct contact c; for(A.concount = 0; A.concount <= 10; A.concount++) { printf("\nDo you want to add a contact (Y or N)?"); scanf(" %c", &A.select ); if(A.select == 'n') break; else { printf("Enter the Name of the Contact: "); scanf("%s", c.name ); /*Reads the Contacts Name*/ printf("Enter the age of the Contact: "); scanf("%d", c.age); /*Reads the Contacts Age*/ } } } void List_Contacts() { int i; struct addressbook A; for(i=0; i<=A.concount; i++) { printf("\n%s is %d years old", A.address[i].name, A.address[i].age); } printf("%d",A.address[0]); } void Search_by_name() { } void Search_by_age() { } int main() { int select = 0; while(select != 5) { printf("Select one of the following:\n"); printf("1. List Contacts\n"); printf("2. Add a contact\n"); printf("3. Search by name\n"); printf("4. Search by age\n"); printf("5. EXIT\n"); printf("Enter the number for what you wish to do: "); scanf("%d", &select); if(select == 1) { } if(select == 2) { Add_a_Contact(); } if(select == 3) { } if(select == 4) { } if(select == 5) { break; } } return 0; }Link to comment
https://www.neowin.net/forum/topic/666554-cstoring-input-in-arrays/Share on other sites
2 answers to this question
Recommended Posts