Hi all,

I was making a program as one of our activities. The output should be something like this:
[IMG]http://img10.imageshack.us/i/ss1qh.jpg/[/IMG]

When I press on Display Output, it should look like this:
[IMG]http://img29.imageshack.us/i/ss2pa.jpg/[/IMG]

However, when I have to Insert a record, the added record should be added on the record when I have to display them. I have my program below, but I can't seem to get how to do that. Please bear with me.

#include<stdio.h>
#include<stdlib.h>
#define x 20
int index=1,ans,as,ctr;

struct student
{
	char studname[20];
	char course[6];
	int yr;
} studinfo [x];

void addrec()
{
	printf("\n\nHow many record(s) do you want to store?	");
	scanf("%d",&as);
	printf("\n\n");

	for(ctr=0;ctr<as;ctr++)
	{
		printf("Record Number %d\n\n",index++);
        printf("Name		: ");
		scanf("%s",&studinfo[ctr].studname);
		printf("Course		: ");
		scanf("%s",&studinfo[ctr].course);
		printf("Year		: ");
		scanf("%d",&studinfo[ctr].yr);
	}
}
void insertrec()
{
	printf("\n\nHow many record(s) do you want to Add?	");
	scanf("%d",&as);
	printf("\n\n");

	for(ctr=0;ctr<as;ctr++)
	{
		printf("Record Number %d\n\n",index++);
        printf("Name		: ");
		scanf("%s",&studinfo[ctr].studname);
		printf("Course		: ");
		scanf("%s",&studinfo[ctr].course);
		printf("Year		: ");
		scanf("%d",&studinfo[ctr].yr);
	}
}
void displayrec()
{
		system ("cls");
		printf ("\n\n\t\t Student Name \tCourse \tYear\n\n");
		 for(ctr=0;ctr<as;ctr++)
		 {
           printf("\t\t %s\t\t %s\t %d\n",studinfo[ctr].studname,studinfo[ctr].course,studinfo[ctr].yr);
		 }
		 printf ("\n\n");
}

void main()
{
start:
  printf("\n[1]-ADD NEW RECORD\n[2]-INSERT RECORD\n[3]-DISPLAY RECORDS\n[4]-EXIT\n\n");
  printf("Enter desired transaction:	");
  scanf("%d",&ans);
    	if(ans==1)
		{
		addrec();
		goto start;
		}

        else if(ans==2)
	   {
		insertrec();
		goto start;
	   }

	  else if(ans==3)
	  {
		displayrec();
		goto start;
	  }
	 else 
		 printf("\nYou have chosen to EXIT this program!\n");
}

1. insertrec() and addrec() functions are doing the same thing.
2. Whenever either of the functions are called the "as" is getting overwritten, where "as" seems like used as total no of records you want to store.

Suggestion:
1. Keep only one function to add new records.
2. Keep one more variable as a counter for how many records has been added till now.
3. Inside addrec() function use value stored in the counter to add new records in the array. (You can also check overflow if (counter+no of records)>as).
4. Use switch instead of if else hierarchy.

This article has been dead for over six months. Start a new discussion instead.