views:

59

answers:

1

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.

+2  A: 

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.

mvds
Hey cool, thanks. I should have really done that already, I was instructed to do so but I had forgotten xD Anyway, is there a good reference you could point me to to help me understand the concept of storing various parts of a program in different functions?
BOSS
Well I wouldn't know, it's very basic to (almost) any language. The basic concept is that you give zero or more arguments, and that one value comes out; the return value. So you could try to make `int getOneInt(char *message) { printf("%s\n",message); ...scanf...etc.. return i; }` which would then get one int from the user, and then return it. So everytime you need an int, you just do `int a = getOneInt("now please enter source scale");` and your main loop will not have to deal with those issues three times in a row.
mvds
Sorry, if what you mean is adding some more commonly used methods to my class files instead of using recurring instances of printf like you just mentioned, then I already know what you mean. Yeah, I'm new fo sho ahaha.
BOSS
It doesn't need to be an objective-C class, you can also create an extra function in main.c, just like `int main(int argc, char**argv) { ... }` is also a function.
mvds
Oh duh! Ahaha, sometimes I lose track of what I'm really programming in. Don't you mean main.m though?
BOSS
Yes, I mean main.m!
mvds