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

animefx

macrumors regular
Original poster
May 10, 2005
157
0
Illinois
I'm pulling my hair out trying to understand why this isn't working, it runs without errors but something with my calculations isn't working out and I'm not able to figure out what. Also the for loop does not end. I have a textfield where the user enters a value, I convert it to an int, and then use that as the number of questions on a quiz. in the loop i display the number of questions wrong from 1 wrong all the way to the number in the text field and next to it a score. The score results in 0 no matter what :\

calculation for a score is number of questions correct / number of questions * 100.

Code:
- (IBAction)buttonTouched:(id)sender {
    
    int myValue = (int) myTextField.text; //convert myTextField to an int
    
    NSLog(@"Wrong      Score");
    
    for (int i = 1; i <= myValue; i++) {
        NSLog(@"%d          %d", i, (myValue - i) / (myValue) * 100);
    }
    
    
    [myTextField resignFirstResponder];
    
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
For what values of 'myValue'

I'm guessing really, really big ones.


This code is completely wrong:
Code:
    int myValue = (int) myTextField.text; //convert myTextField to an int
It doesn't do what the comment says. Well, it might be said to do it in an insane way, which is most definitely wrong.

The expression myTextField.text is an object reference. A pointer. A memory address. Casting a pointer to an int means "interpret this memory address as an integer". It does not mean "convert the text represented by this object to an integer".

So, what might be a likely result of interpreting a memory address as an integer? It obviously depends on the actual address, but something in the range of tens of thousands is quite possible. It would not surprise me if it were upwards of hundreds of thousands, or even millions. Because if you have 1 MB of memory, then you have the possibility of a memory address being any value up to a million. And if you have 512 MB of memory, well, good luck with that.


The correct way to convert the text of myTextField to an integer is to look up the reference doc for NSString, and look for a method that returns the numeric value as an integer. I recommend the keywords numeric and/or integer.

As to why the score is always zero: it's being performed on integers. I suggest working through some example calculations manually, say with myValue at 1000 and i at 10. Remember that every calculation or partial calculation will be an integer. What is the final result, as an integer?

I also recommend learning to use the debugger. If this code had been single-stepped thru, and the variables looked at, the insane value of myValue should have been evident. Learning to use the debugger is an important part of learning to program. Learn it early, use it often.
 
Last edited:

animefx

macrumors regular
Original poster
May 10, 2005
157
0
Illinois
Ok I understand. Thanks for explaining this to me. I'll have to look at the NSString docs to see what methods I need to use to, etc

I'm guessing really, really big ones.


This code is completely wrong:
Code:
    int myValue = (int) myTextField.text; //convert myTextField to an int
It doesn't do what the comment says. Well, it might be said to do it in an insane way, which is most definitely wrong.

The expression myTextField.text is an object reference. A pointer. A memory address. Casting a pointer to an int means "interpret this memory address as an integer". It does not mean "convert the text represented by this object to an integer".

So, what might be a likely result of interpreting a memory address as an integer? It obviously depends on the actual address, but something in the range of tens of thousands is quite possible. It would not surprise me if it were upwards of hundreds of thousands, or even millions. Because if you have 1 MB of memory, then you have the possibility of a memory address being any value up to a million. And if you have 512 MB of memory, well, good luck with that.


The correct way to convert the text of myTextField to an integer is to look up the reference doc for NSString, and look for a method that returns the numeric value as an integer. I recommend the keywords numeric and/or integer.

As to why the score is always zero: it's being performed on integers. I suggest working through some example calculations manually, say with myValue at 1000 and i at 10. Remember that every calculation or partial calculation will be an integer. What is the final result, as an integer?

I also recommend learning to use the debugger. If this code had been single-stepped thru, and the variables looked at, the insane value of myValue should have been evident. Learning to use the debugger is an important part of learning to program. Learn it early, use it often.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Ok I understand. Thanks for explaining this to me. I'll have to look at the NSString docs to see what methods I need to use to, etc

In one of your other threads, you already showed code that used a suitable method. The code within the body of the loop is wrong, but the approach to getting the numeric value of an NSString is right. It may not be as speedy as it could be, but the basic approach is correct.

Based partly on the code in that other thread, what other programming or scripting languages do you already know? The &&'s in that other loop body look like an attempt at AppleScript's string-concatenation operator (a single &). Unfortunately, Objective-C and AppleScript are worlds apart in syntax. And in the original post here, the expectation of a type-cast doing a conversions seems like an expectation for an "as <type>" expression in AppleScript.

Also, if you're learning from a book or tutorial, which one? (Title, author, edition, or tutorial URL.)

If you're working through an exercise from a book or tutorial, which one? (Chapter, page, exercise number.)
 

animefx

macrumors regular
Original poster
May 10, 2005
157
0
Illinois
I know basic, pascal, a moderate amount of visual basic, a moderate amount of c, and I'm learning objective-c now through a few books that I'm not thrilled about and some good video tutorials I found on YouTube from a young guy named Bucky. It's a bit hard to wrap my head around calculating the values of objects if I can't just assign them to int or float the traditional way.

I changed my code to work with the console only so the calculation works as does the loop. I was looking at the documentation and couldn't I just use NSString initwithformat %i and assign textfield.text to %i?

Here is the working code btw... I have to change myValue manually in the code of course. I assigned it as float variable so the calculation would result in a float for the score.

Code:
float myValue = 65;   //this will need to accept users input from a textfield
    
    NSLog(@"Wrong      Score");
    
    for (int i = 1; i <= myValue; i++) {
        NSLog(@"%i          %.3g", i, (myValue - i) / (myValue) * 100);
    }    
    
    
    [myTextField resignFirstResponder];

In one of your other threads, you already showed code that used a suitable method. The code within the body of the loop is wrong, but the approach to getting the numeric value of an NSString is right. It may not be as speedy as it could be, but the basic approach is correct.

Based partly on the code in that other thread, what other programming or scripting languages do you already know? The &&'s in that other loop body look like an attempt at AppleScript's string-concatenation operator (a single &). Unfortunately, Objective-C and AppleScript are worlds apart in syntax. And in the original post here, the expectation of a type-cast doing a conversions seems like an expectation for an "as <type>" expression in AppleScript.

Also, if you're learning from a book or tutorial, which one? (Title, author, edition, or tutorial URL.)

If you're working through an exercise from a book or tutorial, which one? (Chapter, page, exercise number.)
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
I know basic, pascal, a moderate amount of visual basic, a moderate amount of c, and I'm learning objective-c now through a few books that I'm not thrilled about and some good video tutorials I found on YouTube from a young guy named Bucky. It's a bit hard to wrap my head around calculating the values of objects if I can't just assign them to int or float the traditional way.

In C, are you familiar with structs? In Pascal, I think the equivalent is 'record'.

Objects, like structs, don't consist of a single value, what you call "the traditional way", but the technical term is "scalar". So, structs and objects are not scalar values. They are both composites: made of other constituents, some of which may be scalars, others which may be composites.

A struct is passive. It's data. It has data members. The operations (actions) are defined separately, in functions. In order to do things on a complex struct, you must call the correct functions on the struct.

An object is active. It has data members and function members (technical term is "methods"). Much of the useful functionality of an object is in its methods. For example, look at the NSString class. Every method would need to be a separate C function, even though the underlying data (the struct-like part) would be identical. In C, there's a whole library of functions for working on C strings: strlen(), strchr(), strrchr(), strcat(), strlcat(), strncat(), and zillions more. Read the C header file "string.h" to get an idea of the scope.

In Objective-C, and other object-oriented languages, that situation is inverted. Instead of many separate functions which take a particular data type, the functions are grouped along with the relevant data (i.e. the struct-like data part), into things called classes. A class then embodies both the actions and the relevant data. Each class is like an immensely expanded data-type, which you can use to make the actual things (the objects) you use in the program. The operations on the data-type are its methods.


That's the gist of object-oriented programming. You'll have to practice it before it becomes natural, because the procedural languages (C, Pascal, Basic, etc.) provide no inherent notion of object-ness.

You should read a book, maybe try several (e.g. a public library), and find the parts where the fundamentals of object-orientation are explained. It doesn't even have to be about Objective-C, since the OOP concepts are the same across OOP languages. An introduction to OOP in Java would cover the same principles, and almost certainly be easier to find a book for in a library.

Then do the exercises in the book. You can't learn OOP effectively simply by reading. You have to write programs with it.

Links you should read, even if you're also reading a book:
http://en.wikipedia.org/wiki/Object-oriented_programming
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/OOP_ObjC/
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.