Variable Subtraction help

Discussion in 'Mac Programming' started by gizabo, Apr 8, 2009.

  1. macrumors regular

    Joined:
    Jul 20, 2008
    #1
    This is so weird... I would think this would work... but it just doesnt....

    Anybody understand why?

    Code:
    //
    //  AppController.m
    //  Right N' Wrong
    //
    //  Created by Daniel Kindler on 4/4/09.
    //  Copyright 2009 Stdio.H. All rights reserved.
    //
    
    #import "AppController.h"
    @implementation AppController
    
    - (void) updateScore {
    	double percentScore = 100.0 * varRight / (varWrong + varRight);
    	[percentCorrect setStringValue:[NSString stringWithFormat:@"%.1f%%", percentScore]];
    }
    
    - (void)correctAns {
    	[numberRight setStringValue:[NSString stringWithFormat:@"%i Correct", varRight]];
    }
    
    -(void)wrongAns {
    	[numberWrong setStringValue:[NSString stringWithFormat:@"%i Incorrect", varWrong]];
    }
    
    
    - (IBAction)reset:(id)sender; {
    	varRight = 0;
    	varWrong = 0;
    	[self wrongAns];
    	[self correctAns];
    	[percentCorrect setStringValue:[NSString stringWithFormat:@"0.0%%"]];
    }
    
    
    - (IBAction)right:(id)sender; {
    	varRight++;
    	[self correctAns];
    	[self updateScore];
    }
    
    - (IBAction)wrong:(id)sender; {
    	varWrong++;
    	[self wrongAns];
    	[self updateScore];
    }
    
    - (IBAction)subOneRight:(id)sender {
    	varRight - 1;
    	[self correctAns];
    	[self updateScore];
    }
    
    
    - (IBAction)subOneWrong:(id)sender {
    	varWrong - 1;
    	[self wrongAns];
    	[self updateScore];
    
    }
    
    @end
    


    Do you see the part at the end were it says:
    Code:
    - (IBAction)subOneRight:(id)sender {
    	varRight - 1;
    	[self correctAns];
    	[self updateScore];
    }
    
    
    - (IBAction)subOneWrong:(id)sender {
    	varWrong - 1;
    	[self wrongAns];
    	[self updateScore];
    
    }
    

    why doesnt that work... Im trying to get varRight and varWrong to get subtracted by one... Why wont it work?
     
  2. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #2
    You need to do any one of the following:

    Code:
    
    someVar--;
    // or 
    someVar -= 1;
    // or
    someVar = someVar - 1;
    
    
     
  3. macrumors 6502a

    Joined:
    Oct 13, 2007
    #3
    You are just subtracting the value, but not storing it. You would need something like this:

    Code:
    varRight = varRight--;
    or
    Code:
    varRight = varRight -1;
     
  4. thread starter macrumors regular

    Joined:
    Jul 20, 2008
    #4
    thanks... And why doesnt this work?

    Code:
    - (IBAction)subOneRight:(id)sender {
    	for (varRight >= 0 ) {
    		varRight--;
    		[self correctAns];
    		[self updateScore];
    	}
    }
    
     
  5. thread starter macrumors regular

    Joined:
    Jul 20, 2008
    #5
    nvm fixed it.. thanks
     
  6. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    Did you mean to create a for loop there?

    I really think you would be doing yourself a huge favor if you learned some basic C (or even Python or Ruby or something) before you try to get too deep into Obj-C and Cocoa.

    There's a lot to be said for learning to walk before you run, you know?
     
  7. macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    This is rather bad style. It's more suited for a "guess what this does" puzzle.
     

Share This Page