PDA

View Full Version : Variable Subtraction help




gizabo
Apr 8, 2009, 10:36 PM
This is so weird... I would think this would work... but it just doesnt....

Anybody understand why?


//
// 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:

- (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?



eddietr
Apr 8, 2009, 10:52 PM
why doesnt that work... Im trying to get varRight and varWrong to get subtracted by one... Why wont it work?

You need to do any one of the following:



someVar--;
// or
someVar -= 1;
// or
someVar = someVar - 1;

larkost
Apr 8, 2009, 10:52 PM
varRight - 1;

varWrong - 1;

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

You are just subtracting the value, but not storing it. You would need something like this:

varRight = varRight--;
or
varRight = varRight -1;

gizabo
Apr 8, 2009, 11:23 PM
thanks... And why doesnt this work?


- (IBAction)subOneRight:(id)sender {
for (varRight >= 0 ) {
varRight--;
[self correctAns];
[self updateScore];
}
}

gizabo
Apr 8, 2009, 11:25 PM
nvm fixed it.. thanks

eddietr
Apr 8, 2009, 11:28 PM
thanks... And why doesnt this work?


- (IBAction)subOneRight:(id)sender {
for (varRight >= 0 ) {
varRight--;
[self correctAns];
[self updateScore];
}
}


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?

Sander
Apr 9, 2009, 02:41 AM
varRight = varRight--;


This is rather bad style. It's more suited for a "guess what this does" puzzle.