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 03-30-2009, 04:39 AM   #1 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 11
cocoa is on a distinguished road
Exclamation iPhone random number game error

This is some code which is supposed to program a simple iPhone application which generates a random number, the user must guess the correct number and the program will give it the appropriate feedback. The main code itself seems to compile succesfully in C++ (we changed the file extension to main.mm because of this) however, we only started working on the GUI after we had finished the main code.
The only error we have received is the one indicated in red text regarding the randomly generated number in the ViewController.h file. Any suggestions as to how to solve this problem? Is there anything else which would prohibit the code from functioning seamlessy together in a working iPhone application?
We are honestly beginners and would appreciate any help whatsoever.



main.mm:

#import <UIKit/UIKit.h>
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
int main()
{
//Seed random number generator
srand(time(0));
//Creates the random number between 1 and 10
int theNumber = rand() % 10 + 1;
int guess;
//Guessing loop
int tries;
while (guess != theNumber)
{
cout<<"Please guess the number!: ";
cin>>guess;
++tries;
if (guess > theNumber)
cout<<"Too high,Guess again:"<< endl << endl;
if (guess < theNumber)
cout << "Too low,Guess again: "<< endl << endl;
}

cout << endl << "Congrats!! You got it" //<<tries<< "guesses."
<< endl;
return 0;
}

RandomnumberViewController.m:

#import "Randomnumber6ViewController.h"

@implementation Randomnumber6ViewController

@synthesize txtNumber,lblPleaseguess;

-(IBAction) updateTextid) sender {
NSString *text;
if([srand(time(0)] == int guess) {
text = @"Congrats!! You got it";
}
else
{
if (guess > theNumber) text = [[NSString alloc] initWithFormat:@"Too high,Guess again:"
if (guess < theNumber) text = [[NSString alloc] initWithFormat:@"Too low,Guess again:"
}

lblPleaseguess.text = text;

[text release];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}


- (void)dealloc {
[super dealloc];
}

@end

RandomnumberViewController.h:

#import <UIKit/UIKit.h>

@interface Randomnumber6ViewController : UIViewController {
IBOutlet UITextField *txtNumber;
IBOutlet UILabel *lblPleaseguess;

}

@property(nonatomic,retain) IBOutlet UITextField *txtNumber;
@property(nonatomic,retain) IBOutlet UILabel *lblPleaseguess;
- (IBAction) updateTextid) sender;

@end
cocoa is offline   Reply With Quote
Old 04-01-2009, 12:09 AM   #2 (permalink)
bfk
Registered Member
 
Join Date: Mar 2009
Posts: 8
bfk is on a distinguished road
Default

The line giving you trouble makes no sense.
You seem to be declaring a variable named guess inside an if conditional test, even if that worked the value would be uninitialised and could be anything. You're also comparing the uninitialised value for guess with the void return from srand().

You should be comparing the randomly generated number with what the user entered, but that's not what's happening here.

I assume the user is entering a number in a text field.

You could use a delegate for the NSTextField and put your checking code in then UITextFieldTextDidChangeNotification notification.
Then notification.object.text.intValue will give you what the user entered, if it matches the random number you generated and stored somewhere then tell the user he's very clever and generate the next random number and start again.

I also don't see any code to generate the random numbers. You also need an ivar to store the correct answer so that you can test against the user's input.

Hope this helps,
Brian
bfk is offline   Reply With Quote
Old 04-01-2009, 12:25 AM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Cocoa, You're probably going to have to start with the cocoa "hello world" examples and, once oyu understand it, adapt it to what you need.

Your program as written won't work because:
(1)cout and cin don't work on the phone
(2)you can't use a while loop to wait for input in an event-driven system
(3)You need to do your work in the viewController, not in main()

This particular line is busted in two ways:
Code:
if([srand(time(0)] == int guess) {
(4)the srand function seeds the random number generator; it doesn't produce a number.
(5)"int guess" would declare a new "guess" variable. You want to compare to what the user chose instead.

You probably want to compare your stored random number to [sender.text intValue]

There's no shortcut for learning this stuff.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-01-2009, 02:18 AM   #4 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 77
shonday is on a distinguished road
Default

It seems like you took your C++ code and tried to transplant it to Objective C. That's a really bad way to write a program, let alone learn the language. You should start from scratch...you'll pick it up pretty quickly because of your knowledge of C++.
shonday is offline   Reply With Quote
Old 04-22-2009, 07:52 AM   #5 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 3
pmaxjenkins is on a distinguished road
Default Random Number

This is the way I have been generating a random number for a game I'm working on. The code blow creates a random number between 1 and 7. Change the 7 to what ever number you like.

randomNo = (arc4random() %7);
pmaxjenkins is offline   Reply With Quote
Old 04-22-2009, 10:06 AM   #6 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 174
will.ray is on a distinguished road
Default

smasher is right... you basically wrote this program as a console app, so it is kind of a square peg/round whole situation from the beginning....

in addition the line in red is wrong. int guess should already be declared and assigned to something before you use it in an 'if' statement.
will.ray is offline   Reply With Quote
Old 04-22-2009, 01:48 PM   #7 (permalink)
Registered Member
 
Naughty_Ottsel's Avatar
 
Join Date: Aug 2008
Location: Gillingham, Dorset, UK
Age: 19
Posts: 218
Naughty_Ottsel is on a distinguished road
Send a message via MSN to Naughty_Ottsel
Default

Like they've all said, this is a C++ console app, trust me I've done it and extended it, if you want any help I'll happily do so, you might want to check out my app - iGuess, it's essentially the best guessing app on the app store so far, and is about to get better!
__________________
Follow me on Twitter
Naughty_Ottsel is offline   Reply With Quote
Old 04-22-2009, 02:00 PM   #8 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 97
fordracerguy is on a distinguished road
Default

wow. Random number generator game? I made one of those on my TI-85 in math class once 12 years ago. lol.
fordracerguy is offline   Reply With Quote
Old 04-22-2009, 02:14 PM   #9 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 174
will.ray is on a distinguished road
Default

Quote:
Originally Posted by fordracerguy View Post
wow. Random number generator game? I made one of those on my TI-85 in math class once 12 years ago. lol.
I guess you have to start somewhere... lol...
will.ray is offline   Reply With Quote
Reply

Bookmarks

Tags
error, game, iphone, number, random

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: 343
5 members and 338 guests
blueorb, guusleijsten, Kryckter, LEARN2MAKE, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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