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
8
  • 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. Commented Jan 27, 2016 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? Commented Jan 27, 2016 at 22:23
  • Nope, no idea. Might help to add the terminal contents (i.e. user input and program output) for a sample run. Commented Jan 27, 2016 at 22:27
  • That was a single run. Commented Jan 27, 2016 at 22:36
  • What did you do about the gets? Please add the updated code to the question. Commented Jan 27, 2016 at 22:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
      }
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.