Hi there, right now I'm working on a relatively simple program and I think I need a second pair of eyes on this because I can't see why its not working. Basically, the program is skipping the if part of the if statement, whether the statement is true or not, and going straight to the else statement. So here's my program (if your wondering, its a tweet shortener)
@interface:
@implementation
main:
I've looked at the Mac OS Dev Center (or whatever you call it) and the documentation there, and couldn't find anything that contradicts anything I'm doing here...so I'm not sure what the problem is. The program returns no errors or warnings, so I have no way of knowing what's wrong, although knowing myself its probably something obvious that I'm just missing. Thanks guys!! You are always so much help!
@interface:
Code:
#import <Foundation/Foundation.h>
@interface tweet : NSObject {
BOOL shorten;
NSMutableArray *longWords;
NSMutableArray *shortWords;
NSString *reply;
NSMutableString *shortTweet;
NSMutableString *usrTweet;
}
-(void) getTweet;
-(void) checkLength;
-(void) initObjects;
-(void) shorten: (NSMutableString *) s;
-(void) findAndReplace: (NSMutableString *) f;
@end
@implementation
Code:
#import "tweet.h"
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import "stdio.h"
@implementation tweet
-(void) getTweet {
char firstTweet[250];
fgets(firstTweet, 250, stdin);
if(firstTweet[strlen(firstTweet) - 1] == '\n') {
firstTweet[strlen(firstTweet) - 1] = 0;
}
usrTweet = [NSMutableString stringWithUTF8String: firstTweet];
[self checkLength];
}
-(void) checkLength {
if([usrTweet length] > 140) { //***skipping this***
int overflow = [usrTweet length] - 140;
NSLog(@"Your tweet is %i characters long, %i more than 140. At this point, you can choose to shorten your tweet with our built in shortener or publish as is.", [usrTweet length], overflow);
char responseC[20];
fgets(responseC, 20, stdin);
if(responseC[strlen(responseC) - 1] == '\n') {
responseC[strlen(responseC) - 1] = 0;
}
reply = [NSString stringWithUTF8String: responseC];
do{
if (reply = @"publish") {
shorten = NO;
NSLog(@"Your tweet is: %@", usrTweet);
return;
} else if (reply = @"shorten") {
shorten = YES;
[self shorten: usrTweet];
} else NSLog(@"Please enter either 'shorten' or 'publish' to continue");
} while( shorten != YES || NO );
} else { //***doing this***
NSLog(@"Your tweet is: %@", usrTweet);
return;
}
}
-(void) initObjects {
longWords = [NSMutableArray arrayWithObjects: @"because", @"for" , @"two", @"to", @"some", @"with", @"without", nil];
shortWords = [NSMutableArray arrayWithObjects: @"b/c", @"4", @"2", @"2", @"sum", @"w/", @"w/o", nil];
shortTweet = [NSMutableString stringWithString: usrTweet];
}
-(void) shorten: (NSMutableString *) s {
[self initObjects];
[self findAndReplace: shortTweet];
NSLog(@"Your shortened tweet is: %@", shortTweet);
}
-(void) findAndReplace: (NSMutableString *) f {
if([longWords count] != [shortWords count]) {
NSLog(@"Error: Arrays unequal. Terminating");
return;
}
int amount = [longWords count];
int i;
for(i = 0; i < amount; ++i) {
NSString *search = [NSString stringWithString: [longWords objectAtIndex: i]];
NSString *replace = [NSString stringWithString: [shortWords objectAtIndex: i]];
[shortTweet replaceOccurrencesOfString: search
withString: replace
options: 0
range: NSMakeRange (0, [shortTweet length])];
}
}
@end
main:
Code:
#import <Foundation/Foundation.h>
#import "tweet.h"
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
tweet *myTweet = [[tweet alloc]init];
[myTweet getTweet];
[myTweet release];
[pool drain];
return 0;
}
I've looked at the Mac OS Dev Center (or whatever you call it) and the documentation there, and couldn't find anything that contradicts anything I'm doing here...so I'm not sure what the problem is. The program returns no errors or warnings, so I have no way of knowing what's wrong, although knowing myself its probably something obvious that I'm just missing. Thanks guys!! You are always so much help!