PDA

View Full Version : Mulitple inputs - looking for the blank




dieseltwitch
Dec 24, 2011, 05:47 PM
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
Dec 24, 2011, 06:10 PM
So I came up with this. But I want to know if there is a faster way or better way to do it?


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");

}




}

lee1210
Dec 24, 2011, 06:14 PM
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

dieseltwitch
Dec 24, 2011, 06:17 PM
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?

lee1210
Dec 24, 2011, 06:24 PM
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

dieseltwitch
Dec 24, 2011, 06:28 PM
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
Dec 24, 2011, 06:46 PM
I figured out that i use
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.

lee1210
Dec 24, 2011, 07:27 PM
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

dieseltwitch
Dec 24, 2011, 07:32 PM
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

lee1210
Dec 25, 2011, 12:25 AM
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

dieseltwitch
Dec 25, 2011, 12:44 AM
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

lee1210
Dec 25, 2011, 10:23 AM
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

PatrickCocoa
Dec 25, 2011, 06:50 PM
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.

dieseltwitch
Dec 25, 2011, 09:23 PM
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.


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?

PatrickCocoa
Dec 26, 2011, 02:23 PM
Keep at it. There's a lot going on, a lot to learn. You will get frustrated, but that's OK.

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.

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".

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.

dieseltwitch
Dec 26, 2011, 03:07 PM
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.