0

I wrote this C program using 2 functions, but in this first case grade() is not prompting for the input:

#include <stdio.h>
int motivQuote();
char grade();

void main()
{
    int x;
    char y, z;
    printf("Programmer-defined Function Type 2\n");
    x = motivQuote();
    printf("Returned value is %d\n\n", x);
    printf("Expected grades this semester:\n");
    printf("Subject1: ");
    y = grade();
    printf("\nReturned value is %c\n", y);
}

int motivQuote()
{
    int n = 0;
    printf("Select quote (1 or 2):");
    scanf_s("%d", &n);
    if (n == 1)
        printf("\n~ Robert Collier ~\n\t\"Success is the sum of small efforts, repeated day in and day out.\"\n");
    if (n == 2)
        printf("\n~ David Bly ~\n\t\"Striving for success without hard work\n\tis like trying to harvest where you haven’t planted.\"\n");
    return n + 2;
}
char grade()
{
    char grade = 0;
    scanf_s("%c", &grade, 1);
    return grade;
}

And here in the second case, grade() prompts for the input only in the second call, but the first one is not working:

 #include <stdio.h>
    int motivQuote();
    char grade();
    
    void main()
    {
        int x;
        char y, z;
        printf("Programmer-defined Functions\n");
        x= motivQuote();
        printf("Returned value is %d\n\n", x);
        printf("Expected grades this semester:\n");
        printf("Subject1: ");
        y= grade();
        printf("\nSubject2: ");
        z= grade();
        printf("Returned values are %c and %c\n", y, z);
    }
    
    int motivQuote ()
    {
        int n = 0;
        printf("Select quote (1 or 2):");
        scanf_s("%d", &n);
        if (n==1)
            printf("\n~ Robert Collier ~\n\t\"Success is the sum of small efforts, repeated day in and day out.\"\n");
        if (n==2)
            printf("\n~ David Bly ~\n\t\"Striving for success without hard work\n\tis like trying to harvest where you haven’t planted.\"\n");
        return n+2;
    }
    char grade()
    {
        char grade = 0;
        scanf_s("%c", &grade,1);
        return grade;
    }

Can anyone tell me what's the reason please?

ADAM78
  • 35
  • 4

1 Answers1

1

Use scanf(" %c",&grade,1); with a space before %c. This will solve the problem.

zoldxk
  • 2,632
  • 1
  • 7
  • 29