Created
June 11, 2017 20:25
-
-
Save sagarpanchal/89033557e187ee7e29fdc71c2003a5d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// Functions | |
// Clear Screen | |
void reset(int a) | |
{ | |
system("cls"); | |
if (a > 0) printf("\n\n"); | |
} | |
// Wait for enter and clear buffer if there is something left | |
void get_n() | |
{ | |
char chr; | |
while ((chr = getchar()) != '\n'); | |
} | |
// Quit | |
void quit() | |
{ | |
printf("\n\tPress Enter to Exit..."); | |
get_n(); | |
reset(0); | |
exit(0); | |
} | |
int main () | |
{ | |
int ch; | |
float cl, fh; | |
char temp, ans; | |
reset(1); | |
printf("\tProgram to convert celcius to fahrenheit and vice versa.\n"); | |
printf("\n\tPress Enter to Continue..."); | |
get_n(); | |
start: ; | |
ch = 0; cl = '\0'; fh = '\0'; temp = '\0'; ans = '\0'; | |
reset(1); | |
printf("\tTemperature Converter\n"); | |
printf("\n\t----------- ---------\n"); | |
printf("\n\t[1]: Celcius to Fahrenheit."); | |
printf("\n\t[2]: Fahrenheit to Celcius."); | |
printf("\n\t[X]: Quit"); | |
printf("\n\n\t > "); | |
if(scanf(" %d%c", &ch, &temp) == 2 && temp == '\n') { | |
switch (ch) { | |
case 1: | |
celcius: ; | |
reset(1); | |
printf("\n\tEnter temperature in Celsius: "); | |
if(scanf(" %f%c", &cl, &temp) == 2 && temp == '\n') { | |
fh = (cl * 1.8) + 32; | |
printf("\n\tTemperature in Fahrenheit: %.2f", fh); | |
printf("\n\n\tPress Enter to Repeat..."); | |
get_n(); | |
goto start; | |
} else { | |
get_n(); | |
printf("\n\tWrong Format! Try Again? [Y/n]: "); | |
if (scanf(" %c", &ans) == 1 && (ans == 'Y' || ans == 'y')) { | |
goto celcius; | |
} else { | |
get_n(); | |
goto start; | |
} | |
get_n(); | |
} | |
break; | |
case 2: | |
fahrenheit: ; | |
reset(1); | |
printf("\n\tEnter temperature in Fahrenheit: "); | |
if(scanf(" %f%c", &fh, &temp) == 2 && temp == '\n') { | |
cl= (fh - 32) / 1.8; | |
printf("\n\tTemperature in Celsius: %.2f",cl); | |
printf("\n\n\tPress Enter to Repeat..."); | |
get_n(); | |
goto start; | |
} else { | |
get_n(); | |
printf("\n\tWrong Format! Try Again? [Y/n]: "); | |
if (scanf(" %c", &ans) == 1 && (ans == 'Y' || ans == 'y')) { | |
goto fahrenheit; | |
} else { | |
get_n(); | |
goto start; | |
} | |
get_n(); | |
} | |
break; | |
default: | |
quit(); | |
break; | |
} | |
} else { | |
get_n(); | |
quit(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment