Weight/Length/Currency Converter

Hey all,
Started learning C++ a few days ago, and just finished my first half-useful program. It's basically a converter for weight, distance and currency.
My question is, is there any better way to do this? Any general tips?
And for some reason, when I did the do { code } while, I got an error code about the compiler not recognising the system and return commands. Then, suddenly, it worked. Why?

The code:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
double pounds, kilograms, feet, metres, centimetres, inches, kilometres, miles, yards, EUR, USD, GBP, CNY;
using namespace std;
 
int choice;

int main()

{
	cout << "Weight: \n" << endl;
	cout << "Pounds      --> Kilograms:   type 1. " << endl;
	cout << "Kilograms   --> Pounds:      type 2. " << endl;
	cout << "\nDistance:\n" << endl;
	cout << "Feet        --> Metres:      type 3. " << endl;
	cout << "Metres      --> Feet:        type 4. " << endl;
	cout << "Inches      --> Centimetres: type 5. " << endl;
	cout << "Centimetres --> Inches:      type 6. " << endl;
	cout << "Miles       --> Kilometres:  type 7." << endl;
	cout << "Kilometres  --> Miles:       type 8." << endl;
	cout << "Yards       --> Metres:      type 9." << endl;
	cout << "Metres      --> Yards:       type 10." << endl;
	cout << "\nCurrency:\n" << endl;
	cout << "EUR --> USD: type 11." << endl;
	cout << "USD --> EUR: type 12." << endl;
	cout << "GBP --> USD: type 13." << endl;
	cout << "USD --> GBP: type 14." << endl;
	cout << "EUR --> GBP: type 15." << endl;
	cout << "GBP --> EUR: type 16." << endl;
	cout << "CNY --> EUR: type 17." << endl;
	cout << "EUR --> CNY: type 18." << endl;
	cout << "CNY --> USD: type 19." << endl;
	cout << "USD --> CNY: type 20.\n" << endl;

	do
	{
	
	cin >> choice;

    switch (choice)
	{
	case 1:
		cout << "\nEnter the number of pounds: ";
		cin >> pounds;
		cout << "\n" << pounds << " lbs is " << pounds * 0.45 << " kg." << endl;
		break;
	case 2:
		cout << "\nEnter the number of kilos: ";
		cin >> kilograms;
		cout << "\n" << kilograms << " kg is " << kilograms * 2.2 << " pounds." << endl;
		break;
	case 3:
		cout << "\nEnter the number of feet: ";
		cin >> feet;
		cout << "\n" << feet << " ft is " << feet * 0.3 << " m." << endl;
		break;
	case 4:
		cout << "\nEnter the number of meters: ";
		cin >> metres;
		cout << "\n" << metres << " m is " << metres * 3.2 << " ft." << endl;
		break;
	case 5:
		cout << "\nEnter the number of inches: ";
		cin >> inches;
		cout << "\n" << inches << " inches is " << inches * 2.55 << " ft." << endl;
		break;
	case 6:
		cout << "\nEnter the number of centimetres: ";
		cin >> centimetres;
		cout << "\n" << centimetres << " cm is " << centimetres * 0.4 << " inches." << endl;
		break;
	case 7:
		cout << "\nEnter the number of miles: ";
		cin >> miles;
		cout << "\n" << miles << " miles is " << miles * 1.6 << " km." << endl;
		break;
	case 8: 
		cout << "\nEnter the number of kilometers: ";
		cin >> kilometres;
		cout << "\n" << kilometres << " km is " << kilometres * 0.62 << " miles." << endl;
		break;
	case 9: 
		cout << "\nEnter the number of yards: ";
		cin >> yards;
		cout << "\n" << yards << " yards is " << yards * 0.91 << " meters." << endl;
		break;
	case 10:
		cout << "\nEnter the number of meters: ";
		cin >> metres;
		cout << "\n" << metres << " m is " << metres * 1.1 << " yards." << endl;
		break;
	case 11: 
		cout << "\nEnter the amount of EUR: ";
		cin >> EUR;
		cout << "\n" << EUR << " EUR is " << EUR * 1.26 << "USD. " << endl;
		break;
	case 12:
		cout << "\nEnter the amount of USD: ";
		cin >> USD;
		cout << "\n" << USD << " USD is " << USD * 0.79 << " EUR." << endl;
		break;
	case 13:
		cout << "\nEnter the amount of GBP: ";
		cin >> GBP;
		cout << "\n" << GBP << " GBP is " << GBP * 1.59 << " USD." << endl;
		break;
	case 14:
		cout << "\nEnter the amount of USD: ";
		cin >> USD;
		cout << "\n" << USD << " USD is " << USD * 0.63 << " GBP." << endl;
		break;
	case 15:
		cout << "\nEnter the amount of EUR: ";
		cin >> EUR;
		cout << "\n" << EUR << " EUR is " << EUR * 0.79 << " GBP." << endl;
		break;
	case 16:
		cout << "\nEnter the amount of GBP: ";
		cin >> GBP;
		cout << "\n" << GBP << " GBP is " << GBP * 1.26 << " EUR." << endl;
		break;
	case 17:
		cout << "\nEnter the amount of CNY: ";
		cin >> CNY;
		cout << "\n" << CNY << " CNY is " << CNY * 0.125 << " EUR." << endl;
		break;
	case 18:
		cout << "\nEnter the amount of EUR: ";
		cin >> EUR;
		cout << "\n" << EUR << " EUR is " << EUR * 7.99 << " CNY." << endl;
		break;
	case 19:
		cout << "\nEnter the amount of CNY: ";
		cin >> CNY;
		cout << "\n" << CNY << " CNY is " << CNY * 0.16 << " USD." << endl;
		break;
	case 20:
		cout << "\nEnter the amount of USD: ";
		cin >> USD;
		cout << "\n" << USD << " USD is " << USD * 6.35 << " CNY." << endl;
		break;
	default: 
		cout << "\nPlease enter a valid choice.\n " << endl;
		}
		}
		while ( choice !=1 && choice !=2 && choice !=3 && choice !=4 && choice !=5 && choice !=6 && choice !=7 && choice !=8 && choice !=9 && choice !=10 && choice !=11 && choice !=12
			    && choice !=13 && choice !=14 && choice !=15 && choice !=16 && choice !=17 && choice !=18 && choice !=19 && choice !=20);
			
	system("pause");
	return 0;
	}
