Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Language C++ I need help, if i put 1 ft 2 inch i convert to 14 inch

ID: 3863524 • Letter: P

Question

Programming Language C++

I need help, if i put

1 ft 2 inch

i convert to 14 inch then multiply by 2.54

My ouput returns 0.0,

I bolded, where the changes need to happen to make this work. Not sure if i have those correct or not.

TCHAR * ConvertToCentimeters::Convert(TCHAR * szInput)
{
   double feet = 1.0;                                           // converted return value
   double inches = 0.0;
   double tInches;
   double centi;

   feet = Parse(szInput);                                       // parse user input

   (tInches = (feet * 12) + inches);
   centi = tInches * 2.54;
  
                                                               // copy value to output TCHAR
   _stprintf_s(szReturn, TEXT("%.*f"), 2, centi);               // display output

   return szReturn; ;
}

double ConvertToCentimeters::Parse(TCHAR * szInput)
{
   double feet = 0.0;                                           // parsed value from user input
   double inches = 0.0;                                       // parsed value from user input
   double tInches = 0.0;

   double centi = tInches;
   string strInput;                                           // user input converted to string
   string strWork;                                               // parsed input
   size_t pReturnValue;                                       // return value for msbstows_s()

   try
   {
       if (lstrlen(szInput) == 0)                               // don't allow 0 length input
           throw 0;

       // convert to upper case
       for (int i = 0; i < lstrlen(szInput); i++)
           szInput[i] = _totupper(szInput[i]);

       // convert TCHAR To String
       wstring temp(szInput);
       string str(temp.begin(), temp.end());
       strInput = str;

       // get feet
       int f = strInput.find("F");                               // finds the letter f
       if (f == string::npos)                                   // if no f in string
           throw badChars();                                   // throw expecption

       strWork = strInput.substr(0, f);                       // grabs all chars up to letter g

                                                               // convert work to TCHAR
       mbstowcs_s(
           &pReturnValue,
           szReturn,
           100,
           strWork.c_str(),
           100);

       // convert work string to double
       feet = _tstof(szReturn);

       // get oz
       int i = strInput.find("I");
       if (i != string::npos)                                   // convert if ther is an i
       {
           while (strInput.at(f + 1) != ' ')
               f++;

           strWork = strInput.substr(f + 1, 0 - (f + 1));       // grab chars after feet to get i

                                                               // convert work to TCHAR
           mbstowcs_s(
               &pReturnValue,
               szReturn,
               100,
               strWork.c_str(),
               100);

           // convert work to double string
           inches = _tstof(szReturn);
           if (inches >= 12.0)
               throw tooManyInches();
       }
       return centi;                       
   }
   catch (int)
   {
       MessageBox(NULL, TEXT("Invalid Entry"), TEXT("Exception"), NULL);
       return 0.00;
   }
   catch (badChars)
   {
       MessageBox(NULL, TEXT("Missing Feet or Inches Indicator"), TEXT("Exception"), NULL);
       return 0.00;
   }
   catch (tooManyInches)
   {
       MessageBox(NULL, TEXT("Inches must be equal to 12 or less"), TEXT("Exception"), NULL);
       return 0.00;
   }
}

Explanation / Answer

Dear Friend

You are getting error due because you have write down * before f. I bolded those line

_stprintf_s(szReturn, TEXT("%.*f"), 2, centi);               // display output

Do changes with *, It will work.

Still you have any error please send me complete code.

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.