0

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;
}
4
  • 3
    Your function returns a char, not a char* - also turn on compiler warnings, your compiler will clearly tell you where the problems are Commented May 10, 2021 at 9:58
  • Thanks, working in VS and I closed it out and re added it. Compiler then had warnings where as before it was compiling. Strange. Commented May 10, 2021 at 10:14
  • 2
    @desmo This statement test_score =getchar(); does not make a sense. Instead use scanf( "%d", &test_score ); Commented May 10, 2021 at 10:27
  • 1
    What do you expect the user to enter after enter test score:? Function getchar() will return the code (ASCII value or other encoding) of a single character or the first byte of a multi-byte character. In determine_grade you compare the value with numbers 85...40 which would correspond to ASCII characters 85 = '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 >=75 etc. Is this what you want? Please edit your question to answer. Commented May 10, 2021 at 10:30

1 Answer 1

2

For starters neither declaration from these headers

#include <stdlib.h>
#include <string.h> 

are used in your program. So you may remove them.

The function determine_grade returns a pointer to a string literal insetead of an object of the type char, Also the parameter test_score should have an unsigned integer type. So the function should be declared like

const char * determine_grade( unsigned int test_score );

and in its definition you have also to write

const char * determine_grade( unsigned int test_score )
{
    const char *test_result;
    //...
}

Correspondingly in main you should write

unsigned int test_score = 0;
const char *test_result;

This call of getchar does not make a sense because the function returns just one internal code of a character. Instead write

scanf( "%u", &test_score );
Sign up to request clarification or add additional context in comments.

1 Comment

A massive thank-you Vlad and the rest of you. I'm off to learn what an unsigned int is and pull this solution apart to understand it better.

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.