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

typedef struct Student
{
   int GL;
   char FN[80];
   char LN[80];
   double GPA;

}SArray[956];

int main(void)
{
   int OP = 0;
   int i;
   int x = 0;
   struct Student SArray[956];

   while(x == 0)
   {
      printf("Welcome to teh ARX7 SYSTEM. Enter 1 to enter information, 2.............\n");
      scanf("%d", &OP);

      if (OP == 1)
      {
         printf("Enter Student ID:\n");
         scanf("%d", &i);

         printf("Enter Grade Level:\n");
         scanf("%d",&SArray[i].GL);

         printf("Enter First Name:\n");
         gets("%s",&SArray[i].FN);

         printf("Enter Last Name:\n");
         gets("%s",&SArray[i].LN);

         printf("Enter GPA:\n");
         scanf("%lf", &SArray[i].GPA);
      }

      if (OP == 2)
      {
         printf("Enter Student ID:\n");
         scanf("%d", &i);

         printf("Grade: %d",&SArray[i].GL);
         printf("First name: %s\n", SArray[i].FN);
         printf("Last name: %s\n", SArray[i].LN );
         printf("GPA: %lf ", &SArray[i].GPA);
      }
   }
}

I'm trying to create a program that stores and displays data for students. However, I keep getting these two errors:

main.c:71:23: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
                    printf("Grade: %d",&SArray[i].GL);


main.c:74:24: warning: format specifies type 'double' but the argument has type 'double *' [-Wformat]
                    printf("GPA: %lf ", &SArray[i].GPA);

INPUT:

Welcome to teh ARX7 SYSTEM. Enter 1 to enter information, 2.............
1
Enter Student ID:
1
Enter Grade Level:
2
Enter First Name:
ee
Enter Last Name:
eee
Enter GPA:
3

OUPUT:

Welcome to teh ARX7 SYSTEM. Enter 1 to enter information, 2............. 
2
Enter Student ID:
1
Grade: -1264132192
First name: ee
Last name: eee
GPA: 0.000000
royhowie
  • 11,075
  • 14
  • 50
  • 67
Hydra King
  • 29
  • 2
  • `scanf` needs a pointer to the object, so that it can store a value in the object. `printf` just needs the value of the object. So `scanf` needs `&i` (the address of `i`) whereas `printf` just needs `i` (the value of `i`). Also, `gets` is evil and should not be used. Use `fgets` instead. And consult the documentation, since you weren't calling `gets` correctly anyways. – user3386109 Jan 27 '16 at 22:08
  • The error went away, but now I'm getting incorrect output for Grade Level and GPA. My program outputs the correct first and last name inputted, but this not the case for GL and GPA. Thoughts as to why? – Hydra King Jan 27 '16 at 22:23
  • Nope, no idea. Might help to add the terminal contents (i.e. user input and program output) for a sample run. – user3386109 Jan 27 '16 at 22:27
  • That was a single run. – Hydra King Jan 27 '16 at 22:36
  • What did you do about the `gets`? Please add the updated code to the question. – user3386109 Jan 27 '16 at 22:38
  • My teacher said using gets was fine, and I'm not sure how to use fgets. – Hydra King Jan 27 '16 at 22:45
  • Ok, but `gets` only takes one argument, e.g. `gets(SArray[i].FN);` – user3386109 Jan 27 '16 at 22:48
  • See this other question [here](http://stackoverflow.com/questions/26192934/how-to-supress-warning-gets-is-deprecated) for help on how to use `fgets` instead of `gets`. – Ahmed Akhtar Jan 28 '16 at 09:15

2 Answers2

2

Take out the '&' in lines 71 and 74. You're providing the printf function a pointer to the value you want - its address - instead of the value itself.

andydavies
  • 3,081
  • 4
  • 29
  • 35
0
  • You are declaring the array of struct named SArray[956] twice, once gloabally and once locally too, don't do that, and no need for struct keyword when using typedef like this.
  • Also, passing an array by its name readily gets its reference, so do not use & for FN and LN.
  • And dont use & for passing a variable to printf
  • Also, gets needs no format specifier such as %s. (gets is depricated and you should prefer using fgets see here.)
  • The getchar() before first gets is to ignore the extra \n in the stream because of the last scanf.

Just making these changes:

#include <stdio.h>
#include <string.h>
typedef struct
{
   int GL;
   char FN[80];
   char LN[80];
   double GPA;

}Student;

int main(void)
{
   int OP = 0;
   int i;
   int x = 0;
   Student SArray[956];

   while(x == 0)
   {
      printf("Welcome to teh ARX7 SYSTEM. Enter 1 to enter information, 2.............\n");
      scanf("%d", &OP);

      if (OP == 1)
      {
         printf("Enter Student ID:\n");
         scanf("%d", &i);

         printf("Enter Grade Level:\n");
         scanf("%d",&SArray[i].GL);

         getchar();
         printf("Enter First Name:\n");
         gets(SArray[i].FN);

         printf("Enter Last Name:\n");
         gets(SArray[i].LN);

         printf("Enter GPA:\n");
         scanf("%lf", &SArray[i].GPA);
      }

      if (OP == 2)
      {
         printf("Enter Student ID:\n");
         scanf("%d", &i);

         printf("Grade: %d\n",SArray[i].GL);
         printf("First name: %s\n", SArray[i].FN);
         printf("Last name: %s\n", SArray[i].LN );
         printf("GPA: %lf\n", SArray[i].GPA);
      }
   }
}
Community
  • 1
  • 1
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28