Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-21-2011, 05:50 PM   #1 (permalink)
Registered Member
 
Mr.Abe90's Avatar
 
Join Date: Sep 2010
Posts: 45
Mr.Abe90 is on a distinguished road
Default Counter Problem

I have this simple number counter app "+" to add one, "-" to subtract one.
My problem is when I press the "+" button the number will subtract by one, then if i press it again it will begin to work like it's supposed to.

Here is my code:
Code:
int count = 0;

-(void)awakeFromNib {
	
	current_number.text = @"0";
	
}

- (IBAction)add1 {
	
	if(count >= 99) return;
	
	NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
	current_number.text = numValue;
	[numValue release];
}

- (IBAction)subtract1 {
	
	if(count <= -99) return;
	
	NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count--];
	current_number.text = numValue;
	[numValue release];	
}
What's the problem?
Mr.Abe90 is offline   Reply With Quote
Old 02-21-2011, 06:12 PM   #2 (permalink)
Crafty Veteran
 
Magic Hands's Avatar
 
Join Date: Nov 2008
Location: Memphis, TN
Age: 23
Posts: 436
Magic Hands is on a distinguished road
Send a message via Skype™ to Magic Hands
Default

Simple fix would be to add a case for if count == 0. Set count = 1.
__________________
Magic Hands is offline   Reply With Quote
Old 02-21-2011, 07:06 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
lgehrig1 is on a distinguished road
Default

You are using postfix notation.

Code:
int i = 0 ; // i = 0
int j = i ++ ; // j = 0, i = 1
int k = ++ i ; // k = 2, i = 2
Read up on postfix operations
lgehrig1 is offline   Reply With Quote
Old 02-21-2011, 11:02 PM   #4 (permalink)
Registered Member
 
Mr.Abe90's Avatar
 
Join Date: Sep 2010
Posts: 45
Mr.Abe90 is on a distinguished road
Default

Quote:
Originally Posted by lgehrig1 View Post
You are using postfix notation.

Code:
int i = 0 ; // i = 0
int j = i ++ ; // j = 0, i = 1
int k = ++ i ; // k = 2, i = 2
Read up on postfix operations
Ok sorry it was someone else's code.
Mr.Abe90 is offline   Reply With Quote
Old 04-24-2011, 09:56 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 6
No_Loafing is on a distinguished road
Default Change

Change from "count++" and "count--" to "++count" and "--count".
No_Loafing is offline   Reply With Quote
Reply

Bookmarks

Tags
add, counter, subtract

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 382
16 members and 366 guests
Absentia, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0