1. ## Having problem with a program for Calculating Grades

I've been working on this program and wondering why I can't get it to work. Any insight on the issue can be helpful.
Code:
```// Program is to convert number scores into Grade Scores using if and elseif statements

#include <stdio.h>
#include <conio.h>

int getScore;
(int numScore,

int getScore()
{
int numScore;
scanf("%i", &numscore);
}

{
if (numscore <= 100 || numscore >= 90)
printf("You got an A");
else if
(numscore <=89 || numscore >=80)
printf("You got a B");
else if
(numscore <=79 || numscore >= 70)
printf("You go a C");
else if
(numscore <=69 || numscore >=60);
printf(" You got a D");
else if (numscore <= 59);

printf(" You got a F");
else
printf("You got a Z, This is not a valid score");
}

{
int numscore
printf("What score did you get in the class?\n");

}

int main ()
{

printf(" Welcome to the Grade Conversion Program!\n");
system("Pause");
return(0);

}```

2. For starters, your main function doesn't call any of your functions.

Quzah.

3. you have to call these functions in main().

4. Now getting errors with too few arguements in function convertGrade and showGrade
Code:
```// Program is to convert number scores into Grade Scores using if and elseif statements

#include <stdio.h>
#include <conio.h>

//int getScore;
(int numscore,

int getScore()
{
printf("What score did you get in the class?\n");
int numscore;
scanf("%i", &numscore);
}

{
//      		int numscore;
if (numscore <= 100 || numscore >= 90)
printf("You got an A");
else if
(numscore <=89 || numscore >=80)
printf("You got a B");
else if
(numscore <=79 || numscore >= 70)
printf("You go a C");
else if
(numscore <=69 || numscore >=60)
printf(" You got a D");
else if (numscore <= 59)
printf(" You got a F");
else
printf("You got a Z, This is not a valid score");
}

{

}

int main ()
{

printf(" Welcome to the Grade Conversion Program!\n");

getScore();
system("Pause");
return(0);

}```

5. You seem confused about how functions work. Here:
returntype functionname( argument list )

If the function does not need to send anything back to where it was called from, then returntype can be void, otherwise, this is some variable type (like "int").

Then you give the function a name. That's how you call it (make it do its task).

The arguments are values you pass to the function when you call it. You must pass a value for each argument you said your function is supposed to receive.

Quzah.

6. Quzah hit the nail right on the head. Also, return statements send output from a function to its caller.

I recommend writing this program as one, big function, before breaking it down into multiple functions.

7. Originally Posted by Babkockdood
I recommend writing this program as one, big function, before breaking it down into multiple functions.
Granted there is not much code there so it could be just the mainline program.

However; there is a lot of advantage in breaking larger jobs down to component blocks like the OP did and building explicit functions right from the beginning. If nothing else it compartmentalizes variable and effects on variables making the code easier to debug.

When I write a program I first think about the best way to solve the problem... then I break it down to it's component parts... Get scores, calculate average, convert to letter grade etc. This breakdown is actually the basis for a list of functions that need to be written.

Then when working on each of the parts --functions-- I only need to think about what I'm trying to accomplish at the moment, without worrying about the "big picture" at all. My variables are local and don't affect the mains, so if something messes up I don't have to troubleshoot my entire program, just the misbehaving bit.

The "big blob" concept is fine for simple programs and examples, but when you start writing serious code it more often than not serves to undermine the problem solving effort.

8. The program compiles and runs but doesn't give the data I want whenever I enter any data it comes up A2686752

Code:
```
// Program is to convert number scores into Grade Scores using if and elseif statements

#include <stdio.h>
#include <conio.h>

int getScore();

int getScore()
{
int numscore;
printf("What score did you get in the class?\n");

scanf("%d", &numscore);
}

{

if (numscore <= 100 || numscore >= 90)
printf("You got an A");
else if
(numscore <=89 || numscore >=80)
printf("You got a B");
else if
(numscore <=79 || numscore >= 70)
printf("You go a C");
else if
(numscore <=69 || numscore >=60)
printf(" You got a D");
else if (numscore <= 59)
printf(" You got a F");
else
printf("You got a Z, This is not a valid score");
}

{

}

int main ()
{

printf(" Welcome to the Grade Conversion Program!\n\n");
int numscore;
getScore();
system("Pause");
return(0);

}```

9. In getscore(), the variable numscore is local. As soon as your program returns from getscore(), numscore is out of scope, and goes to "heaven".

