discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: my calculator has problems


From: Germán Arias
Subject: Re: my calculator has problems
Date: Fri, 08 Apr 2016 22:02:28 -0600

Hi,

I have not clear what is "number1". But in method "-add" you assign a
string to this variable, so this become a string object. Then, later,
you call "-setDoubleValue:" at "number1" as if it was an NSTextField
object. If this is an NStextField object, then you should do (in method
"-add"):

[number1 setDoubleValue: 0];

Hope this help.
Germán

El vie, 08-04-2016 a las 15:01 -0400, umen escribió:
> Hi people,
> 
> I'm trying to write a basic calculator using GNUstep+ObjC and I get stucked. 
> I really don't know where is the problem in my code. May you help me?
> 
> 
> I'm sending here what I have implemented until now:
> 
> 
> 
> @interface UCalc : NSObject
> 
> {
> 
>   char operation; //just a char to validate the operation
> 
>   id number1, number2; //number1 = the current button pressed, number2
> = the old number
> 
>   id result; //the calculator display
> 
> }
> 
> - (void) Add: (id)sender;
> 
> @end
> 
> 
> 
> 
> 
> @implementation UCalc
> 
> - (void) Add: (id)sender
> 
> {
> 
>   operation = '+';
> 
>   number2 = number1; //when you press the add button the current
> number becomes the old number
> 
>   number1 = @"0";
> 
> }
> 
> 
> 
> - (void) Button0: (id)sender
> 
> {
> 
>   number1 = @"0";
> 
>   [result setStringValue: number1];
> 
> }
> 
> 
> 
> - (void) Button1: (id)sender
> 
> {
> 
>   //if there is no number yet
> 
>   if (number1 == nil || [number1 isEqual: @"0"]){
> 
>     [result setStringValue: @""];
> 
>     number1 = @"1";
> 
>   }
> 
>   //else append the current number in itself, so we can get number > 9
> 
>   else{
> 
>     number1 = [number1 stringByAppendingFormat: @"1"];
> 
>   }
> 
>   [result setStringValue: number1];
> 
> }
> 
> 
> 
> 
> 
> - (void) Button2: (id)sender
> 
> {
> 
>   if (number1 == nil || [number1 isEqual: @"0"]){
> 
>     [result setStringValue: @""];
> 
>     number1 = @"2";
> 
>   }
> 
>   else{
> 
>     number1 = [number1 stringByAppendingFormat: @"2"];
> 
>   }
> 
>   [result setStringValue: number1];
> 
> }
> 
> 
> 
> All the others number buttons are implemented in the same way...
> 
> 
> 
> The problem is in Equal button:
> 
> 
> 
> - (void) Equal: (id)sender{
> 
>   switch(operation){
> 
>     case '+':
> 
>     //trying  to do this: number1 = number2 + number1 and then print
> in the display result
> 
>     [number1 setDoubleValue: [number2 doubleValue] + [number1
> doubleValue]];
> 
>       [result setStringValue: number1];
> 
>       break;
> 
>    }
> 
> }
> 
> 
> 
> I already have created the window in Gorm, so everything is
> implemented there. The problem in this code is in the number1 =
> number2 + number1 in Equal method, I don't know why it's not assigning
> to number1 the operation correctly and then result working correctly.
> Any idea about what I'm doing wrong? Everything?
> 
> 
> 
> Sorry my english. Thanks.
> _______________________________________________
> Discuss-gnustep mailing list
> Discuss-gnustep@gnu.org
> https://lists.gnu.org/mailman/listinfo/discuss-gnustep





reply via email to

[Prev in Thread] Current Thread [Next in Thread]