954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with this conversion program

Hello, I need to create a program that will read in a weight in kilograms and grams and will output the equivalent weight in pounds and ounces. I need to use 3 functions: for input, calculation, and output. I also need to have a loop that will let the user repeat this computation for new values, which will let the user whether they want to compute again. As you know there are 2.2046 pounds in a kilogram, 1,000 grams in a kilogram, and 16 ounces in a pound. If anyone is willing to assist me on creating a layout or to help to get me started for this program I will appreciate it. Thanks.

-Kama

kama
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

this is what I have so far with the program. I am struggling on how to create these functions, PLease help. As you know the stuff you see with the "//" is just the description of the parts.

//directives
#include <iostream>
using namespace std;

//function prototypes
void input(int& kilograms,
              int& grams);

void calculate;

void output;

//main function
int main ()

{
   int kilograms;
   int grams;
   int pounds;
   int ounces

   //begin main function executables
   void input(int& kilograms,
              int& grams);

   void calculate;

   void output;
kama
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Just looking at this, there seems to be very little you've actually done. I don't program that much, if any, and I probably could have framed out what you've got there, no offense.

What part are you stuck on? I wouldn't write any code for you, but wouldn't you first want to start by looking for a formula to convert grams to ounces, or something similar, and integrating that into your calculate function? The functions for input and output should be fairly basic, too-- your textbook should clearly outline how to deal with user input and output, as that's something EVERY program should do.

I mean, if you know 1 kilo is = 1000 grams, 1 lb is 16 oz, and 1 kilo is = 2.20046 lbs, you can figure it all out:

1 kilo = 35.20736 ounces

so

1 gram = 0.03520736 ounces

There you go right there. Just being me, and I'm sure someone will come with a better way if I'm wrong, I'd get the input in kilos and grams. Then, convert the kilos to grams. Convert the total grams to ounces, and then work out how many pounds and ounces are in that amount(use a modulus for that? I dunno...)

Disclaimer: I'm not a programmer, but that's what I'd think to do logically. Take someone else's advice before mine, or use my advice only in the event of a lack of more suitable advice. ;)

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

in fact, he's done nothing at all beyond declaring some variables and function prototypes (which were probably just copied from the assignment paper).

Do your own homework kama, it's the only way you'll ever learn anything.

Questions like yours make me believe there should be a minimum requirement of a college degree to be allowed onto the internet...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

