I am writing a simple console application in Objective-C but I have heard that this coding may be loosely applied to some C as well, so I wanted to make that clear.
I am having a slight problem however, when I run from the Debugger in Xcode, for some reason, my program does not wait for user input, it just rolls right on through, displaying the strings and converting a number automatically.
Is there a case for this?
Did I not specify something correct in my if else statement?
Here is my code:
//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit
#import <stdio.h>
@interface Converter: NSObject
{
//Instance variable which stores converting formula for objects
double formula;
}
//Declare instance method for setting instance variable
-(double) convert: (double) expression;
//Declare instance method for getting instance variable
-(double) formula;
@end
@implementation Converter;
//Define instance method to set the argument (expression) equal to the instance variable
-(double) convert: (double) expression
{
formula = expression;
}
//Define instance method for returning instance variable, formula
-(double) formula
{
return formula;
}
@end
int main (int argc, char *argv[])
{
//Point two new objects, one for the Fahrenheit conversions and the other for the Celsius conversions, to the Converter class
Converter *fahrenheitConversion = [[Converter alloc] init];
Converter *celsiusConversion = [[Converter alloc] init];
//Declare two double variables holding the user-inputted data, and one integer variable for the if else statement
double fahrenheit, celsius;
int prompt;
NSLog(@"Please press 0 to convert Celsius to Fahrenheit, or 1 to convert Fahrenheit to Celsius\n ");
scanf("%i", &prompt);
if(prompt == 0) {
NSLog(@"Please enter a temperature in Celsius to be converted into Fahrenheit!:\n");
scanf("%lf", &celsius);
[fahrenheitConversion convert: ((celsius*(9/5)) + 32)];
NSLog(@"%lf degrees Celsius is %lf Fahrenheit", celsius, [fahrenheitConversion formula]);
}
else {
NSLog(@"Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
[celsiusConversion convert: ((fahrenheit - 32)*(5/9))];
NSLog(@"%lf degrees Fahrenheit is %lf Celsius", fahrenheit, [celsiusConversion formula]);
}
return 0;
}
UPDATE:
It looks as though the reason the console in the Xcode debugger did not wait for my input was because I had it set as an iPhone application, not a Mac OS X Cocoa, I can’t even remember why I did that. What I really should do is just make a project with no template, so it doesn’t expect anything..
I’m still really appreciative and open to suggestions like dreamlax, and I’m trying to implement your code suggestion, it’s a little harder than expected though, because I’m still very new.
When you call
scanf, always check the return value. It will return however many items were successfully scanned. In your application, you only provide one argument, so ifscanfwas successful, then it should return 1. It can return two other values:scanfmay returnEOF.scanfmay return 0 (this may happen if the user provided an alphabetic character instead of a number, or if the user just pressed enter without typing anything, etc.)You should check the result of each
scanfcall and handle it appropriately.There could be a number of reasons why
scanfmay not return 1 while running through a debugger, here’s a few that I can think of:stdinis closed for some reason, so reading fromstdinwill always fail.stdinstream. I’m not sure where this could come from.Some further advice
It may be better if you store the temperature in your class using only a single scale. For example:
Then, if you have a value in fahrenheight, you can do:
See if you can implement other temperature scales too, such as Kelvin, and Rankine!