Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kabz

macrumors newbie
Original poster
Feb 2, 2013
3
0
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
 
#
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 a moderator:
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
 
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 a moderator:
I dont know what insult and swearing at the user means??

Code:
            NSLog(@"\n\nY[B]**** you[/B].\nPress enter to continue.");
 [/QUOTE]

The bolded bit looks like our profanity filter replaced something.

[quote="lee1210, post: 16776462"]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.
[/QUOTE]

This. [B]kabz[/B] Take a few moments to read [URL="http://whathaveyoutried.com"]What Have You Tried?[/URL] and [URL="http://www.mikeash.com/getting_answers.html"]Getting Answers[/URL].

Having a quick look at [url]http://www.raywenderlich.com/[/url] 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
 
Last edited:
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.