I am trying to get started with Objective-C and want to write a simple mathematical calculator. I am running OSX Lion with latest updated X-Code.
Ask the user for 3x inputs with scanf. 2x inputs = integers. 1x input = char(computational operator ie "*","+","/","-").
I message a function with these input values, but when I print the values in the function, the 2nd int(actually the last int just before sending the char) is zero.
My code is as follows:
The doCalculation function is declared as follows:
The doCalcuation function produces the following output when I enter 4,6,*:
You are about to do the following calculation on numbers 4 and 0 and use the following operator *
If I remove num2, and simply pass num1 and the comptOperator then the function returns the following output when I enter 4,*:
You are about to do the following calculation on numbers 0 and use the following operator *.
Everything works fine when:
1.) I dont use scanf, but manually *hardcode* the values passed to the function.
2.) Dont use scanf to get comptOperator (thinking this format string is probably incorrect?)
3.) I input with scanf (in this order) num1,comptOperator,num2
I dont find the logic in this and have been debugging the daylights out of it...can anyone assist please? ie tell me that this is not programmable or what the correct format string is to scanf for this comptOperator - I don't find the *correct" format string from the developer.apple...forums or google search.
Ask the user for 3x inputs with scanf. 2x inputs = integers. 1x input = char(computational operator ie "*","+","/","-").
I message a function with these input values, but when I print the values in the function, the 2nd int(actually the last int just before sending the char) is zero.
My code is as follows:
Code:
int num1 = 0;
int num2 = 0;
char comptOperator;
NSLog(@"Number one:");
scanf("%d",&num1);
NSLog(@"Number two:");
scanf("%d",&num2);
NSLog(@"Enter computational operator");
scanf("%s",&comptOperator);
doCalculation(num1,num2,comptOperator);
The doCalculation function is declared as follows:
Code:
void doCalculation (int x,int y, char z) {
NSLog(@"You are about to do the following calculation on numbers %i and %i and use the following operator %c",x,y,z);
The doCalcuation function produces the following output when I enter 4,6,*:
You are about to do the following calculation on numbers 4 and 0 and use the following operator *
If I remove num2, and simply pass num1 and the comptOperator then the function returns the following output when I enter 4,*:
You are about to do the following calculation on numbers 0 and use the following operator *.
Everything works fine when:
1.) I dont use scanf, but manually *hardcode* the values passed to the function.
2.) Dont use scanf to get comptOperator (thinking this format string is probably incorrect?)
3.) I input with scanf (in this order) num1,comptOperator,num2
I dont find the logic in this and have been debugging the daylights out of it...can anyone assist please? ie tell me that this is not programmable or what the correct format string is to scanf for this comptOperator - I don't find the *correct" format string from the developer.apple...forums or google search.
Last edited: