|
|
#1 |
|
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 |
|
|
|
0
|
|
|
#2 |
|
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 |
|
|
|
0
|
|
|
#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... Last edited by balamw; Feb 3, 2013 at 07:25 AM. Reason: Fixed code tags |
|
|
|
0
|
|
|
#4 |
|
What do you expect to happen?
What is happening? |
|
|
|
0
|
|
|
#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 |
|
|
|
1
|
|
|
#6 | |
|
Quote:
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;
Last edited by balamw; Feb 4, 2013 at 07:24 AM. Reason: Code tags. |
||
|
|
0
|
|
|
#7 | |
|
The bolded bit looks like our profanity filter replaced something.
Quote:
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. |
||
|
|
0
|
|
|
#8 |
|
forget Xcode
use make or cmake and build it with vi/bash use Komodo if you really need "colors" for text |
|
|
|
0
|
|
|
#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.
|
|
|
|
0
|
|
|
#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.
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 09:32 AM.




)


Linear Mode
