• 0

[C]Storing Input in Arrays


Question

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

  • 0

I don't see any statement that stores anything in an array. You have the Add_a_Contact() function that creates an instance of your contact struct and puts information into it, but nothing is done with that instance of the contact.

Also you need to instantiate your addressbook in the main function, so it is available for all the methods.

When something is instantiated in a function, it is destroyed when that function is done. You need to instantiate objects, structs, variables, etc. in the outermost "layer" of your code so that they are available to everything they need to be available for. Of course if you only need a variable or object inside of a certain function then you don't need to worry, but for a struct that you are continuously adding data to during the runtime of your program, you need to instantiate that outside of everything it is needed in.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.