Why not return the value numscore? (or you can send the address of numscore from main, into getscore(), and make your changes "stick".

10. Now I get kind of the same results as before the integers are out of scope and it never calls the void Show Grade Function
Code:
```
// Program is to convert number scores into Grade Scores using if and elseif statements

#include <stdio.h>
#include <conio.h>

int getScore(int numscore);

int getScore(int numscore)
{
//   int numscore;
printf("What score did you get in the class?\n");

scanf("%i", &numscore);
}

{

if (numscore <= 100 || numscore >= 90)
else if
(numscore <=89 || numscore >=80)
else if
(numscore <=79 || numscore >= 70)
else if
(numscore <=69 || numscore >=60)
printf(" You got a D\n", letterGrade);
else if (numscore <= 59)
printf(" You got a F\n", letterGrade);
else
printf("You got a Z, This is not a valid score\n", letterGrade);
}

{

}

int main ()
{

printf(" Welcome to the Grade Conversion Program!\n\n");
int numscore;
getScore(numscore);
system("Pause");
return(numscore);

}```

11. I think you should re-read the parts of your book/tutorial that deal with the way functions work....

When you pass in a value, internally the function operates on a copy of the variable. Thus changing a variable inside a function does not affect any same-name variables outside the function. Moreover, when the function exits, it's internal variables are destroyed so there is no way to access them.

In the case of your GetScore() function you are not passing in anything with meaning so the parameter list can be (should be) void. However you are generating a value that you need outside the function so you want to return that value from the function.

In main() you would call GetScore and assign it's returned value to a variable that you can then pass into your other functions to display results...

I fixed the GetScore() function for you... now you get to fix the rest.

Code:
```#include <stdio.h>
#include <conio.h>

int getScore(void)
{ int numscore;
printf("What score did you get in the class?\n");
scanf("%i", &numscore);
return numscore;  }

// rest of code

int main ()
{ int score;

printf(" Welcome to the Grade Conversion Program!\n\n");
score = getScore();

system("Pause");
return(0);

}```

12. You need to review the meaning of AND and OR.
Code:
`if (numscore <= 100 || numscore >= 90)`
should be using &&. Same with the others.

13. I think the more and more I work on it I get confused. Score is returned in the int getScore(void). Then when it goes through main that function score becomes the value of the getscore(). Should I have my convertGrade to read the numscore integer or the score integer? The problem I'm still having is my character that has to be returned through letterGrade is not displaying when I call the function showGrade().
Code:
```// Program is to convert number scores into Grade Scores using if and elseif statements

#include <stdio.h>
#include <conio.h>

int getScore(void);

int getScore(void)
{
int numscore = 0;
printf("What score did you get in the class?\n");

scanf("%i", &numscore);
return numscore;
}

{

if (numscore <= 100 || numscore >= 90)
//   		printf("You got an A\n", letterGrade);
else if
(numscore <=89 || numscore >=80)
//      	printf("You got a B\n", letterGrade);
else if
(numscore <=79 || numscore >= 70)
//      	printf("You go a C\n", letterGrade);
else if
(numscore <=69 || numscore >=60)
//  		 printf(" You got a D\n", letterGrade);
else if (numscore <= 59)
//	           		 printf(" You got a F\n", letterGrade);
else
//  	 printf("You got a Z, This is not a valid score\n", letterGrade);
}

{
score = getScore();

}

int main ()
{

printf(" Welcome to the Grade Conversion Program!\n\n");
int score;
int numscore;

score = getScore();
system("Pause");
return(0);

}```

I think the more and more I work on it I get confused. Score is returned in the int getScore(void). Then when it goes through main that function score becomes the value of the getscore(). Should I have my convertGrade to read the numscore integer or the score integer? The problem I'm still having is my character that has to be returned through letterGrade is not displaying when I call the function showGrade().
Ok... set that code aside for a few minutes. Try a couple of simple exercises to understand how functions work. Of course they work the same way in your code as they do anywhere else, it's just a matter of what you feed them and what comes back...

But first a big hint... trying to fix your code with scoop and poop is just a big waste of time and it teaches you nothing... to understand, you have to do it for yourself.

Code:
```#include <stdio.h>

// example 1 ... nothing in, nothing returned
// this function does it's thing all by itself

void SayHello(void)
{ printf("Hello\n"); }

// example 2... something in nothing returned
// this function uses data but does not return a value

void PrintValue( int a)
{ printf("%d  ", a); }

// example 3 ... nothing in something out
// here we don't give it anything but we get a random number back

int GetRand(void)
{ return rand(); }

// example 4 ... stuff in, stuff returned
// this one does an operation on data fed in and returns the result

int GetDouble (int a)
{ return a *2 }

// now to see what all this does...
int main (void)
{ int x;
int y;

SayHello();                // nothing in, nothing back
x = GetRand();             // nothing in result in x
PrintValue(x);             //  x goes in nothing comes back
y = GetDouble(x);          //   x goes in y gets what comes back
PrintValue(y);             //    y in nothing back

return 0; }```
Play with this until you understand it... then apply what you've learned to your own code.

15. It's obvious the OP is confused about functions.

OP, put all your code in the main function, and read up on functions before breaking down your code into multiple functions.

Cprogramming.com Tutorial: Functions