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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 10:46 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
Default int question

So i'm trying to figure out why this isn't working correctly and i keep getting a passing argument 1 of setText error. if anyone could please look at it i'm sure its something stupid.
Code:
#import "Controller.h"

@implementation Controller

- (void)awakeFromNib
{
	srand ( time(NULL) );
	randomNum = rand() % 100;
	guessOutput.text = [NSString stringWithFormat:@"%i", randomNum];
	[userGuess becomeFirstResponder];
}

- (IBAction)guess:(id)sender;
{
	userGuess.text = num1;
	
	if (num1 = randomNum);
	{
		guessOutput.text = @"You Guessed Right";
	}
	
	if (num1 > randomNum);
	{
		guessOutput.text = @"Too Low";
	}
	
	if (num1 < randomNum);
	{
		guessOutput.text = @"Too High";
	}
	
	
}


@end
thavok is offline   Reply With Quote
Old 02-09-2010, 10:59 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Is num1 an int? userGuess.text or [userGuess setText: ] expects a string, not an int. You'll have to use stringWithFormat like you did when setting the text of guessOutput.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-09-2010, 11:15 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
Default

Sorry I'm really new at this i tried

Code:
- (IBAction)guess:(id)sender;
{
	[NSstring stringWithFormat:@"%i", num1];
	
	if (num1 = randomNum);
	{
		guessOutput.text = @"You Guessed Right";
	}
	
	if (num1 > randomNum);
	{
		guessOutput.text = @"Too Low";
	}
	
	if (num1 < randomNum);
	{
		guessOutput.text = @"Too High";
	}
	
	
}
num1 is an int, i'm wondering if there is some way to do it without num1 like compare userGuess vs randomNum
thavok is offline   Reply With Quote
Old 02-10-2010, 12:31 AM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Sorry, I thought you were trying to set userGuess.text . Are you trying to get the number from userGuess.text into num1? Then you want this:

Code:
num1 = [userGuess.text intValue];
Also, you want to use if (num1 == randomNum) , not if (num1 = randomNum) . using two == tests a value, using a single = changes a value.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-10-2010, 07:23 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
Default

So they syntax seems to be right now but it keeps reverting to the last if statement, the only reason I make it display the random number at first is so I know what the value is so I can test it, and no matter if I guess right on, to low or to high it tells me to high.

and thank you so much for your help I was stumbling through this all day yesterday to get frustrated by the int value vs the string. he is what i have so far.

Also I just added a guessCount so it could count how many tries it takes by setting it to 0 in the awake from nib and then adding one to the int value everytime they hit the guess button. how would i get that to display with a string the you guessed right statement

Code:
#import "Controller.h"

@implementation Controller

- (void)awakeFromNib
{
	srand ( time(NULL) );
	randomNum = rand() % 100;
	// This line will be taken out when I know my if statements work properly
        guessOutput.text = [NSString stringWithFormat:@"%i", randomNum];
	
        [userGuess becomeFirstResponder];
	guessCount = 0;
}

- (IBAction)guess:(id)sender;
{
	num1 = [userGuess.text intValue];
	guessCount = guessCount + 1;
	
	if (num1 == randomNum);
	{
		// I want to add the guess count after your guessed right
		guessOutput.text = @"You Guessed Right";
		
	}
	
	if (num1 > randomNum);
	{
		guessOutput.text = @"Too Low";
	}
	
	if (num1 < randomNum);
	{
		guessOutput.text = @"Too High";
	}
	
	
}


@end

Last edited by thavok; 02-10-2010 at 07:32 AM. Reason: updating code
thavok is offline   Reply With Quote
Old 02-10-2010, 08:59 AM   #6 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

You've got semicolons after your if statements, they shouldn't be there. What's happening is that each of your if statements is controlling an empty statement created by that stray semicolon rather than the compound statement in braces you want it to control.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 02-10-2010, 09:31 AM   #7 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
Default

Stray ; it works!!! thanks you guys for all your info, sorry for all the silly questions.

as far as the guess count goes I though something like this might work but it doesn't do i need to create another label to display the counter.

Code:
	if (num1 == randomNum)
	{
		guessOutput.text = @"You Guessed Right", @",%i", guessCount;
	}
and one last question if i add a reset button do I just create an action to reset and pick a new random number like

- (IBAction)resetid)sender;
// guess count to zero and pick new random number?

ps thanks for all the help so far guys just bought some new apps to try out :-)
thavok is offline   Reply With Quote
Old 02-10-2010, 09:42 AM   #8 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Quote:
Originally Posted by thavok View Post
as far as the guess count goes I though something like this might work but it doesn't do i need to create another label to display the counter.
I think you want to use stringWithFormat, like this:

Code:
if (num1 == randomNum)
	{
		guessOutput.text = [NSString stringWithFormat: @"You Guessed Right %d", guessCount];
	}
Quote:
and one last question if i add a reset button do I just create an action to reset and pick a new random number like
That sounds about right, yeah.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 02-10-2010, 11:30 AM   #9 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
Default

Thank you again it works like a charm, sorry this is my first solo app, i've been going through some books but with only a background in assembly and basic objective c is taking a bit to learn.
thavok is offline   Reply With Quote
Reply

Bookmarks

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: 265
20 members and 245 guests
2WeeksToGo, @sandris, AdamL, ADY, BrianSlick, Dani77, diyora, F_Bryant, GHuebner, HDshot, headkaze, mer10, Oral B, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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