开发者

Creating a program loop for the first time in objective-c

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开发者_JAVA百科;

  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.


Yes you could just put it in a loop, and ask the exit question right before the end of the loop. Depending on the answer, you could just exit(0). Or you could integrate it in the first question; 1=Fahrenheit, 2=..., 0 = Exit.

The loop could just be while(1) { ... }. Another approach would be to have a variable before the loop:

int done = 0;

and then loop over while ( !done ) { ... }. (read this as "while not done"). In the loop, set done=1 when you're done, and the loop will then terminate.

(for clarity: it will terminate only after completing the whole { ... } block, but you will find that out - if you come to that point you need to read(/ask) about continue and break)


There are a few things to consider: does your loop need to clean up? Here

 Temperature *converter = [[Temperature alloc] init];

you allocate some memory. If you just loop again, you will allocate some more memory. And so on: this is called a "memory leak". This goes on until you run out of memory and the program would crash (although it would take a long long time in this case).

So you should really release the converter when you're done with it, by doing

 [converter release];

This way you will not leak any memory.


Also this would be a good moment to put parts of your program in a separate function, because it becomes a little bit unclear what exactly is happening when it gets bigger and bigger.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