Hello,
I have an assignment coming up in which we are supposed to input a value and then convert it into another value by multiplying it with a conversion factor/multiplier. The inputs are the units of the input value and converted value, the conversion factor, the value to start with, the increments and the amount of increments to generate. Once it has been generated, it needs to be displayed in a table like the one below.
Inches Centimetres| Inches Centimetres
1.00 2.5400000 | 2.00 5.0800000
3.00 7.6200000 | 4.00 10.1600000
5.00 12.7000000 | 6.00 15.2400000
7.00 17.7800000 | 8.00 20.3200000
9.00 22.8600000 | 10.00 25.4000000
Displaying this table seems pretty straightforward however we are told to try using recursive functions to try and display the converted values. I can't seem to do that part. Any ideas? I made part of the code but the actual value calculation using recursive functions seems confusing. Any ideas?
Code:
#include <cmath>
#include <iostream>
#include <string>
#include <iomanip>
#include <ctype.h>
using namespace std;
string originalUnit; //Name of original measurement unit
string convertedUnit; //Name of converted measurement unit
float convFactor = 0; //Conversion factor/multiplier
float startValue = 0; //Starting value of conversion table
float increment = 0; //Increment from one original measurement value to the next
int noOfvalues = 0; //Number of original measurement values to be generated
float x = 0;
float calculation(int, float, float) ;
int main()
{
cout << "Unit Conversion Table Generator\n\n";
for(;;)
{
do
{
cout << "Enter name of the original measurement unit (e.g. inches): ";
cin >> originalUnit;
if (isalpha(originalUnit[0]))
{
cout << originalUnit << "\n";
}
else
{
cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
}
} while (!isalpha(originalUnit[0]));
do
{
cout << "Enter name of the converted measurement unit (e.g. centimetres): ";
cin >> convertedUnit;
if (isalpha(convertedUnit[0]))
{
cout << convertedUnit << "\n";
}
else
{
cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
}
} while (!isalpha(convertedUnit[0]));
cout << "Enter conversion factor / multiplier (e.g. 2.54): ";
cin >> convFactor;
cout << "Enter the starting value of the conversion table for the original measurement unit: ";
cin >> startValue;
cout << "Enter the increment from one original measurement value to the next: ";
cin >> increment;
do
{
cout << "Enter the number of original measurement values to be generated: ";
cin >> noOfvalues;
if (noOfvalues > -1)
{
cout << noOfvalues << "\n\n";
}
else if (noOfvalues < 0)
{
cout << "ERROR: Value must be positive.\n";
}
} while (noOfvalues < 0);
cout << originalUnit << " to " << convertedUnit << " Unit Conversion Table\n\n";
cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "|";
cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "\n";
cout << " " << setw(7) << left << startValue << setw(15) << left << startValue*convFactor << "|";
cout << " " << setw(7) << left << startValue + increment << setw(15) << left << (startValue + increment)*convFactor << "\n";
calculation(noOfvalues, startValue, increment);
system("pause");
}
}
float calculation(int b /*noOfvalues*/, float a /*startValue*/, float c /*increment*/)
{
if (x <= b*a)
{
cout << "Value to convert: " << x << endl;
calculation(b, a, c + x);
}
return 0;
}#include <cmath>
#include <iostream>
#include <string>
#include <iomanip>
#include <ctype.h>
using namespace std;
string originalUnit; //Name of original measurement unit
string convertedUnit; //Name of converted measurement unit
float convFactor = 0; //Conversion factor/multiplier
float startValue = 0; //Starting value of conversion table
float increment = 0; //Increment from one original measurement value to the next
int noOfvalues = 0; //Number of original measurement values to be generated
float x = 0;
float calculation(int, float, float) ;
int main()
{
cout << "Unit Conversion Table Generator\n\n";
for(;;)
{
do
{
cout << "Enter name of the original measurement unit (e.g. inches): ";
cin >> originalUnit;
if (isalpha(originalUnit[0]))
{
cout << originalUnit << "\n";
}
else
{
cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
}
} while (!isalpha(originalUnit[0]));
do
{
cout << "Enter name of the converted measurement unit (e.g. centimetres): ";
cin >> convertedUnit;
if (isalpha(convertedUnit[0]))
{
cout << convertedUnit << "\n";
}
else
{
cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
}
} while (!isalpha(convertedUnit[0]));
cout << "Enter conversion factor / multiplier (e.g. 2.54): ";
cin >> convFactor;
cout << "Enter the starting value of the conversion table for the original measurement unit: ";
cin >> startValue;
cout << "Enter the increment from one original measurement value to the next: ";
cin >> increment;
do
{
cout << "Enter the number of original measurement values to be generated: ";
cin >> noOfvalues;
if (noOfvalues > -1)
{
cout << noOfvalues << "\n\n";
}
else if (noOfvalues < 0)
{
cout << "ERROR: Value must be positive.\n";
}
} while (noOfvalues < 0);
cout << originalUnit << " to " << convertedUnit << " Unit Conversion Table\n\n";
cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "|";
cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "\n";
cout << " " << setw(7) << left << startValue << setw(15) << left << startValue*convFactor << "|";
cout << " " << setw(7) << left << startValue + increment << setw(15) << left << (startValue + increment)*convFactor << "\n";
calculation(noOfvalues, startValue, increment);
system("pause");
}
}
float calculation(int b /*noOfvalues*/, float a /*startValue*/, float c /*increment*/)
{
if (x <= b*a)
{
cout << "Value to convert: " << x << endl;
calculation(b, a, c + x);
}
return 0; }