-2

I am making a simple program to calculate the required exam mark based on your current mark, the % total worth of the exam on the course, and your desired mark. I was having no problems with it until I wanted a simple while loop to redo the proses if the user wanted to do so. The problem that arose was that when I have the scanf to get a char response from the user, it terminates the while loop and then the program. When I comment out the scanf line and leave the code as is (an infinite while loop) it works properly looping forever but with it, it will terminate with no errors. Any thoughts?

    #include <stdio.h>

    #define TRUE 1
    #define FALSE 0

    int main()
    {
        int continueProgram = TRUE;
        float currentMark = 0.0, examPercent = 0.0, desiredMark = 0.0, requiredMark = 0.0;
        char response  = 'y';

        while (continueProgram == TRUE)
        {
            printf("What is your current mark in your class: ");
            scanf("%f", &currentMark);
            printf("How much is you exam worth: ");
            scanf("%f", &examPercent);
            examPercent /= 100.0;
            printf("What is your desiered mark: ");
            scanf("%f", &desiredMark);

            // Simple math to get mark
            requiredMark = (desiredMark - currentMark * examPercent) / examPercent;

            if (requiredMark > 100.0)
            {
                printf("\nI'm sorry but you need above 100%% to get to your required mark\nHere is a list of all the marks you can get:\n\n");

                for (requiredMark = 0.0; requiredMark <= 100.0; requiredMark += 5.0)
                {
                    desiredMark = currentMark * examPercent + requiredMark * examPercent;
                    printf("Exam Mark: %.2f, Final Mark: %.2f\n", requiredMark, desiredMark);
                }
            }
            else
            {
                printf("\nThe mark that you need to get on the exam is: %.2f\nHere is a list of all the marks you can get:\n\n", requiredMark);

                for (requiredMark = 0.0; requiredMark <= 100.0; requiredMark += 5.0)
                {
                    desiredMark = currentMark * examPercent + requiredMark * examPercent;
                    printf("Exam Mark: %.2f, Final Mark: %.2f\n", requiredMark, desiredMark);
                }
            }

            printf("\nDid you want to continue with a new grade (y or n): ");

            // THIS IS WHERE I AM HAVING THE ISSUE
            scanf("%c", &response );

            if (response  == 'y')
            {
                printf("\n\nResetting  terminal...\n\n");
                currentMark = 0.0;
                examPercent = 0.0;
                desiredMark = 0.0;
                requiredMark = 0.0;
            }
            else
                continueProgram = FALSE;
        }
    }
  • Have you tried using the debugger? Also, maybe a spell-checker? – Mark Benningfield Dec 08 '17 at 02:36
  • Haha yes, a spell checker would be nice but I only care about the answer at the moment! My debugger is not working currently, unfortunately. – theGreatOne Dec 08 '17 at 02:45
  • The daily `scanf("%c", ....` problem. – chux - Reinstate Monica Dec 08 '17 at 03:39
  • @theGreatOne 'My debugger is not working currently' - you cannot write programs and/or develop software. You dont have the correct tools available. You should stop trying to write code until you get a working debugger. – Martin James Dec 08 '17 at 04:51
  • @MartinJames You sure know how to nurture someone new to a language/IDE! I thank you for your assistance through my perils! – theGreatOne Dec 09 '17 at 01:33
  • a `float` literal contains a trailing `f`. Without the trailing `f`, the literal is a `double` Suggest correct all the `double` literals to `float` by appending the missing `f` – user3629249 Dec 09 '17 at 09:17
  • regarding: `#define TRUE 1` and `#define FALSE 0` strongly suggest including the header file: `stdbool.h` and using `true` and `false` that are defined in that header file. – user3629249 Dec 09 '17 at 09:18
  • when calling any of the `scanf()` family of functions: always check the returned value (not the parameter values) to assure the operation was successful. In the posted code, the correct returned value is `1` and anything else is an error. Suggest using something similar to `fprintf( stderr, "scanf for ... failed")' followed by `exit( EXIT_FAILURE );` – user3629249 Dec 09 '17 at 09:21
  • suggest changing `desiered` to `desired` before your students see this. – user3629249 Dec 09 '17 at 09:22
  • what happens, when the user enters nothing but a newline? what happens when the user enters a `Y` – user3629249 Dec 09 '17 at 09:29
  • why tell the user that the code is resetting the main program variables via: `printf("\n\nResetting terminal...\n\n");` That is something that is of no interest to the user. – user3629249 Dec 09 '17 at 09:30
  • @user3629249 1st comment: I will thank you! 2nd: Is there a difference between the functionality of the stdbool.h header file or is it just considered to be a better and more accepted method of using bool values in c? 3rd: I will implement that, thank you! 4th: Yes I just downloaded a spellchecker to my IDE as spelling is not my strong suit. 5th: I later implemented an error checking function to verify that the input is actually correct. 6th: This is actually 100% just for fun! There is no functionality there I just wanted to have that in! – theGreatOne Dec 09 '17 at 17:15
  • @user3629249 This code was really just a quick and dirty version. I am cleaning up a lot of the code before I would even consider letting anyone use this as it is still very flawed and not safe from crashing. Also, I forgot to answer one of your questions in the last comment but I am using `tolower()` as a safeguard for capitals unless you know of a better method to check this. – theGreatOne Dec 09 '17 at 17:19

5 Answers5

1

The problem that arose was that when I have the scanf to get a char response from the user, it terminates the while loop and then the program.

This is happening because the scanf() is reading the stray \n (newline character) from the input buffer.

To resolve this, add a space before % character in scanf() like this:

scanf(" %c", &responce);

This will skip the leading whitespace characters (including newline character) and read the input given by the user.

H.S.
  • 11,654
  • 2
  • 15
  • 32
0

before this

scanf("%c", &response );

need to clear out stdin, perhaps by:

int ch;
while( (ch = getchar()) != EOF && '\n' != ch );

because the prior call to scanf() left the newline char in the input stream.

user3629249
  • 16,402
  • 1
  • 16
  • 17
-1

The reason you face this has to do with leading whitespaces

Change your scanf line to this:

scanf(" %c", &responce);

Here's a link for the explanation

How to do scanf for single char in C

Suhaib Ahmad
  • 487
  • 6
  • 25
-1

Try this, which works as you expected.

Original :

scanf("%c", &responce);

Replace:

responce=fgetc(stdin);

ntshetty
  • 1,293
  • 9
  • 20
-1

Personally I like to use fflush while under Windows

fflush(stdin);               //-- clear the input buffer
scanf("%c", &response );

or fseek under Linux environments.

fseek(stdin,0,SEEK_END);    //-- prepare the input buffer to allow fresh data from the keyboard
scanf("%c", &response );
dmaelect
  • 151
  • 2
  • 14