Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 24, 2011, 05:47 PM   #1
dieseltwitch
macrumors regular
 
Join Date: Jan 2008
Mulitple inputs - looking for the blank

I'm trying to write a simple Command line program that uses the fallowing formal

BTU = GPM*500*(Tf-Ti)

BTU = BTU's Per Hour
GPM = Gallons Per Minute
Tf = Temperature Final
Ti = Temperature Initial

What I want todo is allow me to type in any three of the four variables and have the computer access any of four different formulas the find the missing value

for example
If i type in the fallowing
BTU = 100,000
GPM = 5.5
Ti = 55

I want it to see that Tf is missing and go directly to the

Tf = BTU/(GPM*500) + Ti

to find the final output temperature.

How might I go about doing this?
dieseltwitch is offline   0 Reply With Quote
Old Dec 24, 2011, 06:10 PM   #2
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
So I came up with this. But I want to know if there is a faster way or better way to do it?
Code:
int findWhat,gpm,btu,tf,ti;
        
        NSLog(@"1 - BTU/hr");
        NSLog(@"2 - GPM");
        NSLog(@"3 - Temperature Final");
        NSLog(@"4 - Temperature Inital");
        NSLog(@"What would you like to find?");
        scanf("%d", &findWhat);
        switch (findWhat) {
            case 1:
                NSLog(@"Finding BTU/Hr");
                NSLog(@"GPM?");
                scanf("%d", &gpm);
                NSLog(@"Temperature Final?");
                scanf("%d", &tf);
                NSLog(@"Temperature Inital?");
                scanf("%d", &ti);
                btu = gpm * 500 * (tf - ti);
                NSLog(@"Output is: %d BTU/hr",btu);
                break;
            case 2:
                NSLog(@"Finding GPM");
                NSLog(@"BTU/HR?");
                scanf("%d", &btu);
                NSLog(@"Temperature Final?");
                scanf("%d", &tf);
                NSLog(@"Temperature Inital?");
                scanf("%d", &ti);
                gpm = btu/(500*(tf - ti));
                NSLog(@"Flow rate is: %d GPM",gpm);
                break;
            case 3:
                NSLog(@"Finding Temperature Final");
                NSLog(@"BTU/HR?");
                scanf("%d", &btu);
                NSLog(@"GPM?");
                scanf("%d", &gpm);
                NSLog(@"Temperature Inital?");
                scanf("%d", &ti);
                tf = btu/(500*gpm)+ti;
                NSLog(@"Final Water Temperature is: %dF",tf);
                break;
            case 4:
                NSLog(@"Finding Temperature Inital");
                NSLog(@"BTU/HR?");
                scanf("%d", &btu);
                NSLog(@"GPM?");
                scanf("%d", &gpm);
                NSLog(@"Temperature Final?");
                scanf("%d", &tf);
                ti = tf - btu/(500*gpm);
                NSLog(@"Inital Water Temperature is: %dF",ti);
                break;
                default:
                NSLog(@"Error - No Such Task Number");
                
        }
        
        
        
        
    }
dieseltwitch is offline   0 Reply With Quote
Old Dec 24, 2011, 06:14 PM   #3
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Accept input of each variable one at a time. Let the user enter nothing or a question mark for the unknown value. If more than one value is unknown, give an error. If 3 values are entered, run the appropriate formula for the missing value:

Tf = BTU/(GPM*500) + Ti
TI = -1*BTU/(GPM*500) - Tf
BTU = GPM*500*(Tf-Ti)
GpM = BTU/(500*(Tf-Ti))

Remember to check for a zero denominator whenever there is division.

-Lee
lee1210 is offline   0 Reply With Quote
Old Dec 24, 2011, 06:17 PM   #4
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
Quote:
Originally Posted by lee1210 View Post
Accept input of each variable one at a time. Let the user enter nothing or a question mark for the unknown value. If more than one value is unknown, give an error. If 3 values are entered, run the appropriate formula for the missing value:

Tf = BTU/(GPM*500) + Ti
TI = -1*BTU/(GPM*500) - Tf
BTU = GPM*500*(Tf-Ti)
GpM = BTU/(500*(Tf-Ti))

Remember to check for a zero denominator whenever there is division.

-Lee
Would I not have to take in each variable as a char then convert it to an int or float? is that even possible?

----------

also if a int comes in as a %d, float as a %f what does a char come in as?
dieseltwitch is offline   0 Reply With Quote
Old Dec 24, 2011, 06:24 PM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Yes, you'd have to read strings. Your approach is fine if you don't know how. You do still need to check for divide-by-zero.

-Lee
lee1210 is offline   0 Reply With Quote
Old Dec 24, 2011, 06:28 PM   #6
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
How would I initiate the 4 variables as strings and then later convert the them to floats or inits?

as far as looking for divide by zero that should be fairly easy once the right formula has been selected i will just check for the any of the values that would result in a zero in the denominator, if that exists i can exit with an error other wise it will just continue, correct?
dieseltwitch is offline   0 Reply With Quote
Old Dec 24, 2011, 06:46 PM   #7
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
I figured out that i use
Code:
NSString *btu;
NSString *gpm;
NSString *tf;
NSString *ti;
To declare them but how do I use scanf with a string? the %@ used in NSLog causes an error when used in scanf.
dieseltwitch is offline   0 Reply With Quote
Old Dec 24, 2011, 07:27 PM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I'm not sure what language you're using (I guess Objective-C if you can compile with NSString) or your skill level with programming in general or these languages.

Your current strategy of allowing which variable to solve for seems best for your skill level. Learning robust input validation is a good idea, but it is a big undertaking. You need to learn to deal with limiting input based on your buffer length, converting from character data to numerical values with good error handling, etc. I actually don't know if there are good console I/O in the libraries using NSStrings.

-Lee
lee1210 is offline   0 Reply With Quote
Old Dec 24, 2011, 07:32 PM   #9
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
Quote:
Originally Posted by lee1210 View Post
I'm not sure what language you're using (I guess Objective-C if you can compile with NSString) or your skill level with programming in general or these languages.

Your current strategy of allowing which variable to solve for seems best for your skill level. Learning robust input validation is a good idea, but it is a big undertaking. You need to learn to deal with limiting input based on your buffer length, converting from character data to numerical values with good error handling, etc. I actually don't know if there are good console I/O in the libraries using NSStrings.

-Lee
Yeah I'm learning that the console is having a hard time with NSString inputs. And yes Im using Obj-C. As for my level... well I've been writing code for years on my calculators in "basic" to do basically this but I want to move on to bigger things. I initially made this program to just test with and learn with but I think that console does not handle user input errors like an actual app would. I looked into vscanf and a few other but they are either from a different language or just not meant to play well. lol
dieseltwitch is offline   0 Reply With Quote
Old Dec 25, 2011, 12:25 AM   #10
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Most, if not all, console input functions will be C. You can use them, but you'll need to use character arrays for storage, rather than having nice NSStrings. Depending on your immediate goals this may not be the right time to get familiar with these functions and strategies. If you're going to make a GUI app soon you won't get much out of rich console I/O functions.

-Lee
lee1210 is offline   0 Reply With Quote
Old Dec 25, 2011, 12:44 AM   #11
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
Quote:
Originally Posted by lee1210 View Post
Most, if not all, console input functions will be C. You can use them, but you'll need to use character arrays for storage, rather than having nice NSStrings. Depending on your immediate goals this may not be the right time to get familiar with these functions and strategies. If you're going to make a GUI app soon you won't get much out of rich console I/O functions.

-Lee
Yeah, that makes good sense. I was just trying to see how the math was handled using obj-c before I go to a more GUI interface. My app is going to use a lot of trig, calc, and other back and forth junk. lots of sun altitude, declination... and other calculations
dieseltwitch is offline   0 Reply With Quote
Old Dec 25, 2011, 10:23 AM   #12
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I'd spend your time reading the man page for math.h rather than boning up on console I/O. You do need to remember that most of what you're doing in Objective-C is just plain C, so you'll need to get comfortable using C functions, libraries, etc.

-Lee
lee1210 is offline   0 Reply With Quote
Old Dec 25, 2011, 06:50 PM   #13
PatrickCocoa
macrumors 6502a
 
Join Date: Dec 2008
Gui

Quote:
Originally Posted by dieseltwitch View Post
Yeah, that makes good sense. I was just trying to see how the math was handled using obj-c before I go to a more GUI interface. My app is going to use a lot of trig, calc, and other back and forth junk. lots of sun altitude, declination... and other calculations
For this case, and for your future trig app, it will be easier to go straight to the GUI and skip the command line. It may sound counter-intuitive that adding GUI functionality is easier than ignoring it. Get a tutorial on building a calculator in Cocoa with Xcode 4 and all of your input issues are solved.

I'm not sure what you mean by "math handling in obj-c". Math is math. Perhaps you're referring to the translation from the NSString and NSText and NSLabel in Cocoa (not obj-c) to primitives that you're used to working with like int, double, and long. In that case, the calculator tutorial above will step you through that.
__________________
iMac 21.5", 3.06GHz, 4 GB, 2 TB HD.
iPod Touch 3G.
PatrickCocoa is offline   0 Reply With Quote
Old Dec 25, 2011, 09:23 PM   #14
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
Quote:
Originally Posted by lee1210 View Post
I'd spend your time reading the man page for math.h rather than boning up on console I/O. You do need to remember that most of what you're doing in Objective-C is just plain C, so you'll need to get comfortable using C functions, libraries, etc.

-Lee
Yeah, I'm learning its just a 'painful learning curve' but Im getting there.


Quote:
Originally Posted by PatrickCocoa View Post
For this case, and for your future trig app, it will be easier to go straight to the GUI and skip the command line. It may sound counter-intuitive that adding GUI functionality is easier than ignoring it. Get a tutorial on building a calculator in Cocoa with Xcode 4 and all of your input issues are solved.

