Home > OS >  My Array item keeps getting overwritten in C programming
My Array item keeps getting overwritten in C programming

Time:10-23

I'm trying to create a program that allows users to add information into a nameCard array using a function called AddNameCard. but when I try to add another set of input in, the previous items seems to get overwritten. and the listnamecard function only displays the last inputted items. Anyone know what i need to do to get around this problem? I'm learning C programming currently, go easy on me please :).

#include <stdio.h>
#include <stdlib.h>
# define Max_Size 5

typedef struct
{
  int nameCardID;
  char personName[20];
  char companyName[20];

} NameCard;

NameCard nameCard[Max_Size];

void AddNameCard() {
  int j;
  printf("\n");
  for (int i = j - 1; i < j; i  ){

      printf("Enter Name Card ID: \n");
      scanf("%d", &nameCard[i].nameCardID);

      printf("Enter Person Name: \n");
      scanf("%s", &nameCard[i].personName);

      printf("Enter Company Name : \n");
      scanf("%s", &nameCard[i].companyName);
  }
  printf("\n");
  return;
}

void ListNameCard() {
  int j;

  printf("\n");
  printf("\nName_Card_ID Person_Name Company_Name \n");

  for (int i = 0; i < j; i  ){
       printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
  }
 printf("\n");
 return;
}

void GetNameCard() {
 printf("%d %s %s", nameCard[1].nameCardID, nameCard[1].personName, nameCard[1].companyName);
}

int main()
{
  int options;

  while (options != 5) {

  printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
  printf("\n");

  printf("What would you like to do? : ");
  scanf("%d", &options);

  switch (options)
  {
    case 1:
        ListNameCard();
        break;
    case 2:
        AddNameCard();
        break;
    case 3:
        printf("Case3 ");
        printf("\n");
        break;
    case 4:
        GetNameCard();
        break;
    default:
        printf("quit ");
  }

 }

CodePudding user response:

#include <stdio.h>

#include <stdlib.h>

#define Max_Size 5

typedef struct {
        int nameCardID;
        char personName[20];
        char companyName[20];

}
NameCard;

NameCard nameCard[Max_Size];

void AddNameCard() {
        printf("\n");
        int i;
        printf("enter the number of person you want to add :");
        scanf("%d", & i);
        printf("Enter Name Card ID: \n");
        scanf("%d", & nameCard[i - 1].nameCardID);
        printf("Enter Person Name: \n");
        scanf("%s", nameCard[i - 1].personName);
        printf("Enter Company Name : \n");
        scanf("%s", nameCard[i - 1].companyName);
        printf("\n");
        return;
}

void ListNameCard() {
        printf("\n");
        printf("\nName_Card_ID Person_Name Company_Name \n");
        for (int i = 0; i < Max_Size; i  ) {
                printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
        }
        printf("\n");
        return;
}

void GetNameCard() {
        int n;
        printf("\n");
        printf("enter the number of the person");
        scanf("%d", & n);
        printf("%d %s %s", nameCard[n - 1].nameCardID, nameCard[n - 1].personName, nameCard[n - 1].companyName);
        printf("\n");
}
int main() {
        int options;

        while (options != 5) {

                printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
                printf("\n");
                printf("What would you like to do? : ");
                scanf("%d", & options);

                switch (options) {
                case 1:
                        ListNameCard();
                        break;
                case 2:
                        AddNameCard();
                        break;
                case 3:
                        printf("Case3 ");
                        printf("\n");
                        break;
                case 4:
                        GetNameCard();
                        break;
                default:
                        printf("quit ");
                }
        }
}

CodePudding user response:

Let's follow the comments' suggestions.

First we avoid using options variable uninitialized by changing:

while (options != 5) {
    ...
}

To:

do {

    ... (set options variable here)

} while (options != 5);

Secondly, we change the name of j variable to count and use it as function parameter/return so we keep track and update the number of added cards. For instance, AddNameCard becomes:

int AddNameCard(int count)
{   
  if (count < Max_Size) {

    count  ;

    int i = count - 1;

    printf("\n");

    printf("Enter Name Card ID: ");
    scanf("%d", &nameCard[i].nameCardID);
    discard_newline();

    printf("Enter Person Name: ");
    scanf("[^\n]", nameCard[i].personName);
    discard_newline();

    printf("Enter Company Name: ");
    scanf("[^\n]", nameCard[i].companyName);
    discard_newline();

  } else {

    printf("Maximum number of cards reached\n");
  }   

  printf("\n");

  return count;
}   

(discard_newline prevents newline characters to creep into the next scanf. "[^\n]" format prevents buffer overrun into 20 length strings.) The name of an array variable is already taken as its address (or the address of its first element). So personName and companyName members shouldn't be preceded by &.

The code becomes:

#include <stdio.h>
#include <stdlib.h>

#define Max_Size 5

typedef struct
{
  int nameCardID;
  char personName[20];
  char companyName[20];

} NameCard;

NameCard nameCard[Max_Size];

void discard_newline(void)
{
  while( getchar() != '\n' );
}

int AddNameCard(int count)
{
  if (count < Max_Size) {

    count  ;

    int i = count - 1;

    printf("\n");

    printf("Enter Name Card ID: ");
    scanf("%d", &nameCard[i].nameCardID);
    discard_newline();

    printf("Enter Person Name: ");
    scanf("[^\n]", nameCard[i].personName);
    discard_newline();

    printf("Enter Company Name: ");
    scanf("[^\n]", nameCard[i].companyName);
    discard_newline();

  } else {

    printf("Maximum number of cards reached\n");
  }

  printf("\n");

  return count;
}

void ListNameCard(int count) {

  if (count > 0) {

    printf("\nName_Card_ID Person_Name Company_Name\n");

    for (int i = 0; i < count; i  ){
         printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
    }

  } else {

    printf("Empty list: none added yet\n");
  }

  printf("\n");

   return;
}

void GetNameCard() {
 printf("%d %s %s", nameCard[1].nameCardID, nameCard[1].personName, nameCard[1].companyName);
}

int main()
{
  int options;
  int count = 0;

  do {

    printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
    printf("\n");

    printf("What would you like to do? : ");
    scanf("%d", &options);

    switch (options)
    {
      case 1:
          ListNameCard(count);
          break;
      case 2:
          count = AddNameCard(count);
          break;
      case 3:
          printf("Case3 ");
          printf("\n");
          break;
      case 4:
          GetNameCard();
          break;
      default:
          printf("quit\n");
    }

  } while (options != 5);
}

Running it:

1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit

What would you like to do? : 1 
Empty list: none added yet 

1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit

What would you like to do? : 2 

Enter Name Card ID: 123 
Enter Person Name: John Smith
Enter Company Name: COMP_01

1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit

What would you like to do? : 1 

Name_Card_ID Person_Name Company_Name
123 John Smith COMP_01 

1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit

What would you like to do? : 5 
quit

You still have to complete the other options.

  •  Tags:  
  • c
  • Related