Can anyone point out the obvious? I can't seem to find what I've done wrong.
This code produces quite a bit of a pause followed by no visible output. Debug crashes so it's a real good attempt.
Edit; The intents just to understand how to output a char via a user defined function when a int is passed to it. eg you enter a test score of 85 printf should print HD. I had no idea getchar() was ASCII value, this might explain the output I was getting earlier. Thanks Bodo.
If I change the printf - %d I do get numbers so thought I was getting ASCII but they have not added up to something I've been able to decrypt. Numerical entry of 85 = 100 & 65 = 104
If it's super wrong let me know and I'll go back and make this code work without a user defined function first.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char determine_grade(int test_score);
int main()
{
int test_score = 0;
char *test_result;
printf("enter test score:");
test_score =getchar();
test_result = determine_grade(test_score);
printf("grade is: %s", test_result);
return 0;
}
char determine_grade(int test_score)
{
char *test_result;
if (test_score >= 85)
test_result = "HD";
else if (test_score >= 75)
test_result = "D";
else if (test_score >= 65)
test_result = "C";
else if (test_score >= 55)
test_result = "P1";
else if (test_score >= 50)
test_result = "P2";
else if (test_score >= 40)
test_result = "F1";
else
test_result = "F2";
return test_result;
}
char, not achar*- also turn on compiler warnings, your compiler will clearly tell you where the problems areenter test score:? Functiongetchar()will return the code (ASCII value or other encoding) of a single character or the first byte of a multi-byte character. Indetermine_gradeyou compare the value with numbers85...40which would correspond to ASCII characters85 = 'U',75 = 'K'etc. So any input that starts with, for example'U','V','[','a'will match the first condition>= 85, input with any of'K'...'T'will match the second condition>=75etc. Is this what you want? Please edit your question to answer.