0

Below is my code.

For simplicity I made the code to take the info about 2 friends for now. But the problem is my for loop in getting user input.

It only works if I remove this part here

printf("Enter Age: ");
            scanf("%d", friends[a].age);

here is the full for-loop

for(a=0; a<2; a++) {

        printf("Friend no. %d\n", friends[a].fnum+1);

        printf("Enter Name: ");
            gets(friends[a].name);

        printf("Enter Town: ");
            scanf("%s", friends[a].address);

       printf("Enter Age: ");
            scanf(" %d", friends[a].age);


        printf("Enter Course: ");
            scanf(" %s", friends[a].course);

        printf("Favorite foods! \n");
            printf("Dish:");
                scanf(" %s", friends[a].fav_dish);

            printf("Snack:");
                scanf(" %s", friends[a].fav_snack);

            printf("Drink:");
                scanf(" %s", friends[a].fav_drink);


        printf("\n");
    }

At first, I thought it was the age part was the problem, so I brought it last. But it did not solve it. I thought it was the scanf too, so I added a space in scanf(" %s", friends[a].course); But nothing work.

I also tried this

printf("Enter Age: ");
    scanf(" %d\n ", friends[a].age);

so I tried deleting

printf("Enter Age: ");
            scanf("%d", friends[a].age);

and the loop continued to friend 2

What should I do?

Here is my full source code:

#include <string.h>

struct myFriends {

    char name[30];
    char address[20];
    int age;
    char course[20];
    char fav_dish[20], fav_snack[20], fav_drink[20];
    }; struct myFriends friends[2];


int main () {
    int a;

    puts("Please enter the following info for 2 friends");

    for(a=0; a<2; a++) {

        printf("Friend no. %d\n", a+1);

        printf("Enter Name: ");
            scanf("%s", friends[a].name);

        printf("Enter Town: ");
            scanf("%s", friends[a].address);

        printf("Enter Age: ");
            scanf(" %d\n ", friends[a].age);

        printf("Enter Course: ");
            scanf(" %s", friends[a].course);

        printf("Favorite foods! \n");
            printf("Dish:");
                scanf(" %s", friends[a].fav_dish);

            printf("Snack:");
                scanf(" %s", friends[a].fav_snack);

            printf("Drink:");
                scanf(" %s", friends[a].fav_drink);


        printf("\n");
    }

    printf("Here are the details");

    for (a=0; a<2; a++) {
        printf("\n\n Friend no. %d", a+1);

        printf("Name:");
            puts(friends[a].name);

        printf("Town: ");
            puts(friends[a].address);

        printf("Age: ");
            printf("%d", friends[a].age);

        printf("Fave FOODS!: ");
            puts(friends[a].fav_dish);
            puts(friends[a].fav_snack);
            puts(friends[a].fav_drink);
    }

    return 0;


}


halfer
  • 19,824
  • 17
  • 99
  • 186
PoorGamer
  • 11
  • 3
  • 9 times out of ten, the for loop is fine and you just misuse scanf. E.g. by ignoring the return value or failing to give an address for the scanned values. http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – Yunnosch May 22 '20 at 12:02
  • 1
    Please provide a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). How is `friends` declared? Probably should be `scanf(" %d ", &friends[a].age);` – kaylum May 22 '20 at 12:03
  • @kaylum I just added my full source code... – PoorGamer May 22 '20 at 12:07
  • 1
    Does this answer your question? [Why scanf must take the address of operator](https://stackoverflow.com/questions/3893615/why-scanf-must-take-the-address-of-operator) – DarkAtom May 22 '20 at 12:07
  • Also, DO NOT use the `%s` specifier. Use `%29s`, for example, where 29 is one less than the size of the character array. – DarkAtom May 22 '20 at 12:10
  • I don't think it's approrirate to close a "debug this code" question as a duplicate of a question asking why a language behaves the way it does. Voting to reopen. – pppery Jun 15 '20 at 00:46

1 Answers1

0

In here, I add & mark to pass pointers to scanf, so try this. I hope this works for you. #include

#include <stdio.h>
#include <string.h>

struct myFriends {

    char name[30];
    char address[20];
    int age;
    char course[20];
    char fav_dish[20], fav_snack[20], fav_drink[20];
}; struct myFriends friends[2];


int main () {
    int a;

    puts("Please enter the following info for 2 friends");

    for(a=0; a<2; a++) {
        char name[30];
        char address[20];
        int age;
        char course[20];
        char fav_dish[20], fav_snack[20], fav_drink[20]
        printf("Friend no. %d\n", a+1);

        printf("Enter Name: ");
            scanf("%s", &name);
            friends[a].name=name;
        printf("Enter Town: ");
            scanf("%s", &address);
            friends[a].address=address;
        printf("Enter Age: ");
            scanf(" %d\n ", &age);
            friends[a].age=age;
        printf("Enter Course: ");
            scanf(" %s", &course);
            friends[a].course=course;
        printf("Favorite foods! \n");
            printf("Dish:");
                scanf(" %s", &fav_dish);
                friends[a].fav_dish=fav_dish;
            printf("Snack:");
                scanf(" %s", &fav_snack);
                friends[a].fav_snack=fav_snack;
            printf("Drink:");
                scanf(" %s", &fav_drink);
                friends[a].fav_drink=fav_drink;

        printf("\n");
    }

    printf("Here are the details");

    for (a=0; a<2; a++) {
        printf("\n\n Friend no. %d", a+1);

        printf("Name:");
            puts(friends[a].name);

        printf("Town: ");
            puts(friends[a].address);

        printf("Age: ");
            printf("%d", friends[a].age);

        printf("Fave FOODS!: ");
            puts(friends[a].fav_dish);
            puts(friends[a].fav_snack);
            puts(friends[a].fav_drink);
    }

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278