Xcode help, im very new

Discussion in 'Mac Programming' started by kabz, Feb 2, 2013.

  1. macrumors newbie

    Joined:
    Feb 2, 2013
    #1
    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
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Can you post the code you have that isn't working?

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

    B
     
  3. kabz, Feb 3, 2013
    Last edited by a moderator: Feb 3, 2013

    thread starter macrumors newbie

    Joined:
    Feb 2, 2013
    #3
    #
    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...
     
  4. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    What do you expect to happen?
    What is happening?
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     
  6. kabz, Feb 4, 2013
    Last edited by a moderator: Feb 4, 2013

    thread starter macrumors newbie

    Joined:
    Feb 2, 2013
    #6
    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.
     
  7. balamw, Feb 4, 2013
    Last edited: Feb 4, 2013

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
     
  8. macrumors member

    Joined:
    May 5, 2011
    Location:
    twilight zone
    #8
    forget Xcode

    use make or cmake and build it with vi/bash

    use Komodo if you really need "colors" for text
     
  9. macrumors 6502a

    xShane

    Joined:
    Nov 2, 2012
    Location:
    United States
    #9
    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.
     
  10. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #10
    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.
     

Share This Page