1

When I entered the age input "20" hit Enter, it goes to a new line, hit Enter again, it goes to a new line again. No matter how many time I hit enter it didn't get out of it until I typed "harri" or any char/words,then it get out of the code then display the next line of code. Why is that? How I can fix it so that when I type "20" Enter, then it will execute next line of code(printf(" Please enter your name\n"))?

Output: output

void print_Struct_Element(List *list) {
   printf("String name: %s\n",list->head->name);
   printf("Age data: %d\n",list->head->age);

   printf("head address %p\n",list->head );
   printf("tail address %p\n",list->tail );
   printf("head value %p\n",&list->head );
   printf("tail value %p\n",&list->tail );
}



int main() {

char name[10];
int age, count;
printf("Please enter your age ");
scanf("%d\n",&age);
printf(" Please enter your name\n");
scanf("%s\n",name);

List moneyManagerList;
insertAtHead(&moneyManagerList,name,age );
print_Struct_Element(&moneyManagerList);
return 0;
}

And this is my other attempt; all fail:

int main() {

  char name[10];
  int age, count;
  // printf("Please enter your name and age  \n ");
  //  sscanf("harrison 30", "%s %d",name, &age );
  printf("Please enter your age ");
    //fgets(age, 5, stdin);

   scanf("%d\n",&age);

  printf(" Please enter your name\n");
   fgets(name,10, stdin);
    //scanf("%s\n",name);

  List moneyManagerList;
  insertAtHead(&moneyManagerList,name,age );
  //moneyManagerList.head->name = strcpy(moneyManagerList.head->name, name );
  //printf("head string name %s\n",moneyManagerList.head->name );
  print_Struct_Element(&moneyManagerList);

  return 0;
}

Github link:https://github.com/lambigini87/linked-link-moneyManager-project/blob/master/main.c

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

2

My answer could have been a comment but I don't have enough points to comment. So, here it is -- get rid of "\n" inside your scanf function.

Nakini
  • 772
  • 1
  • 8
  • 19
1

Please remove \n from the scanf.

scanf("%d",&age);

And if you have any integer/float read statement, then it will try to read integer/float until a value is given. However the scanf terminates immediately if you provide string for integer in the console.

I. Ahmed
  • 2,438
  • 1
  • 12
  • 29