mercredi 27 mars 2019

How to fix - unexpected output using getchar() and do-while

I am trying to make a simple code that will read a char from input and execute "Correct" or "Incorrect input" and run the code again until the correct input is entered. First of all it does not work for capital X. The other issue that I want to fix is that after the incorrect input I have to press enter to get the "Enter x" message, instead of getting in immediately after the incorrect input message. Thanks in a advance. I am a beginner.

#include <stdio.h>
int main()
{
         do
         {
            printf("Enter x\n");
            if (getchar()=='x'|| getchar()=='X')
            {
            printf("Entered char is X\n");
            return 0;
            }
            else
            {
            printf("Input incorrect! Please try again!!!\n");
            }
         }
         while (getchar()!='x' || getchar()!='X');
     return 0;
 }

Aucun commentaire:

Enregistrer un commentaire