mercredi 9 décembre 2020

Couldn't execute the last if else statement correctly

I'm Kev and still new to C programming. I'm currently playing and learning with coding. I created a dialogue code but my last if and else statement wasn't executed correctly. It straight execute "Sorry I don't understand." I've ran out of ideas. Is there anyone who can notice my mistakes? Thank you.

This is the code:

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

int main ()
{
char ans, your_ans [20], your_answer [10];
char ans1 = 'y', ans2 = 'n';
char ans3[]= "ok", ans4[] = "not ok";
printf("Hi. Welcome to C Programming.\n");
printf("Are you willing to learn C Programming?\n");
scanf(" %c", &ans);

if(ans!=ans1 && ans!=ans2){
    printf("Sorry, I don understand.\n");
    exit(0);
    }
    else if(ans==ans1){
    printf("That's great. I'm glad to hear that.\n");
    exit(0);
    }
    else{
        printf("Why?\n");
        scanf(" %c", &your_ans);
        scanf("%[^\n]", your_ans);
    }

    printf("You're doing fine. Keep it up!\n");
    scanf(" %c", &your_answer);

        if(your_answer!=ans3 && your_answer!=ans4){
        printf("Sorry, I don't understand.\n");
        exit(0);
        }
        else if(your_answer==ans3){
        printf("Nice. Don't worry. Programming is fun. You can do it!\n");
        exit(0);
        }
        else{
            printf("Stop worrying! You can do it! Have faith!\n");
        }

   return 0;
   }

Aucun commentaire:

Enregistrer un commentaire