C Program to Get User Input and Compare

How to write a C Program to Get User Input and Compare ?


Solution :

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

#define clear() printf("\e[1;1H\e[2J");

void How(char *name1);

int main(void){
char name[20];

clear();
printf("Hello.\n");
sleep(1);
clear();

printf("Please Enter Your Name: ");
fgets(name,sizeof(name),stdin);

clear();
int nn = strlen(name); //get length of name
name[nn - 1] = '\0'; //remove newline at end of name

How(name);
}

void How(char *name1){
        char ch;

        clear();
        printf("Hello %s, how are you?\n", name1);
        printf("\t1)Good\n\t2)Bad\n>");
        scanf(" %c", &ch );

        if(ch == '1'){
                printf("That is good to hear %s\n",name1);
                sleep(2);
        }
        else if(ch == '2'){
                printf("Sorry to hear that %s\n",name1);
                sleep(2);
        }
        else{
                printf("Sorry, that is not an option.\n");
                sleep(2);
        }
}

C Program Quadratic equation


Learn More :