Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C: Hi, my code doesn't display the error message properly

I wrote a code to create a glossary of the most basic C instructions, their function and some definition (explained in a very simplistic way).

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

int main(void)
{
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(void)
{
        char dv = '"';
        char choice;
        int lsel;
        printf("%s", "C GLOSSARY\nInstructions' syntax list, their function and definitions\n\n----------------------------------------------\n");
        printf("%s", "\nType:\n- 'L' to display the list of instructions currently \n  covered in this glossary.");
        printf("%s", "\n- 'E' to exit the program\n");
        do
        {   
            scanf("%c", &choice);
            choice = toupper(choice);
            if(choice != 'L')
                printf("ERROR: invalid option. Please, try again: ");
        } while(choice != 'L');
        if(choice == 'E')
            return 0; //the code continues...
}


        

It works, but the message ERROR: invalid option. Please, try again: is displayed two times! Do you know why? If you want to try the code: https://github.com/AndreMarinelli/C-glossary

Thank you 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Change scanf("%c", &choice); to scanf(" %c", &choice);.

The space in the format specifier allows scanf() to consume any amount of leading whitespace in the input.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading