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

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
I am just starting xcode, and am fiddling with basic c, such as performing algebraic math equations. I already wrote a program which runs in terminal and shows conversion between Fahrenheit and Celsius, from 0 to 200. That works fine. Now, without guidance, I am writing the code for the so complex equation of y = mx + b. Whenever I run this, it only shows the output for 51 as x to 105 y! Here's the script:

Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        int xscope, yscope;
	int slope = 2;
	int intercept = 3;
	for (xscope = 0; xscope <= 50; xscope = xscope + 1);
	{
		yscope = (slope * xscope) + intercept;
		NSLog(@"%10.2i -> %10.2i", xscope, yscope);
	}
    [pool drain];
    return 0;
}
Thank you!
 
All your variables are of type int. Perhaps you want double or float.

If you use double or float types, you must also change the format string of NSLog, otherwise the i conversion-specifier won't match the actual type, and you'll get garbage output. Refer to the String Format Specifiers document for the correct conversion.
 
Nope, still not working. Changed everything to the float, added the .0s just in case, and changed the is to fs. Still the same output.
 
Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        int xscope, yscope;
	int slope = 2;
	int intercept = 3;
	for (xscope = 0; xscope <= 50; xscope = xscope + 1)[COLOR="Red"][SIZE="4"];[/SIZE][/COLOR]
	{
		yscope = (slope * xscope) + intercept;
		NSLog(@"%10.2i -> %10.2i", xscope, yscope);
	}
    [pool drain];
    return 0;
}
Misplaced semicolon hilited above in red. This effectively defines an empty body for the 'for' loop. Remove semicolon to fix.

Sorry for earlier misdirection. My net connection is flaky.
 
Remove the ; after the for ). That's gotten us all. The code in the block only runs once, with your variables in the state after the loop. Your loop body is an empty statement, so nothing happens each iteration.

Int is fine here.

-Lee

Balls, slow phone typing.
 
Wow! Thanks! Now that I look at my previous temp converter code, that makes sense. Just gonna get comfortable with this kind of stuff before moving on to any kind of GUI whatsoever.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.