I am trying to add some "replay-value" if you will to my temperature scale conversion console program in Objective-C by adding a simple loop.
Now, here is the code for my current main.m file:
#import <Cocoa/Cocoa.h>
#import "class.h"
int main(int argc, char *argv[])
{
int result;
int prompt, prompt2, sourceTempText;
double sourceTemp;
printf("Please choose a source temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n");
result = scanf("%i", &prompt);
if (result != 1)
printf("I couldn't understand your input, I need only one number!");
else if (result == EOF)
printf("I apologize, I encountered an error when trying to read your input.");
else if (result == 1)
{
printf("\nNow, please enter the temperature you would like to convert:\n\n");
scanf("%lf", &sourceTemp);
Temperature *converter = [[Temperature alloc] init];
switch (prompt)
{
case 1:
//end-user chooses Fahrenheit
[converter setFahrenheitValue:sourceTemp];
sourceTempText = 1;
break;
case 2:
//end-user chooses Celsius
[converter setCelsiusValue:sourceTemp];
sourceTempText = 2;
break;
case 3:
//end-user chooses Kelvin
[converter setKelvinValue:sourceTemp];
sourceTempText = 3;
break;
case 4:
//end-user chooses Rankine
[converter setRankineValue:sourceTemp];
sourceTempText = 4;
break;
}
printf("\nNow, please choose a target temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n");
scanf("%i", &prompt2);
char *scales[4] = { "Fahrenheit", "Celsius", "Kelvin", "Rankine" };
switch (prompt2)
{
case 1:
//end-user chooses Fahrenheit
printf("%lf degrees %s is %lf degrees Fahrenheit\n", sourceTemp, scales[prompt-1], [converter fahrenheitValue]);
break;
case 2:
//end-user chooses Celsius
printf("%lf degrees %s is %lf degrees Celsius\n", sourceTemp, scales[prompt-1], [converter celsiusValue]);
break;
case 3:
//end-user chooses Kelvin
printf("%lf degrees %s is %lf degrees Kelvin\n", sourceTemp, scales[prompt-1], [converter kelvinValue]);
break;
case 4:
//end-user chooses Rankine
printf("%lf degrees %s is %lf degrees Rankine\n", sourceTemp, scales[prompt-1], [converter rankineValue]);
break;
}
}
}
OK, so I would like to prompt the user with a printf statement, asking them if they would like to convert another temperature once they have made their first conversion.
The prompt would ask the end-user to press 0 to exit the program, or 1 to make another conversion.
My first inclination was to declare an integer variable which would be set to 0 or 1 from scanf once the end-user has inputted their choice.
Then, if the new variable == 1, then it would loop back to the beginning, if not, it would exit the program.
Pretty simple, huh?
Just wondering, is there a more efficient way to loop this program or is this a good way, at least with the basic knowledge I have now.