A lot of recipes are in grams, however heath nutrition is in ounces. This project was a request from a friend to remove that little hurdle. Let me know if you see anything that can be improved!
cpp
#include <stdio.h>
#include <ctype.h>
void otog(void), gtoo(void); // ounce to grams, grams to ounce
char menu(void);
int main(void) {
float grams;
int ounces;
char ch='\0';
while(toupper(ch)!='Q') {
if(ch!='\r') ch=menu();
else ch='\0';
printf("\n");
}
return 0;
}
void otog(void) {
float grams;
int ounces;
printf("Input amount in Ounces:");
scanf("%i",&ounces);
grams = ounces * 28.35;
printf("\n\nGrams: %.2f\n",grams);
}
void gtoo(void) {
float ounces;
int grams;
printf("Input amount in Grams:");
scanf("%i",&grams);
ounces = grams * 0.0353;
printf("\n\nOunces: %.8f\n",ounces);
}
char menu(void) {
int good=0;
char ch='\0',garbage;
printf("1.) Convert Ounces to Grams\n");
printf("2.) Convert Grams to Ounces\n");
printf("Q.) Quit\n");
printf("===========================\n");
printf(" Selection: ");
scanf("%c%c",&ch,&garbage);
/* Garbage will absorb the enter */
if(garbage!='\n') ch=garbage;
if(ch=='1') {
otog();
good=1;
ch='1';
}
if(ch=='2') {
gtoo();
good=1;
}
if(good==0) return('Q');
else return(ch);
}