closed account (28poGNh0)
I think you did use while loop but program did not works, than you used do while then It works am I right?
Nope, I was using do and while the whole time.
closed account (28poGNh0)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# include <iostream>
# include <cstdlib>
using namespace std;

int choice;

// You use a large screen of infos ,That's why I think you should introduce a menu function to call It after every operation .
void menu();

int main()
{
	// It is a lot better to declare your variables as a local ones ,because when your programs grow you'll find some troubles

	double pounds = 0,// Initialisatin is good
		   kilograms = 0,
		   feet = 0,
		   metres = 0,
		   centimetres = 0,
		   inches = 0,
		   kilometres = 0,
		   miles = 0,
		   yards = 0,
		   EUR = 0,
		   USD = 0,
		   GBP = 0,
		   CNY = 0;

	// We use do while if we want to excute at least once the stuff inside it,this is why your program goes right but one time,
        // I think just while could do the job ,also you can keep your do while if you want to
	// The problem is in the condutions of the do while loop you just say if the choice != 1 exit the loop ,
        // for exemple when I choose the choice 1 the program will do the conversion pounds to kg ,the choice still 1 ,
        // the loop says I will continue if the choice != 1,You know the rest..
	while ( choice>=0&&choice<=21)
	{
		menu();
		cin >> choice;

		switch (choice)
		{
			case 1:
				cout << "\nEnter the number of pounds: ";
				cin >> pounds;
				cout << "\n" << pounds << " lbs is " << pounds * 0.45 << " kg." << endl;
			break;
			case 2:
				cout << "\nEnter the number of kilos: ";
				cin >> kilograms;
				cout << "\n" << kilograms << " kg is " << kilograms * 2.2 << " pounds." << endl;
			break;
			case 3:
				cout << "\nEnter the number of feet: ";
				cin >> feet;
				cout << "\n" << feet << " ft is " << feet * 0.3 << " m." << endl;
			break;
			case 4:
				cout << "\nEnter the number of meters: ";
				cin >> metres;
				cout << "\n" << metres << " m is " << metres * 3.2 << " ft." << endl;
			break;
			case 5:
				cout << "\nEnter the number of inches: ";
				cin >> inches;
				cout << "\n" << inches << " inches is " << inches * 2.55 << " ft." << endl;
			break;
			case 6:
				cout << "\nEnter the number of centimetres: ";
				cin >> centimetres;
				cout << "\n" << centimetres << " cm is " << centimetres * 0.4 << " inches." << endl;
			break;
			case 7:
				cout << "\nEnter the number of miles: ";
				cin >> miles;
				cout << "\n" << miles << " miles is " << miles * 1.6 << " km." << endl;
			break;
			case 8:
				cout << "\nEnter the number of kilometers: ";
				cin >> kilometres;
				cout << "\n" << kilometres << " km is " << kilometres * 0.62 << " miles." << endl;
			break;
			case 9:
				cout << "\nEnter the number of yards: ";
				cin >> yards;
				cout << "\n" << yards << " yards is " << yards * 0.91 << " meters." << endl;
			break;
			case 10:
				cout << "\nEnter the number of meters: ";
				cin >> metres;
				cout << "\n" << metres << " m is " << metres * 1.1 << " yards." << endl;
			break;
			case 11:
				cout << "\nEnter the amount of EUR: ";
				cin >> EUR;
				cout << "\n" << EUR << " EUR is " << EUR * 1.26 << "USD. " << endl;
			break;
			case 12:
				cout << "\nEnter the amount of USD: ";
				cin >> USD;
				cout << "\n" << USD << " USD is " << USD * 0.79 << " EUR." << endl;
			break;
			case 13:
				cout << "\nEnter the amount of GBP: ";
				cin >> GBP;
				cout << "\n" << GBP << " GBP is " << GBP * 1.59 << " USD." << endl;
			break;
			case 14:
				cout << "\nEnter the amount of USD: ";
				cin >> USD;
				cout << "\n" << USD << " USD is " << USD * 0.63 << " GBP." << endl;
			break;
			case 15:
				cout << "\nEnter the amount of EUR: ";
				cin >> EUR;
				cout << "\n" << EUR << " EUR is " << EUR * 0.79 << " GBP." << endl;
			break;
			case 16:
				cout << "\nEnter the amount of GBP: ";
				cin >> GBP;
				cout << "\n" << GBP << " GBP is " << GBP * 1.26 << " EUR." << endl;
			break;
			case 17:
				cout << "\nEnter the amount of CNY: ";
				cin >> CNY;
				cout << "\n" << CNY << " CNY is " << CNY * 0.125 << " EUR." << endl;
			break;
			case 18:
				cout << "\nEnter the amount of EUR: ";
				cin >> EUR;
				cout << "\n" << EUR << " EUR is " << EUR * 7.99 << " CNY." << endl;
			break;
			case 19:
				cout << "\nEnter the amount of CNY: ";
				cin >> CNY;
				cout << "\n" << CNY << " CNY is " << CNY * 0.16 << " USD." << endl;
			break;
			case 20:
				cout << "\nEnter the amount of USD: ";
				cin >> USD;
				cout << "\n" << USD << " USD is " << USD * 6.35 << " CNY." << endl;
			break;
			case 21:
				choice = 22;// Or any number(>21 or <0)
			break;
			default:
				cout << "\nPlease enter a valid choice.\n " << endl;
				choice = 0;//If we dont do that the program will end if we enter for exp 33
			}system("pause");system("cls");// To clear the screen for the next operation
		}
		// If you want to do one conversion operation ,well then you can keep what I just delete.
		// although,your program seems perfectly fine and nice too
		cout << "End of program " << endl;

	system("pause");
	return 0;
	}

