Learning Objective - C using Stephen Kochan's Objective C 1st Edition Book....

Discussion in 'Mac Programming' started by Darter, Mar 20, 2010.

  1. Darter macrumors newbie

    Joined:
    Mar 20, 2010
    #1
    ....And I need some guidance with Chapter 4 Exercise 2

    The task is fairly simple

    ------------------------------------------------------------------------
    2: Write a program that converts 27° from degrees Fahrenheit (F) to degrees Celsius (C) using the following formula:

    C = (F - 32) / 1.8
    -------------------------------------------------------------------------

    Okay so this is fairly straight forward but I want to go one step further and instead of hard coding the Fahrenheit temperature into the program I decided to try to write something that will take a Fahrenheit temperature from the user and then do the math and then output the Celsius result.
    And here I am running into a wall. Now please understand I have no formal programming education. But I have been working in IT Desktop Support for a couple of years and I am now trying to expand my horizons and learn programming and I figured that since the Mac is my platform of choice for personal use I would first start trying to write for it. So any ways on to the code itself (I know it looks awful but I am trying to get better)
    --------------------------------------------------------------------------
    #import <stdio.h>

    main()

    {

    float 'Celsius';
    float 'Fahrenheit';

    Celsius = (Farhrenheit - 32) / 1.8;

    printf ("Input Fahrenheit Temprature\n");
    scanf Fahrenheit;


    printf Celsius;
    }
    --------------------------------------------------------------------------
    When I try to compile with GCC I get the following

    --------------------------------------------------------------------------
    the-house-2:Chapter 4 brian$ gcc Exercise\ 2.m -o Exercise2 -l objc
    Exercise 2.m:7:8: warning: character constant too long for its type
    Exercise 2.m: In function ‘main’:
    Exercise 2.m:7: error: syntax error before '\x73697573'
    Exercise 2.m:8:8: warning: character constant too long for its type
    Exercise 2.m:10: error: ‘Celsius’ undeclared (first use in this function)
    Exercise 2.m:10: error: (Each undeclared identifier is reported only once
    Exercise 2.m:10: error: for each function it appears in.)
    Exercise 2.m:10: error: ‘Farhrenheit’ undeclared (first use in this function)
    Exercise 2.m:13: error: syntax error before ‘Fahrenheit’
    the-house-2:Chapter 4 brian$
    --------------------------------------------------------------------------

    Now what I don't understand is that it seems to me that it is mainly complaining about not having declared the variables ‘Celsius’ and ‘Farhrenheit’ but the declaration is right there in the beginning of the program. What am I doing wrong here?

    Thanks in advance:).
     
  2. Rhalliwell1 macrumors 6502a

    Joined:
    May 22, 2008
    #2
    you need to be doing something more along the lines of:

    Code:
    #import <stdio.h>
    
    main()
    
    {
    
    float Celsius;
    float Fahrenheit;
    
    printf ("Input Fahrenheit Temprature\n");
    scanf ("%f", &Fahrenheit);
    
    Celsius = (Farhrenheit - 32) / 1.8;
    
    printf("The temperature in celsius is %f\n", Celsius);
    
    }
     
  3. Alkiera macrumors regular

    Joined:
    Mar 11, 2008
    #3
    Also, check your spelling for Fahrenheit, you've got it spelled differently in different places.
     
  4. Darter thread starter macrumors newbie

    Joined:
    Mar 20, 2010
    #4
    Thanks

    Thanks. I now officially feel a little smarter for it now although in retrospect the solution was a little obvious.
     
  5. zophtx macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Inside a Cocoa Book
    #5
    remember when you use a 'printf' and a 'scanf' statement you always us '(' ')'

    Code:
    printf("Enter Example\n");
    
    scanf("%i", &isExample); // uses '&' for the declaring the value for %i
    
    printf("%i is the Example", isExample); // doesn't use the '&' for declaring value
    -----------------------------------
    that how you use printf and scanf.
    but im sure you know this by now.
     
  6. skochan macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #6
    Forum

    @Darter: You may want to visit the forum for the book at classroomM.com/objective-c. There you'll find lots of support, including answers to exercises for the first edition.

    Cheers,

    Steve Kochan
     
  7. Exponent macrumors regular

    Joined:
    Jul 17, 2002
    Location:
    Silicon Valley
    #7
    Stephen Kochan posts here? COOL!

    Hi Mr. Kochan-

    I just wanted to let you know that for the past 22(?) years, I have a simple rule: If you write a book, I buy it. You explain technical issues in a clear cut, unadorned, but understandable way. Your work has helped me write programs that have powered the Silicon Valley & semiconductor industry in general for 17 years now.

    So here's a big "hat's off" to you!

    Exponent
    Silicon Valley Survivor
     
  8. skochan macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #8
    Wow! Thanks so much for the compliments! :eek:

    Cheers,

    Steve
     

Share This Page