Hello guys. I just have a problem that I think you pros will help me solve real quick..

I was instructed to make a table that will show the inputed miles, yards, feet, and inches this way:

Units Value Meters
Miles XXX XXX.XX
Yards XXX XXX.XX
Feet XXX XXX.XX
Inches XXX XXX.XX
Total XXX XXX.XX

This is what I have, but I cant seem to do the calculations. I get error C2296 and C2297. I dont see whats wrong with it really.
Thanks in advance.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

	const int FEET_PER_MILE = 5280;
	const int YARDS_PER_MILE = 1760;
	const int FEET_PER_YARD = 3;
	const int INCHES_PER_YARD = 36;
	const float METERS_PER_INCH = 0.0254;
	double miles;
	double yards;
	double feet;
	double inches;
	double meters, meters2, meters3, meters4;
	double totalmeters;

	cout <<"Please enter the miles: \n";
	cin >>miles;

	cout <<"Please enter the yards: \n";
	cin >>yards;

	cout <<"Please enter the feet: \n";
	cin >>feet;

	cout <<"Please enter the inches: \n";
	cin >>inches;

	cout <<"\n\n";


	cout <<"Units\t\tValue\t\tMeters\n\n\n";
	cout <<"Miles\t\t"<<miles<<"\t\t"<<meters = miles*YARDS_PER_MILE*INCHES_PER_YARD*METERS_PER_INCH235<<"\n\n";
	cout <<"Yards\t\t"<<yards<<"\t\t"<<meters2 = yards*INCHES_PER_YARD*METERS_PER_INCH<<"\n\n";
	cout <<"Feet\t\t"<<feet<<"\t\t"<<meters3 = feet/FEET_PER_YARD*INCHES_PER_YARD*METERS_PER_INCH<<"\n\n";
	cout <<"Inches\t\t"<<inches<<"\t\t"<<meters4 = inches*METERS_PER_INCH<<"\n\n\n";
	cout <<"Total\t\t"<<"\t\t"<<totalmeters=meters+meters2+meters3+meters4;
	cout <<"\n\n";
	system ("pause");
}

Recommended Answers

All 4 Replies

The assignment operator doesn't play well with the insertion (<<) and extraction (>>) operators. Ideally you would break those expressions out and print the result:

meters = miles*YARDS_PER_MILE*INCHES_PER_YARD*METERS_PER_INCH;
meters2 = yards*INCHES_PER_YARD*METERS_PER_INCH;
meters3 = feet/FEET_PER_YARD*INCHES_PER_YARD*METERS_PER_INCH;
meters4 = inches*METERS_PER_INCH;
totalmeters=meters+meters2+meters3+meters4;
    
cout <<"Units\t\tValue\t\tMeters\n\n\n";
cout <<"Miles\t\t"<<miles<<"\t\t"<<meters<<"\n\n";
cout <<"Yards\t\t"<<yards<<"\t\t"<<meters2<<"\n\n";
cout <<"Feet\t\t"<<feet<<"\t\t"<<meters3<<"\n\n";
cout <<"Inches\t\t"<<inches<<"\t\t"<<meters4<<"\n\n\n";
cout <<"Total\t\t"<<"\t\t"<<totalmeters;

The simpler your individual statements are, the easier the code will be to read and understand. Alternatively you could remove the assignment entirely since you don't use the result object:

cout <<"Units\t\tValue\t\tMeters\n\n\n";
cout <<"Miles\t\t"<<miles<<"\t\t"<<miles*YARDS_PER_MILE*INCHES_PER_YARD*METERS_PER_INCH<<"\n\n";
cout <<"Yards\t\t"<<yards<<"\t\t"<<yards*INCHES_PER_YARD*METERS_PER_INCH<<"\n\n";
cout <<"Feet\t\t"<<feet<<"\t\t"<<feet/FEET_PER_YARD*INCHES_PER_YARD*METERS_PER_INCH<<"\n\n";
cout <<"Inches\t\t"<<inches<<"\t\t"<<inches*METERS_PER_INCH<<"\n\n\n";
cout <<"Total\t\t"<<"\t\t"<<meters+meters2+meters3+meters4;

Finally, you can correct the error by fixing the precedence with parentheses:

cout <<"Units\t\tValue\t\tMeters\n\n\n";
cout <<"Miles\t\t"<<miles<<"\t\t"<<(meters = miles*YARDS_PER_MILE*INCHES_PER_YARD*METERS_PER_INCH)<<"\n\n";
cout <<"Yards\t\t"<<yards<<"\t\t"<<(meters2 = yards*INCHES_PER_YARD*METERS_PER_INCH)<<"\n\n";
cout <<"Feet\t\t"<<feet<<"\t\t"<<(meters3 = feet/FEET_PER_YARD*INCHES_PER_YARD*METERS_PER_INCH)<<"\n\n";
cout <<"Inches\t\t"<<inches<<"\t\t"<<(meters4 = inches*METERS_PER_INCH)<<"\n\n\n";
cout <<"Total\t\t"<<"\t\t"<<(totalmeters=meters+meters2+meters3+meters4);
commented: Thanks again Narue +1

Like a boss ._. so easy but I couldnt find that out.. haha Thank you very much.. I just used the parentheses.. I'll never forget them anymore :D

I find it both humorous and disappointing that people usually choose the worst of my options. :icon_rolleyes:

Dont worry. I ended up putting the first one because it looks better :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.