void menu()
{
	cout << "Weight: \n" << endl;
	cout << "Pounds      --> Kilograms:   type 1. " << endl;
	cout << "Kilograms   --> Pounds:      type 2. " << endl;
	cout << "\nDistance:\n" << endl;
	cout << "Feet        --> Metres:      type 3. " << endl;
	cout << "Metres      --> Feet:        type 4. " << endl;
	cout << "Inches      --> Centimetres: type 5. " << endl;
	cout << "Centimetres --> Inches:      type 6. " << endl;
	cout << "Miles       --> Kilometres:  type 7." << endl;
	cout << "Kilometres  --> Miles:       type 8." << endl;
	cout << "Yards       --> Metres:      type 9." << endl;
	cout << "Metres      --> Yards:       type 10." << endl;
	cout << "\nCurrency:\n" << endl;
	cout << "EUR --> USD: type 11." << endl;
	cout << "USD --> EUR: type 12." << endl;
	cout << "GBP --> USD: type 13." << endl;
	cout << "USD --> GBP: type 14." << endl;
	cout << "EUR --> GBP: type 15." << endl;
	cout << "GBP --> EUR: type 16." << endl;
	cout << "CNY --> EUR: type 17." << endl;
	cout << "EUR --> CNY: type 18." << endl;
	cout << "CNY --> USD: type 19." << endl;
	cout << "USD --> CNY: type 20." << endl;
	cout << "To quie    : type 21." << endl << endl;
}
Topic archived. No new replies allowed.