Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 2, 2013, 05:48 PM   #1
kabz
macrumors newbie
 
Join Date: Feb 2013
Xcode help, im very new

Hi guys

Im trying to make something very simple. Im making a text game where you get a puzzle and the user have to type in a word answer.

I have created a stribg for what the user type in, and now i want to make my if statment to se if thats correct. but i cant get it to work, help)

----kabz
kabz is offline   0 Reply With Quote
Old Feb 2, 2013, 05:56 PM   #2
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Can you post the code you have that isn't working?

Use the # to enclose it in CODE tags...

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Feb 3, 2013, 06:12 AM   #3
kabz
Thread Starter
macrumors newbie
 
Join Date: Feb 2013
#
Code:
        char svar;
        scanf("%s", &svar);
        waitOnCR();
        svar = [NSString stringWithCString:svar encoding:NSASCIIStringEncoding];
        waitOnCR();
        
        //Answer 1
        
        NSRange range1 = [svar rangeOfString:@"hejhej"];
    
         if (range1.location != NSNotFound ) {
            NSLog(@"\n\nY**** you.\nPress enter to continue.");
             userIsAlive=false;
            waitOnCR();
#

----------

but im really not sure how to go about it...im so green...

Last edited by balamw; Feb 3, 2013 at 07:25 AM. Reason: Fixed code tags
kabz is offline   0 Reply With Quote
Old Feb 3, 2013, 09:17 AM   #4
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
What do you expect to happen?
What is happening?
ArtOfWarfare is offline   0 Reply With Quote
Old Feb 3, 2013, 09:46 AM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
svar is being used in a lot of exciting, incorrect ways. It's a single char, but you pass its address to scanf when it's expecting to read a string. This will smash memory. You then pass the value of svar to stringWithCString:encoding: which expects an address, specifically a char *. This will likely cause a crash. You then assign the result of a method that returns NSString * to svar, which is still a char. You then try to send a rangeOfString: message to svar, which should be sent to a NSString * (or at least an NSObject *). This is also likely to cause a crash. Your if condition seems OK, assuming you want to test that hejhej was contained in the result (if you've managed not to crash, though, I don't believe this will ever be true). Your NSLog seems fine (are you swearing at the user?). Is userIsAlive a bool or BOOL? The latter is more common, and YES and NO are the normal values used.

I can guess what waitOnCR does, but it seems to be used a bit too much.

Where are you learning from? What is your ultimate goal? Right now the gist seems to be to read from the console and, if the input contains hejhej, insult the user. You've got quite a ways to go. At the very least you need to read into a char[] and you need a new variable to store your NSString *. This isn't actually a complete function or program so there may be other bugs in code you didn't include.

Keep at it, and post your updated code. Please try to post code in at least self-contained functions if not complete programs. This allows others to test your code more easily. Also include what you want your code to do, what you think your current code should do, and what it actually does. If you have tried other approaches, details those and why you abandon them.

-Lee
lee1210 is offline   1 Reply With Quote
Old Feb 4, 2013, 07:12 AM   #6
kabz
Thread Starter
macrumors newbie
 
Join Date: Feb 2013
Quote:
Originally Posted by lee1210 View Post
I can guess what waitOnCR does, but it seems to be used a bit too much.

Where are you learning from? What is your ultimate goal? Right now the gist seems to be to read from the console and, if the input contains hejhej, insult the user. You've got quite a ways to go. At the very least you need to read into a char[] and you need a new variable to store your NSString *. This isn't actually a complete function or program so there may be other bugs in code you didn't include.

-Lee
Thank you so much for at least trying to get what i want.

im learning from http://www.raywenderlich.com/ witch is just t textbased games. My coal with this program is to give the user a puzzel and the user then type the answer and if it correct he moves on to the next puzzle....is that clear??


My userIsAlive is a BOOL...

I dont know what insult and swearing at the user means??

And with my char command i should specifie how many charecters it should log, so if it 10 with space i char something[11];??

----------

I have created a variable for NSString* svar

#
Code:
{
    int ans;
    BOOL userIsAlive;
    int score;
    int health;
    NSString* name;
    NSString* svar;
    
    
    
}

@property int ans;
@property BOOL userIsAlive;
@property int score;
@property int health;
@property (strong) NSString* name;
@property NSString* svar;
hope this clear it a little more up what it is thats happening.

Last edited by balamw; Feb 4, 2013 at 07:24 AM. Reason: Code tags.
kabz is offline   0 Reply With Quote
Old Feb 4, 2013, 07:40 AM   #7
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by kabz View Post
I dont know what insult and swearing at the user means??
Quote:
Originally Posted by kabz View Post
[CODE]
NSLog(@"\n\nY**** you.\nPress enter to continue.");
The bolded bit looks like our profanity filter replaced something.

Quote:
Originally Posted by lee1210 View Post
Please try to post code in at least self-contained functions if not complete programs. This allows others to test your code more easily. Also include what you want your code to do, what you think your current code should do, and what it actually does. If you have tried other approaches, details those and why you abandon them.
This. kabz Take a few moments to read What Have You Tried? and Getting Answers.

Having a quick look at http://www.raywenderlich.com/ gives me a clue as to why you think that these little code snippets might be enough for us to help you, but they really aren't.

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme

Last edited by balamw; Feb 4, 2013 at 07:45 AM.
balamw is offline   0 Reply With Quote
Old Feb 7, 2013, 08:12 AM   #8
the-wanderer
macrumors member
 
Join Date: May 2011
Location: twilight zone
forget Xcode

use make or cmake and build it with vi/bash

use Komodo if you really need "colors" for text
the-wanderer is offline   0 Reply With Quote
Old Feb 7, 2013, 10:21 AM   #9
xShane
macrumors 6502a
 
xShane's Avatar
 
Join Date: Nov 2012
Location: United States
Quote:
Originally Posted by the-wanderer View Post
forget Xcode

use make or cmake and build it with vi/bash

use Komodo if you really need "colors" for text
This doesn't help the poster at all. I also notice that you have hostile anger at Apple, insulting everything from GPUs in Apple, to CPUs, to Xcode now. Nice passive troll attempt.
__________________
Macbook Pro 15" 2.6, 8GB, 750GB, 1GB VRAM
"Everything for the people, nothing by the people."

"Be the change that you wish to see in the world."
xShane is offline   0 Reply With Quote
Old Feb 7, 2013, 10:35 AM   #10
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by xShane View Post
This doesn't help the poster at all. I also notice that you have hostile anger at Apple, insulting everything from GPUs in Apple, to CPUs, to Xcode now. Nice passive troll attempt.
I agree with him that the poster should drop Xcode and instead be using a text editor (I use gedit) coupled with a command line tool for compiling (I've been using clang recently.) Xcode is overkill for someone still writing such basic code.
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
The App Store app thinks Xcode is installed, but I deleted Xcode. moonman239 Mac Applications and Mac App Store 3 May 6, 2014 05:53 PM
Moving from xcode 3.2 to xcode 4.5.2 compilation error for 'abs' macro in stdlib.h hrishidev Mac Programming 7 Jun 2, 2013 10:36 AM
Xcode 4.5 iNeedAnsurs iPhone 0 Jan 25, 2013 02:49 PM
Xcode 3 vs Xcode 4 when dealing with Objective-C timebourne Mac Programming 8 Sep 23, 2012 05:36 PM
XCode 4.4.1 to Xcode 4.5 in Mountain Lion VinegarTasters OS X 10.8 Mountain Lion 2 Sep 19, 2012 08:55 PM

Forum Jump

All times are GMT -5. The time now is 02:16 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC