Code compiles and runs correctly, just need to have the 'total' keep adding to itself after the do you want to continue question. Right now it clears it everytime after it asks that question (Hopefully that makes sense)

#include <iostream>
using namespace std;
int main()

{
	double length =0;
	char operation;
	double total = 0;
	char answer = 'Y';

do 
{
cout << "Please enter a length: ";
	cin >> length;
cout << "(I)nches, (F)eet or (M)eters? ";
	cin >> operation;

	if (operation == 'M')
		{
		total = length ;
		}

	if (operation == 'F')
		{
		total = (length / 3.2808) ;
		}

	if (operation == 'I')
		{
		total = ((length / 12) * 3.2808);
		}
	
	
	
cout << "The current total length is " << total<< " meters." << endl;
cout << " Do you want to continue (Y/N)? ";
	cin >> answer;
	
}
	while (answer == 'Y');
	cout << "Goodbye!";
}

Dani AI

Generated

The reason the running total appears to "clear" is that each branch in the loop assigns the current conversion to total instead of adding the newly converted length to the running sum. was pointing in the right direction but included a small numeric typo (3.208) and the original inches formula in the first post is incorrect; ’s total += total would just double whatever you already had — not what you want.

A safe, clear approach is: convert the just-entered measurement to meters into a temporary variable, then add that temporary to the running total. Doing the conversion with explicit constants avoids confusion and makes the code easier to read and test:

const double METERS_PER_INCH = 0.0254;
const double METERS_PER_FOOT  = 0.3048;
double converted = 0.0;

/* map operation (case-insensitive) to meters */
switch (tolower(operation)) {
  case 'm': converted = length; break;
  case 'f': converted = length * METERS_PER_FOOT; break;
  case 'i': converted = length * METERS_PER_INCH; break;
  default:  /* handle invalid choice */ break;
}

/* add this entry to the running total */
total += converted;

Troubleshooting tips: validate numeric input with if (!cin) and clear the stream if needed; handle unexpected operation characters (ask again or skip the entry); and format output with std::fixed and std::setprecision so accumulated floating-point rounding is readable. Final note on conversions: use 0.0254 m/inch and 0.3048 m/foot (or divide by ~3.28084 to go ft→m) — those are exact definitions and will keep accumulation accurate.

Recommended Answers

All 2 Replies

put
total += length,
total += length/3.208,
total += ((length / 12) * 3.2808);

in their respective places. Note D += A is shorthand for D = D + A
Dont forget to terminate the statements.

do 
{
   total += total;     //there is
cout << "Please enter a length: ";
	cin >> length;
cout << "(I)nches, (F)eet or (M)eters? ";
	cin >> operation;

	if (operation == 'M')
		{
		total = length ;
		}

	if (operation == 'F')
		{
		total = (length / 3.2808) ;
		}

	if (operation == 'I')
		{
		total = ((length / 12) * 3.2808);
		}
	
	
	
cout << "The current total length is " << total<< " meters." << endl;
cout << " Do you want to continue (Y/N)? ";
	cin >> answer;
	
}
	while (answer == 'Y');
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.