-1

I am trying to make a program so that the teacher can type in a number and it will output the corresponding alphabetical grade in C. Here is what I have so far, I am trying to make the console program stay open until a teacher types in exit or clicks the X icon. I appreciate any help.

#include <stdio.h>
#include<stdlib.h>
#define exit -1

int main(void) {

    int grade;


    printf("Enter student grade: ");
    scanf_s("%d", &grade);
    printf("Students grade is: %d", grade);

    while (grade != exit) {

        if (grade < 65)
        {
            printf("\nStudent grade is: F\n");
        }
        else if (grade <= 69)
        {
            printf("\nStudent grade is: D\n");
        }
        else if (grade <= 79)
        {
            printf("\nStudent grade is: C\n");
        }
        else if (grade <= 89)
        {
            printf("\nStudent grade is: B\n");

        }
        else if (grade <= 100)
        {
            printf("\nStudent grade is: A\n");
        }
        else if (grade == exit) {
        #define EXIT_SUCCESS 0;
        }
        break;
    }
    printf("Enter student grade: ");
    scanf_s("%d", &grade);
    printf("Students grade is: %d", grade);
    return 0;
}
Parkofadown
  • 616
  • 7
  • 11
  • 8
    What does not work? Your input request is not inside the loop. – Osiris Nov 07 '18 at 23:16
  • 1
    `#define EXIT_SUCCESS 0;` what is this supposed to do? – wildplasser Nov 07 '18 at 23:48
  • Just put `getchar()` before the `return 0;` – lost_in_the_source Nov 07 '18 at 23:51
  • 1
    you say "until a teacher types in exit", but you have not prepared to accept "exit" as input -- you are only scanning for numerical input ... and not checking to see if the input was successfully parsed as numerical ... – landru27 Nov 07 '18 at 23:58
  • @landru27 exit is defined as -1. – ad3angel1s Nov 08 '18 at 00:00
  • `EXIT_SUCCESS` is already `#define`'d in ``; your line `#define EXIT_SUCCESS 0;` is not doing whatever you think it's doing, and the trailing `;` would almost certainly cause a compile-time error if you did try to use it in the way it looks like it is meant to be used (to mean 'zero') – landru27 Nov 08 '18 at 00:03
  • @ad3angel1s : right; so if the teacher types in `-1`, that will mean something, but there is nothing in this program that causes the string value `exit` to equate to the numerical value `-1` – landru27 Nov 08 '18 at 00:10
  • I keep getting LNK1120 and LNK2019 errors. VS 2015. – Parkofadown Nov 08 '18 at 00:20
  • How do you make exit as an input to exit the program? Wouldn't you have two different data types? – Parkofadown Nov 08 '18 at 00:20
  • 1
    there are a few approaches to coping with input that might be a string or might be a number; e.g., you could input a string, and then test to see if that string can be parsed as a number ... but I strongly feel you should just prompt the user with `Enter student grade or '-1' to exit` and focus on more fundamental matters, such as error checking your function call return values, putting the input logic in the right place to have it repeated, re-thinking what you were attempting with `EXIT_SUCCESS`, and other basic things – landru27 Nov 08 '18 at 00:29
  • when compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -Werror -pedantic -std=gnu11` ) Note: other compilers use different options to perform the same functionality. – user3629249 Nov 08 '18 at 01:35

2 Answers2

0

Figured it out, thanks for your help.

#include <stdio.h>
#include<stdlib.h>
#define exit -1

int main(void) {

    int grade;


    printf("Enter student grade , or '-1' to exit \n");
    scanf_s("%d", &grade);
    while (grade != exit) {
        printf("Students grade is: %d", grade);
        if (grade < 65)
        {
            printf("\nStudent grade is: F\n");
        }
        else if (grade <= 69)
        {
            printf("\nStudent grade is: D\n");
        }
        else if (grade <= 79)
        {
            printf("\nStudent grade is: C\n");
        }
        else if (grade <= 89)
        {
            printf("\nStudent grade is: B\n");

        }
        else if (grade <= 100)
        {
            printf("\nStudent grade is: A\n");
        }
        else if (grade == exit) {
            exit;
        }
        printf("Enter student grade , or '-1' to exit \n");
        scanf_s("%d", &grade);
    }
    fgetc(stdin);
    return 0;
}
Parkofadown
  • 616
  • 7
  • 11
-1

A few things before the answer: Please do some research, learn, and understand this code before you submit it. I'm not saying you will, but lots of students I went to school with would blindly copy and paste their homework and this only hurts you in the long run. It's not as complicated as you think, once you learn the logic it becomes way easier.

Some Resources that I recommend you study for this homework: Convert char array to a int number in C, https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm

If you want to be able to enter the word "exit" or an int in the same array, you need to write it to a char array. The idea is to convert the string to an int if it is a number. You can do This with something like sscanf or atoi.

If you were to type "exit" it is case sensitive. You can make all the cases lower or upper case using a for loop. In my case I take the user input and compare it to my array exit_Array. If it returns 0 ( It matches) it moves on to the next stage which is exiting the program.

If it is an int it moves on to phase 1, which is grading. Once it is graded it lowers the phase by 1 so it becomes 0 (enters the first loop again). It will do this over and over until the user enters "exit".

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

int main()
{
char input_Array[3];
char exit_Array[] = "exit";

int grade;
int phase = -1;

phase++;
while(phase == 0)
{
    printf("Enter student grade: ");
    scanf("%s", input_Array);

    // Converts all letters to lower case so it can be Exit, eXiT, etc.
    for(int i = 0; i <= strlen(input_Array); i++)
    {
        input_Array[i] = tolower(input_Array[i]);
    }

    // Exit Input
    if(strcmp(input_Array, exit_Array) == 0)
    {
        printf("Goodbye!");
        exit(0);
    }
    else
    {
        // Convert String to Number
        grade = atoi(input_Array);
    }
    phase++;

    while(phase == 1)
        {
            if (grade < 65)
                {
                    printf("Student grade is: F\n");
                }
            else if (grade <= 69)
                {
                    printf("Student grade is: D\n");
                }
            else if (grade <= 79)
                {
                    printf("Student grade is: C\n");
                }
            else if (grade <= 89)
                {
                    printf("Student grade is: B\n");
                }
            else if (grade <= 100)
                {
                    printf("Student grade is: A\n");
                }
            phase--;
        }
}

return 0;
}