I'm not sure what you mean by "math handling in obj-c". Math is math. Perhaps you're referring to the translation from the NSString and NSText and NSLabel in Cocoa (not obj-c) to primitives that you're used to working with like int, double, and long. In that case, the calculator tutorial above will step you through that.
My problem with going strait to a GUI is I have hard time finding error. not knowing where things are going wrong during a program can be extremely frustrating.

I looked for a calc app but they all seem to be Xcode 3.

What I mean by "math handling" is knowing how to build an equation so that it works in Xcode. Like right now most of my stuff is handled inside of excel or numbers. this is getting very old and very clunky. I just need to see how the syntax works. I'm learning fast.

Do you know of any good tutorials for x-code 4 on calculators?
dieseltwitch is offline   0 Reply With Quote
Old Dec 26, 2011, 02:23 PM   #15
PatrickCocoa
macrumors 6502a
 
Join Date: Dec 2008
Good questions, keep them coming!

Keep at it. There's a lot going on, a lot to learn. You will get frustrated, but that's OK.

Quote:
Originally Posted by dieseltwitch View Post
My problem with going strait to a GUI is I have hard time finding error. not knowing where things are going wrong during a program can be extremely frustrating.
I'm not arguing with you for the sake of arguing, but I don't see how going to the GUI makes it harder to find any errors. If you start with a tutorial GUI app, there won't be any GUI related errors. Also, the effort to get the command line app to recognize separate inputs equals the effort to get the GUI app to work.

I don't see the benefit of starting with a command line app as opposed to a GUI app.

Quote:
I looked for a calc app but they all seem to be Xcode 3. Do you know of any good tutorials for x-code 4 on calculators?
Googled "Xcode 4 tutorial" and the fourth results was "Xcode 4 Tutorial Basic Calculator" at http://www.youtube.com/watch?v=Ihw0cfNOrr4. I haven't watched it, but it's 20 minutes long and it seen very detailed from my five-second glance at it.

If that doesn't work, head back to google. I suggested a calculator app because that takes in numbers and does calculations on them, which is what you want to do. Once you get the calculator app going, you can implement your app by changing the "plus" function to "translate-temperature-differential-to-BTU".

Quote:
What I mean by "math handling" is knowing how to build an equation so that it works in Xcode. Like right now most of my stuff is handled inside of excel or numbers. this is getting very old and very clunky. I just need to see how the syntax works. I'm learning fast.
I think you're talking about syntax (how to express what you want to do in a format that the objective-c understands). You'll pick that up as you walk through the tutorial. There are two types of syntax in objective-c:
1. The normal C syntax, where a formula may look like:
(variable name) = (some constant) * (another variable name)

for example

britishThermalUnitsPerHour = 500 * gallonsPerMinute
* (temperatureFinalInCelcius -temperatureInitalInCelcius)

2. Method messaging syntax
(variable name) = [(object) (message)];

for example

NSString * myName;
myName = [[NSString alloc] init];
myName = @"PatrickCocoa";

Note the verbosity of my variable names. Your code has a variable "BTU" which it turns out is NOT British Thermal Units, but is BTUs per hour. That is misleading and will cause confusion (I originally had my variable name as "britishThermalUnits" until I happened to take another look at your original code. If you're programming in a team, misleading variable names will quickly get you ostracized. If you're not programming in a team, then six-months-from-now-dieseltwitch will thank current-dieseltwitch. Believe me.
__________________
iMac 21.5", 3.06GHz, 4 GB, 2 TB HD.
iPod Touch 3G.
PatrickCocoa is offline   0 Reply With Quote
Old Dec 26, 2011, 03:07 PM   #16
dieseltwitch
Thread Starter
macrumors regular
 
Join Date: Jan 2008
Well doing in a command line all made sense when I started. lol. but Im actually picking up on this quickly.

As for the BTU vs BritishThermalUnits thing. as i said before I've only been programing on a calculator (Voyage 200), and is doesn't like some names. so its just taking me a while to get used to it.

I've started reading the book that was recommended earlier in this post and its a great books so thank you again!

As for the calc app, its for the iPhone os, not for an OS X desktop app. and thats where i run into trouble because I'm finding thats not an easy port for a starting out with, when even small changes don't pop right out.
dieseltwitch is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Mulitple USB wireless speakers luke1081 MacBook Pro 0 Mar 24, 2014 07:44 AM
iPad: How many inputs can the ipad receive at once? chado53 iPad 2 Jun 14, 2013 06:01 PM
In iMessage , contact sends me a texts that is blank and only shows the blank bubble. Jooohhn243 iPhone Tips, Help and Troubleshooting 0 Dec 3, 2012 07:56 PM
2 Keyboards, 2 Different Language Inputs. Possible? -BigMac- OS X 4 Aug 21, 2012 09:22 AM
TB Display used for 2 computer inputs???? foto-max Mac Peripherals 3 Jun 19, 2012 10:10 PM

Forum Jump

All times are GMT -5. The time now is 09:02 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC