Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 995215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:41:20+00:00 2026-05-16T06:41:20+00:00

I am writing a simple console application in Objective-C but I have heard that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:41:21+00:00Added an answer on May 16, 2026 at 6:41 am

    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 if scanf was successful, then it should return 1. It can return two other values:

    1. If there was an error reading input, or if the input ended before successfully scanning all format descriptions, scanf may return EOF.
    2. If there was no input that matched the format description provided, scanf may 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 scanf call and handle it appropriately.

    int result = scanf("%i", &prompt);
    
    if (result == EOF || result == 0)
    {
        NSLog (@"There was an error reading your choice.");
        exit(1);
    }
    else
    {
        // result should be 1 here, continue onwards doing work
    }
    

    There could be a number of reasons why scanf may not return 1 while running through a debugger, here’s a few that I can think of:

    1. stdin is closed for some reason, so reading from stdin will always fail.
    2. There could be some data being piped into your application’s stdin stream. 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:

    @interface Temperature : NSObject
    {
        double celsius; // only store one temperature
    }
    
    - (id) initWithFahrenheit:(double) f;
    - (id) initWithCelsius:(double) c;
    
    - (double) fahrenheitValue;
    - (double) celsiusValue;
    @end
    
    @implementation Temperature
     - (id) initWithFahrenheit:(double) f
    {
        self = [super init];
        if (!self) return nil;
        celsius = (f - 32)*(5/9);
        return self;
    }
    
    - (id) initWithCelsius:(double) c
    {
        self = [super init];
        if (!self) return nil;
        celsius = c;
        return self;
    }
    
    - (double) fahrenheitValue
    {
        return (celsius*(9/5)) + 32);
    }
    
     - (double) celsiusValue
    {
        return celsius;
    }
    
    @end
    

    Then, if you have a value in fahrenheight, you can do:

     Temperature *myTemp = [[Temperature alloc] initWithFahrenheit:f];
     double celsius = [myTemp celsiusValue];
    

    See if you can implement other temperature scales too, such as Kelvin, and Rankine!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long-running console-based application Sender that sends simple text to STDOUT using
I have a simple console application that runs calculations in several threads (10-20 of
I am writing a simple console application in LInux/C++ that accepts user input from
I'm writing a simple OpenGL application that uses GLUT . I don't want to
I'm writing a simple app that's going to have a tiny form sitting in
I am writing a simple Python web application that consists of several pages of
I'm writing a simple console application (80x24) in Java. Is there a gotoxy(x,y) equivalent
Okay, simple situation: I'm writing a simple console application which connects to a SOAP
We have a console application (currently .NET) that sends out mail in bulk to
I'm writing a sample console application in VS2008. Now I have a Console.WriteLine() method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.