• 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.
  • Posts

    • I agree. I also think Phil stayed too long. They should definitely fire whoever thought all a console platform needed was Call of Duty, Elder Scrolls, and Fallout to survive. Asha and crew are still saying they need more Elder Scrolls and Fallout games. They simply don't get it.
    • Macbook Air is an appealing option, as are plethora of Windows devices with various different CPU's
    • Mozilla highlights Firefox Nova 2026 redesign and more upcoming features with new roadmap by Sayan Sen Last month Mozilla confirmed that Firefox was set to get a major redesign this year. Dubbed "Project Nova", it can already be tested and will roll out to all users later this year.The idea is to keep the browser competitive in a rapidly evolving internet landscape. As such the revamp focuses on improving privacy, usability, performance, accessibility, and customization. Key privacy features including the built-in VPN, private browsing mode, and Enhanced Tracking Protection, will be more visible and easier to manage, while users will have the option to disable AI features entirely through a dedicated kill switch. Additionally, the redesign promises faster page loading, the return of Compact mode, expanded personalization options, and stronger accessibility support. You can find the full details in the dedicated piece linked above. In a new blog post today the company once again reiterated on Nova and also emphasized other new and upcoming features like the settings revamp that is intended to make it easier for users to understand browser settings. In order to make it simpler for users to keep up with such features Mozilla today is launching Firefox roadmap. Hence enthusiasts and interested users will be able to check out what's cooking and also share feedback about the upcoming additions. Alongside the roadmap announcement, Mozilla also highlighted what's new in Firefox 152. One of the biggest additions is the arrival of Tab Groups on Android. The feature, which has already been helping desktop users organize large numbers of tabs, is now beginning to roll out on mobile. Users will be able to group related tabs together, assign names and colors to them, and return to them later. Mozilla says support for iOS will arrive later this year. Firefox 152 also introduces the aforementioned redesigned Settings experience. The company says the changes are meant to make controls easier to find and help users discover features they may not have previously known about. Existing preferences are not changing, though they are now better organized. Another notable addition is the new Blocked Tracker Widget, which provides a visual overview of Firefox's privacy protections by showing how many trackers have been blocked over time and the types of tracking activity the browser has stopped. Looking ahead, Mozilla revealed several upcoming roadmap features. They include customizable keyboard shortcuts, as well as enhanced PDF editing tools that will allow documents to be split, merged, and reorganized directly within Firefox. The company is also working on bringing Multi-Account Containers into the native Firefox experience thus removing the need for a separate extension. Meanwhile Firefox's built-in VPN is set to expand to mobile devices. Mozilla is also developing AI-powered features like Quick Answers, which can provide concise responses to voice queries, and Smart Window, its optional AI browsing experience that is now available without a waitlist. Finally, a new Power Saving Mode is in the works and will help reduce the impact of resource-heavy tabs on mobile devices in order to extend battery life. The video below summarizes the upcoming changes in an easy to understand format: You can find the announcement blog post here on Mozilla's official website.
    • Dead on arrival at that price. Like they missed the mark by multiple hundreds of dollars - this should actually undercut the Macbook Air at $899 if they want any sort of sales / further adoption of WoA
  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      513
    2. 2
      +Edouard
      196
    3. 3
      PsYcHoKiLLa
      109
    4. 4
      Steven P.
      89
    5. 5
      Nick H.
      71
  • Tell a friend

    Love Neowin? Tell a friend!