Amazon.com Widgets Car program
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 09:47:13 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Help!!!
| | |-+  Car program
Pages: [1]   Go Down
Print
Author Topic: Car program  (Read 378 times)
nautolian
Newbie
*
Posts: 6






« on: November 10, 2009, 01:34:56 PM »

Hi,
To test my skills I'm attempting to write a program to just display text on how far you have gone and the amount of gas you have as if you were driving a car. W would be the drive button. I'm getting a bunch of errors from the code that i have so far, and also, I'm not really sure how to make the number change for the amount of gas. Please help
What I would want the program to look like is:

Amount of Gas Left: [changing amount based on distance driven]
Miles Driven: [changing amount based on distance]

If you press w you go, if you release it you stop. It's basically a simple counter so far.

This is the code i have so far:

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface car : NSObject
{
char input;
float g;
float miles;
}

-(float) calculateGas;
-(char) setC;
-(float) g;
-(float) miles:
-(char) input;


@end

@implementation car

-(float) g
{
return g;
}

-(float) miles
{
return miles;
}

-(char) input
{
return input;
}

-(char) setC;
{
char c;
input = c;
scanf ("%c", c);
return c;
}

-(float) calculateGas;
{
char c;
if (g != 0)
{
while (scanf ("%c", &c) = 'w')
{
++miles;
g -= (miles * .03);
}
}
else
{
NSLog (@"Sorry you don't have any more gas, you're screwed...");
}

return g, miles;
}

@end
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "carFunc.h"

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

NSLog(@"Welcome to the car program. To drive, press w...");
[myCar setC];

//check for input
if (myCarc = 'w')
{
[myCar calculateGas];
NSLog (@"You have driven %f miles", miles);
NSLog (@"You have %f gallons of gas left", %f);
}

else
{
[Wasn't sure how to make it return to check again for input]
}


[myCar release];
    [pool drain];
    return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: November 10, 2009, 02:00:33 PM »

I don't mean to sound discouraging, but there are many errors in this program.   Can you tell me how far you have read in the text?  I would suggest you start by answering the exercises at the end of the chapters first, as they're more structured and follow more closely the examples from the chapters.   

Cheers,

Steve Kochan
Logged
nautolian
Newbie
*
Posts: 6






« Reply #2 on: November 16, 2009, 12:58:49 PM »

Thanks for the advice, I thought maybe I could just dive right in, but I guess I'll just work my way up.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.