here`s the code to convert weight in kg into pounds

#include
#include
#include

struct weight
{
int w1,w2;
};
weight take() //function ti take input
{
weight wt1;
cout<<"Please enter kg part:";
cin>>wt1.w1;
cout<<"\nplease enter the gram part:";
cin>>wt1.w2;
return wt1;
}

weight calculate(weight wt1)
{
weight wt;
int grams = wt1.w1*1000 + wt1.w2;
float totalounces = grams*0.0352736;
int pounds = totalounces/16;
int ounces = totalounces - (16*pounds);
wt.w1 = pounds;
wt.w2 = ounces;
return wt;
}

void display(weight wt)
{
cout<<"The weight is "<>ans;

}
getch();
}

varunrathi
Light Poster
41 posts since Aug 2004
Reputation Points: 10
Solved Threads: 1
 
#include<iostream.h>

Don't use that. It's 10 years out of date (I know, the oldest book that lists the proper way is about 10 years old).

void main()

Don't use, it's an abomination.

And lastly, don't do peoples' homework for them. It teaches them not to think and to be lazy and never learn the craft.
I don't want lazy poorly trained people as my future colleagues, it means I'll be doing more overtime doing their work and correcting their mistakes while they sit at home watching Wheel Of Fortune...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

ok this is what I did. I just made myself a pseudocode (basically the layout of the program) if you know what it is. Tell me if this is a the right idea.

#include
using namespace std;


void input;

void calculate;

void output;

//main function
int main ()
{

input()

}


int input()
{
int pounds;
int ounces;

//get pounds from user
pounds = whatever the user entered for pounds

//get ounces from user
ounces = whatever user entered for ounces

//call the calculate function with the pounds and ounces
calculate(pounds, ounces)

}

int calculate(int pounds, int ounces)
{
int kilograms;
int grams;

kilograms = pounds / 2.2046


output(kilograms, grams)

}

int output(int kilograms, int grams)
{

cout kilograms

count grams

}

kama
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

For pseudocode it looks like you're on the right track. [For actual code, there are a number of syntactical issues.]

But what are you doing with ounces? You ask for it, but never use it. Similarly, are you planning to do something with grams?

Please learn to use [CODE][/CODE] tags.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

hi well first of let me see if got ur question right.
u need to input a weight value in kilograms and in grams separately and give the corresponding output in pounds and ounces right. well if so just 4 get the loop 4 the time being. i would suggest that u concentrate on input process and output. this is wat i think should be done

#include
#include
#include
#define pound 2.0246;
void main()
{
int kilo,gram,pound=1,ounce=1;
int answerp,answero;

// inputing data
cout<<" Please enter the weight in kilos: ";
ciin>>kilo;
cout<<" Please enter the grams: ";
cin>>gram;

// convert kilo and gram into pound and ounces
answerp=kilo*pound;
answero=( gram*16*pound)/1000;
// this will compute data separately for the kilo part and gram part
// to display them saparately
cout<

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

ok this is where I am now.

#include <iostream>
using namespace std;


void InputWeight(int kilograms,
				 int grams);

void CalculateWeight(int kilograms,
					 int grams,
					 int pounds,
					 int ounces);

void OutputWeight(int pounds,
				  int ounces);

/***************************Main Function**********************************/
int main()
{

	int grams;
	int kilograms;
	int pounds;
	int ounces;
	char userChoice;

	do
	{


	InputWeight(kilograms,grams);

	CalculateWeight(kilograms,grams,pounds,ounces);

	OutputWeight(pounds,ounces);

	cout <<"Calculate another weight? (Y/N):  ";
	cin >> userChoice;
	
	}
	while ((userChoice == 'Y') || (userChoice == 'y'));


	return 0;
}
//call the input function
void InputWeight(int kilograms,
				 int grams)
{

	cout <<"Enter your weight for Kilograms: ";
	cin >> kilograms;
	cout<<"Enter your weight for Grams: ";
	cin >> grams;
} 

//call the calculate function with the pounds and ounces
void CalculateWeight(int kilograms,
					 int grams,
					 int pounds,
					 int ounces)
{
	pounds=kilograms*pounds;
	ounces=(grams*16*pounds)/1000;
}

//call the output function 
void OutputWeight(int pounds,
				  int ounces)
{
 
cout <<"Your weight conversion to pounds is: "<<pounds<<endl;
cout <<"Your weight conversion to ounces is: "<<ounces<<endl;

 
}


I still am confused of what calculations I need to use to convert the weights.......

kama
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

When you pass parameters to a function and you want to modify those parameters within that function, you'll need to pass the parameter by reference or by using a pointer. Otherwise the value will not be changed in the calling routine.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

varunrathi:

Don't post whole solutions for people on the forum. We're not here to hand-hold, we're here to help people learn.

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

well i guess i know wat ur problem is . actually please dont take offence u urself dont know what u should convert into wat in ur program. i would suggest that u get 1 thing clear. the first step 2 programming is program definition. understand what ur program should do. answer these few questions
1. do u really need 2 input the kilo and gram values separately?
2. cant the user just input a value in kilograms only?
3. the kilogram value do u want it 2 be converted to pounds only or ounces only or both and display correspounding values?

to get it clear i need these answers. :D :lol: :) :rolleyes: :cool: :confused:

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

sorry jwenting,Dev Sinkula and all for breaking the rules. actually iam new to this site . i would not send the whole solution in future .sorry again.

varunrathi
Light Poster
41 posts since Aug 2004
Reputation Points: 10
Solved Threads: 1
 

I would like to add my 2 cents worth this guy should use a text book becuase this is a very basic 1st year program. Although I may not be a boff at programming, it sounds like this guy wanted the easy way out . i don't want to insult the person or hurt his feelings but this type of programming can be found in any basic level programming book. Please Kama next time use a text book if you can otherwise ask for some tutorials from the people on this site and I am sure they are willing to help you.
I have some great notes on c++ if you like i can try and e-mail them to you. Here is my e-mail [email]pietro.mariani@absamail.co.za[/email] and send me your e-mail address

Pietro
Newbie Poster
5 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

well kama i am still waiting for those answers. in fact i would suggest that you use a good c++ book of balaguruwsamy. i dont know if it is available in your country or if there do exits much excellent books but still do take a look at the books. usually they do contain gun notes and examples. ur program is not that difficult to write but i can only help you and cannot do your work for you. ok best of luck

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You