Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 23, 2018, 01:42:56 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: How do you solve 4.6?  (Read 1486 times)
Posts: 4

« on: January 09, 2012, 10:58:51 PM »

I've seen a try here, and in Chapter 7, where the exercise is revisited.  I don't agree with the program's written by the forum members.  They just tack on an "i" at the end of the int.  If you tested that, and decided to make it "3i + 3", it wouldn't work.

How do you accept the user's input of "3i" and display it in NSLog?

Is there a way to take two characters from the user and store it in a variable without using a string?  A char is only good for one character, correct?

Posts: 27

« Reply #1 on: January 10, 2012, 02:51:13 AM »

4.6 specifies that Complex should only use two doubles for its instance variables, a double for both real and imaginary (as you can see from the declarations for the accessors).

No, you cannot store "i" in a double. As far as allowing the user to input "x+yi" in the terminal (which is definitely beyond the scope of the question), dealing with formatted multi-data type input probably involves fiddling with scanf or some other low-level C method way more than any OOP should have to.

It's not worth bothering with. You're not really going to be building terminal applications to input imaginary numbers on a single line - you're going to use GUIs eventually with an interface like PCalc and working with Core Data objects that have actual I/O and text parsing capabilities.

The author is probably envisioning the easier solution of just getting the real and imaginary numbers (as just doubles) separately from the user and formatting the whole complex number in a separate print method. Or just setting them yourself in the main and ignoring scanf for now.

The key at this point of the book is to focus on how to organize data in classes. Data classes like Complex are ultimately going to be part of the Model you'll use to extend views and controllers (which handle the I/O in real apps) - just ignore the terminal except as a means to debug your program. That's all you need it for.
« Last Edit: January 10, 2012, 04:19:27 AM by jgelling » Logged
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.