Welcome to Dream.In.Code
Getting Help is Easy!

Join 135,917 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,587 people online right now. Registration is fast and FREE... Join Now!




Ounces to Grams, Grams to Ounces

 
Reply to this topicStart new topic

Ounces to Grams, Grams to Ounces, Universal Conversion

no2pencil
6 May, 2008 - 09:40 PM
Post #1

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,433



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
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);
}

User is offlineProfile CardPM
+Quote Post

mikeblas
RE: Ounces To Grams, Grams To Ounces
23 May, 2008 - 06:55 AM
Post #2

D.I.C Head
**

Joined: 8 Feb, 2008
Posts: 155



Thanked: 1 times
My Contributions
It only takes integers as an input. What if I want to convert a real quantity, like 3.5 ounces, to grams?
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Ounces To Grams, Grams To Ounces
23 May, 2008 - 01:25 PM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,015



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
One thing I see is you're using different values for each side of the conversion. You want to pick one side and stick with it. Also, make the basic conversion function separate from all UI logic.

e.g.
cpp

#define G_FOR_OZ 28.349

float ouncesToGrams(float ounces) { return ounces * G_FOR_OZ; }
float gramsToOunces(float grams) { return grams / G_FOR_OZ; }

printf("Input amount in Grams:");
scanf("%f",&grams);
printf("\n\nOunces: %.8f\n", gramsToOunces(grams) );


User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Ounces To Grams, Grams To Ounces
30 May, 2008 - 01:39 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,433



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
That's slick...!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:00AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month