Skip to content

Commit b022fa9

Browse files
committed
REFACTOR: UnitConverter code.
1 parent 7cffddf commit b022fa9

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

Projects/UnitConverter.c

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,43 @@ void mass();
88

99
int main(){
1010

11-
label1:
11+
char retry, category;
1212

13-
//clears the terminal screen
14-
system("cls");
15-
16-
char category;
17-
printf("Welcome to Unit Converter!\n");
18-
printf("Here is a list of converters to choose from: \n");
19-
printf("Enter 'T' for Temperature convertion.\n");
20-
printf("Enter 'M' for Mass convertion.\n");
21-
printf("Entre 'C' for Currency convertion.\n\n");
22-
printf("Enter input: ");
23-
scanf(" %c", &category);
24-
25-
if(category=='T' || category=='t')
26-
temperature();
27-
else if (category=='M' || category=='m')
28-
mass();
29-
else if (category=='C' || category=='c')
30-
currency();
31-
else
32-
printf("ERROR: Invaild Input. \n ");
33-
34-
//Prompt for try again ...
35-
char retry;
36-
printf("\nDo you wanna try again [y/N]: ");
37-
scanf(" %c",&retry);
38-
39-
if(retry=='y' || retry=='Y')
40-
goto label1;
41-
else if (retry=='n' || retry=='N')
42-
printf("\nProgram Ended, Thanks for testing it.");
43-
else
44-
printf("\nInvaild Input, Program Ended. ");
13+
do
14+
{
15+
// clears the terminal screen
16+
system("cls");
17+
18+
printf("Welcome to Unit Converter!\n");
19+
printf("Here is a list of converters to choose from: \n");
20+
printf("Enter 'T' for Temperature convertion.\n");
21+
printf("Enter 'M' for Mass convertion.\n");
22+
printf("Entre 'C' for Currency convertion.\n\n");
23+
printf("Enter input: ");
24+
scanf(" %c", &category);
25+
26+
fflush(stdin);
27+
28+
switch (category)
29+
{
30+
case 'T':
31+
case 't':
32+
temperature();
33+
case 'M':
34+
case 'm':
35+
mass();
36+
case 'C':
37+
case 'c':
38+
currency();
39+
default:
40+
printf("\nError: Invalid Input. \n");
41+
}
42+
43+
printf("\nDo you wanna try again [y/N]: ");
44+
scanf(" %c", &retry);
45+
fflush(stdin);
46+
47+
} while (retry == 'y' || retry == 'Y');
4548

4649
return 0;
4750
}
@@ -52,6 +55,8 @@ void temperature()
5255
system("cls");
5356

5457
int input;
58+
float celsius, fahrenheit;
59+
5560
printf("Welcome to Temperature Converter!\n");
5661
printf("Here is a list of conversion to choose from: \n");
5762
printf("Enter 1 for Celsius to Fahrenheit. \n");
@@ -64,8 +69,6 @@ void temperature()
6469

6570
if(input==1)
6671
{
67-
float celsius, fahrenheit;
68-
6972
printf("Enter Celisus: ");
7073
scanf("%f",&celsius);
7174

@@ -75,8 +78,6 @@ void temperature()
7578
}
7679
else if (input==2)
7780
{
78-
float fahrenheit, celsius;
79-
8081
printf("Enter Fahrenheit: ");
8182
scanf("%f",&fahrenheit);
8283

@@ -185,4 +186,4 @@ void currency()
185186
}
186187
else
187188
printf("ERROR: Invaild Input.");
188-
}
189+
}

0 commit comments

Comments
 (0)