Home > front end >  Reading string value appears as NULL
Reading string value appears as NULL

Time:02-26

This program when it receives the input 1, it receives info from the user and if the input 2 is given it shows what it has stored so far.

The issue is that when you select 2 after having inserted the required info, the name that was provided appears as NULL.

When I run it, it goes like that:

Menu
1. Insert new member
2. Show information from the members
3. Other
4. Exit
Insert your choice:1
Name please:Mary jad
Insert the cost:12
1
Mary jad
12

Menu
1. Insert new member
2. Show information from the members
3. Other
4. Exit
Insert your choice:2
1     (null)    12     #the name appears as null

The code so far is the following:

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

#define NAME_LEN 15
struct member{
int AM;
char OE[NAME_LEN];
int XS;
struct member *next;};



struct member *insert_member (struct member *head, int AM1)
{

  char OE1[NAME_LEN];
  printf("Name please:");
  fflush(stdin);
  scanf("%[^\n]%*c", &OE1);
  int XS1;

  do {
  printf("Insert the cost:");
  fflush(stdin);
  scanf("%d", &XS1);
  }while (XS1<=0);

  printf("%d\n", AM1);
  printf("%s\n", OE1);
  printf("%d\n", XS1);

   { struct member *ptr;
       ptr=(struct member *) malloc(sizeof (struct member));

       ptr-> AM=AM1;
       ptr -> OE[NAME_LEN]=OE1[NAME_LEN];
       ptr -> XS=XS1;
       if(head==NULL)
         ptr->next=NULL;
       else
         ptr->next=head;
      head=ptr;
     return(head);

  }


}

void display_list(struct member *ptr)
{

  while (ptr!=NULL)
  {
    printf("%d     %s    %d\n",ptr->AM, ptr->OE[NAME_LEN], ptr->XS);
    ptr=ptr->next;

  }
printf("\n");
}


int main() {
  system ("chcp 1253>NULL");
 int i; struct member *head, *ptr1, *ptr2, *ptr3;
 int choice;
 int AM1=0, XS1;
 char OE1[15];
 head = NULL;
 do{
    printf("Menu\n");
    printf("1. Insert new member\n");
    printf("2. Show information from the members\n");
    printf("3. Other\n");
    printf("4. Exit\n");
    printf("Insert your choice:"); scanf("%d",&choice);
    switch (choice){
        case 1:
            AM1  ;   // Each new member gets AM  1 which is like an ID number
            head = insert_member(head, AM1);
            break;
        case 2:
            if (head!=NULL)
                display_list(head);
            else
                printf("The list is empty");
            break;
        case 3:
            printf("lala");
            break;
        case 4:
            break;
        default:
            break;
    }
    printf("\n");
  } while (choice!=4);
}

CodePudding user response:

Instead of

scanf("%[^\n]%*c", &OE1);
                  ^^^  

you need to write

scanf("%[^\n]%*c", OE1);

This statement

ptr -> OE[NAME_LEN]=OE1[NAME_LEN];

does not make a sense.

You need to write

#include <string.h>

//...

strcpy( ptr -> OE, OE1 );

This functionality of this if statement

   if(head==NULL)
     ptr->next=NULL;
   else
     ptr->next=head;

can be performed just by one statement

ptr->next = head;

And this statement

printf("%d     %s    %d\n",ptr->AM, ptr->OE[NAME_LEN], ptr->XS);

must be substituted for this one

printf("%d     %s    %d\n",ptr->AM, ptr->OE, ptr->XS);
  • Related