0

I am studying C in college, I'm working on a code that if the GPA of a student is less than 2 >> it adds 0.2 to it

the problem that in debugging ..the value of X = 0 , not equal to f (the return of the function)

#include <stdio.h> #include <stdlib.h> typedef struct user { int userID; char Firstname[25]; float gpa; }; int GPAedit (float x) { float f; if (x < 2) f = x + 0.2; else f = x; return f; } int GPAedit(float); int main() { int i = 0; float x; struct user S[i]; for (i == 0; i < 1; i++) { printf("enter user %d ID", i); scanf("%d", &S[i].userID); printf("enter user %d name\n", i); scanf("%s", S[i].Firstname); printf("enter user %d GPA\n", i); scanf("%f", &S[i].gpa); x = GPAedit(S[i].gpa); S[i].gpa = x; printf("\n\n"); } for (i = 0; i < 1; i++) { printf("user %d ID\n", S[i].userID); printf("%s\n", S[i].Firstname); printf("%.4f\n", S[i].gpa); } } 
5
  • 1
    Another issue: in your for loop, i == 0 should be i = 0 Commented Dec 21, 2015 at 2:12
  • Your code is pure C, remove the C++ tag Commented Dec 21, 2015 at 2:13
  • This is an unexpected twist on the typedef syntax! I learned something today. Commented Dec 21, 2015 at 2:24
  • I'm still discovering the whole thing here ..be patient with me please :) @chqrlie what's the typedef issue that you are talking about ? Commented Dec 21, 2015 at 2:36
  • I updated my answer. My remark was not harsh: it is this first time I see a typedef with no name, you only get a warning for this, it is not an error but a very surprising use of typedef. Commented Dec 21, 2015 at 3:01

3 Answers 3

3

In the main function, you define user as

struct user S[i]; 

But i has the value 0 when this definition is executed.

Define the array with a size greater than 0.

GPAedit should be defined to return float. Returning int, it truncates the value so instead of increasing by 0.2 points, it may actually reduce the low GPAs.

It would be more consistent to change the API this way:

void GPAedit(struct user *up) { if (up->gpa < 2) up->gpa += 0.2; } 

And call this function with the address of the structure:

GPAedit(&S[i]); 

Also note that the typedef is useless in your definition of struct user. typedef is used to create type aliases such as:

typedef struct user { int userID; char Firstname[25]; float gpa; } user; 

You can then use user as a type instead of struct user.

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

2 Comments

I tried using a random number so i put "struct user S[10];" but still x doesn't change
wrong return type on GPAedit(), MehrdadMomeny was quicker to spot it!
1

Your GPAedit() function is stripping off the precision part of your value by returning a float variable as int:

int GPAedit (float x ) { float f ; if ( x < 2) f = x+0.2 ; else f = x; return f; } 

It should return float like this:

float GPAedit (float x ) { float f ; if ( x < 2) f = x+0.2 ; else f = x; return f; } 

Also, you don't need to redefine it over your main function, since it's already defined up there, so remove this:

int GPAedit (float ); 

from the line before int main()

1 Comment

You are absolutely Right ! ... Thanks a lot ,it works :)
1

in int GPAedit (float x), you return int so your float is truncated.

you probably ant to return float

2 Comments

MehrdadMomeny deserves the checkmark, he gave this answer first.
@chqrlie i checked his answer many times ,but it's removed... , didn't notice that it's only for 1 answer :D [STUPID] sorry :)..Thank you too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.