[UPDATED] Logical error happened.

I am a beginner and i'm quite confuse to produce the output as the image link below, especially the loop (although the output of the following code is the same as image). Please do guide me.
image link: http://s21.postimg.org/tcpq0uchj/image.jpg

*Also, is these code able to do recursion?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iomanip>
#include <string>
#include <iostream>

using namespace std;

int main()
{
	double num, cNum, rNum;
	int y, row;
	string myString;

	cout << "Conversion Table Generator" << endl << endl;
	cout << "Name of the original measurement unit (e.g. inches): ";
	cin >> myString;
	cout << "Name of the converted measurement unit (e.g. centimetres): ";
	cin >> myString;
	cout << "Conversion factor / multiplier (e.g. 2.54): ";
	cin >> cNum;
	cout << "Starting value of the conversion table for the original measurement unit: ";
	cin >> num;
	cout << "Enter the increment / row size for the original measurement unit: ";
	cin >> rNum;
	cout << "Enter the number of rows to be generated: ";
	cin >> row;
	
	cout << endl << endl << "Inches to Centimetres Conversion Table" << endl << endl;
	cout << "Inches		Centimetres" << endl;

	for (y = 0; y < row; y++)
	{
		for (num; num < 10; num += rNum)
		{
			cout << " " << setprecision(2) << fixed << num << "		" << setprecision(7) << fixed << num * cNum << endl;
		}
	}
	
	system("pause");
	return 0;
}
Last edited on
@jackiettw

Here is your program. I changed the loop from int to a double, otherwise, you would have problems if the increments were not whole numbers, or if the starting measurement was not either.

And yes, you could do the conversions using recursion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iomanip>
#include <string>
#include <iostream>

using namespace std;

int main()
{
	double num, cNum, rNum,start;
	int row;
	string original, converted;

	cout << "Conversion Table Generator" << endl << endl;
	cout << "Name of the original measurement unit (e.g. inches): ";
	cin >> original;
	cout << "Name of the converted measurement unit (e.g. centimetres): ";
	cin >> converted;
	cout << "Conversion factor / multiplier (e.g. 2.54): ";
	cin >> cNum;
	cout << "Starting value of the conversion table for the original measurement unit: ";
	cin >> num;
	cout << "Enter the increment / row size for the original measurement unit: ";
	cin >> rNum;
	cout << "Enter the number of rows to be generated: ";
	cin >> row;

	cout << "    " << original << " to " << converted << " Conversion Table" << endl << endl;

	cout.width(17);
	cout << original << "   " << converted << endl << endl;
	for (start = num; start < num + (row*rNum); start += rNum)
	{
		cout.width(15);
		cout << setprecision(2) << fixed << start << "		" << setprecision(4) << fixed << start * cNum << endl;
	}

	system("pause");
	return 0;
}
@whitenite1

Thank you so much Mr. White. Main problem fixed. I will try the recursion later and i will post back here again if i meet problem in recursion. Appreciate for your help. :)
Topic archived. No new replies allowed.