0

Hello I have a problem with integer. This is my code:

#include <stdio.h>
#include <stdlib.h>
struct data
{
    char name[50];
    int grade[1];
};

int main()
{
    struct data persons[30];
    int n = 3;
    int i;

    for(i=0;i<n;i++)
    {
        printf("Type person name nr: [%d] ",i+1);
        scanf("%s",persons[i].name);
        printf("Type grade: (from 1 to 6) ");
        scanf("%d",persons[i].grade);
    }

    for(i=0;i<n;i++)
    {
        printf("Name [%d]: %s\n",i+1,persons[i].name);
        printf("Grade [%d]: %d\n",i+1,persons[i].grade);

    }

    return 0;
}

And when I type some names and grades the output isn't correct: This is my output

adrian
  • 35
  • 5
  • 1
    Your compiler should've printed some warnings. If not, compile with `-Wall`. – tkausl Jan 25 '19 at 08:14
  • 1
    The correct way `&persons[i].grade`. Please, use `-Wall` and `-Werror` flags every time when compiling your program. – funnydman Jan 25 '19 at 08:16
  • This is probably the millionth question on SO regarding missing & in scanf. Voting to close as simple typo. – Lundin Jan 25 '19 at 08:30
  • Also, `int grade[1];` declares an array of 1 integer. While there are legitimate uses for a one-element array (in certain cases where you want an address to provide allocation beyond the struct itself, such as when implementing a memory allocation scheme) -- this isn't one. If you simply need one `int`, declare `int grade;` and remove the `[1]`. – David C. Rankin Jan 25 '19 at 08:34

4 Answers4

3

You are using an array for a single grade, when a standard int is enough.

struct data
{
    char name[50];
    int grade;
};

And then you also need to pass the reference of the int to scanf using an &.

scanf("%d", &persons[i].grade); 
visibleman
  • 3,175
  • 1
  • 14
  • 27
3

The corrected version of your program:

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

struct data {
    char name[50];
    int grade; // In you case, to store one integer value just use int varibale because it seems more logical.
};

int main() {
    struct data persons[30];
    int n = 3;
    int i;

    for (i = 0; i < n; i++) {
        printf("Type person name nr: [%d] ", i + 1);
        scanf("%s", persons[i].name);
        printf("Type grade: (from 1 to 6) ");
        scanf("%d", &persons[i].grade); // To fill member of structure you may use '&' sign because scanf gets address.
    }

    for (i = 0; i < n; i++) {
        printf("Name [%d]: %s\n", i + 1, persons[i].name);
        printf("Grade [%d]: %d\n", i + 1, persons[i].grade);

    }

    return 0;
}

Also, keep in mind that compiler not always tell you that something is going to be wrong, in most cases using special flags (some of them I mentioned in the comment) for compiler can give you more information.

It's worth to note that current IDEs highlight obvious errors and really speed up your development process.

funnydman
  • 9,083
  • 4
  • 40
  • 55
0

you have to replace

printf("Grade [%d]: %d\n",i+1,persons[i].grade);

by

printf("Grade [%d]: %d\n",i+1,persons[i].grade[0]);

in the scanf you do scanf("%d",persons[i].grade); so because grade is a vector you give its address, but in the printf you have to specify the index else you print the address of the vector

However to use a vector is useless because it contains only one element, better to directly use an int for grade, and of course in that case you have to give &persons[i].grade for the scanf and persons[i].grade for the printf


Execution :

Type person name nr: [1] a
Type grade: (from 1 to 6) 1
Type person name nr: [2] b
Type grade: (from 1 to 6) 2
Type person name nr: [3] c
Type grade: (from 1 to 6) 3
Name [1]: a
Grade [1]: 1
Name [2]: b
Grade [2]: 2
Name [3]: c
Grade [3]: 3
bruno
  • 32,421
  • 7
  • 25
  • 37
0

It's because grade is an array, so you need to do .grade[0]when writing or reading data to / from it, like so:

scanf("%d",persons[i].grade[0]);
...
printf("Grade [%d]: %d\n",i+1,persons[i].grade[0]);
Leo
  • 741
  • 6
  • 14