Newbie question

S

Srdja123

Im doing some C++ exercises from the Primer plus book by Prata. So
anyway, the assignment is to convert meters and centimeters to inches
and foot and so on.

Code:
#include <iostream>

int meterTillCm(int);
double cmTillTum(double);


int main()
{
	using namespace std;

	cout << "Type in your length in m and cm \n";

	cout << "Meter: ";
	int meter;
	cin >> meter;

	int meterIcm;
	meterIcm = meterTillCm(meter);


	cout << "Centimeter: ";
	int cm;
	cin >> cm;

	int alltIcm;
		alltIcm = meterIcm + cm;
		cout << "You are: " << alltIcm << " cm tall\n";

	double tum;
	tum = cmTillTum(alltIcm);

	cout << "You are: " << tum << " inch tall\n";




	double fot;
	double tum2;
	const int inch_per_foot = 12;
	fot = tum / inch_per_foot;
	tum2 = tum % inch_per_foot);

	 cout << "Foot: " << fot << " and " << tum2 << " inch.";




	cin.get();
	cin.get();
	return 0;
}


int meterTillCm(int n)
{
	return n *100;
}

double cmTillTum(double n)
{
	return n / 2.54;
}

I just dont know how to make the variable "tum" to show feet and
inches. I know that I can use cmath and fmod(a, b), and then use
double as a type, but is there any other way to the solution?

All help is appreciated!
 
D

Dennis Jones

Im doing some C++ exercises from the Primer plus book by Prata. So
anyway, the assignment is to convert meters and centimeters to inches
and foot and so on.

Code:
#include <iostream>

int meterTillCm(int);
double cmTillTum(double);


int main()
{
using namespace std;

cout << "Type in your length in m and cm \n";[/QUOTE]

Don't you mean, "height"?!?!  [Sorry, I couldn't resist!]  :-)

[QUOTE]
I just dont know how to make the variable "tum" to show feet and
inches. I know that I can use cmath and fmod(a, b), and then use
double as a type, but is there any other way to the solution?[/QUOTE]

It looks like you already have the solution:

const int inch_per_foot = 12;
fot = tum / inch_per_foot;
tum2 = tum % inch_per_foot;

So, yuou are going to have to be more specific.  What *exactly* are you 
having trouble with?

- Dennis
 
S

Srdja123

Im doing some C++ exercises from the Primer plus book by Prata. So
anyway, the assignment is to convert meters and centimeters to inches
and foot and so on.
Code:
#include <iostream>[/QUOTE]
[QUOTE]
int meterTillCm(int);
double cmTillTum(double);[/QUOTE]
[QUOTE]
int main()
{
using namespace std;[/QUOTE]
[QUOTE]
cout << "Type in your length in m and cm \n";[/QUOTE]

Don't you mean, "height"?!?!  [Sorry, I couldn't resist!]  :-)
[QUOTE]
I just dont know how to make the variable "tum" to show feet and
inches. I know that I can use cmath and fmod(a, b), and then use
double as a type, but is there any other way to the solution?[/QUOTE]

It looks like you already have the solution:

const int inch_per_foot = 12;
fot = tum / inch_per_foot;
tum2 = tum % inch_per_foot;

So, yuou are going to have to be more specific.  What *exactly* are you
having trouble with?

- Dennis[/QUOTE]

Well, I cant use the modulus % with the double type :<
 
D

Daniel T.

Im doing some C++ exercises from the Primer plus book by Prata. So
anyway, the assignment is to convert meters and centimeters to inches
and foot and so on.

Code:
#include <iostream>

int meterTillCm(int);
double cmTillTum(double);


int main()
{
	using namespace std;

	cout << "Type in your length in m and cm \n";

	cout << "Meter: ";
	int meter;
	cin >> meter;

	int meterIcm;
	meterIcm = meterTillCm(meter);


	cout << "Centimeter: ";
	int cm;
	cin >> cm;

	int alltIcm;
		alltIcm = meterIcm + cm;
		cout << "You are: " << alltIcm << " cm tall\n";

	double tum;
	tum = cmTillTum(alltIcm);

	cout << "You are: " << tum << " inch tall\n";




	double fot;
	double tum2;
	const int inch_per_foot = 12;
	fot = tum / inch_per_foot;
	tum2 = tum % inch_per_foot);

	 cout << "Foot: " << fot << " and " << tum2 << " inch.";




	cin.get();
	cin.get();
	return 0;
}


int meterTillCm(int n)
{
	return n *100;
}

double cmTillTum(double n)
{
	return n / 2.54;
}

I just dont know how to make the variable "tum" to show feet and
inches. I know that I can use cmath and fmod(a, b), and then use
double as a type, but is there any other way to the solution?

All help is appreciated!

Change all your doubles to ints. In your cmTillTum function use the
following:

return (n / 2.54)+0.5;

I also suggest you work on reducing the number of variables in the
program.
 
D

Daniel T.

Thx!

Could you expound that? How do I do that?

For example, you have:

int meterIcm;
meterIcm = meterTillCm(meter);


cout << "Centimeter: ";
int cm;
cin >> cm;

int alltIcm;
alltIcm = meterIcm + cm;

How about this instead:

cout >> "Centimeter: ";
int cm = 0; // always initialized your variables
cin >> cm;
int alltIcm = meterTillCm(meter) + cm;

That gets rid of the "meterlcm" variable...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top