Hey everyone, 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.