Objective C help

15 posts

Flag Post

So I tried making a calculator… but it doesn’t seem to work. I have NO idea why. The way it’s acting, is it let’s me input the first number (a float), then skips OVER the operator (a char), and then lets me input the second number (another float), which results in an error… I think the problem is something with me using scanf(), maybe the fact that the operator is a char as well? It’s a 6 function calculator that i made mainly to test switch, scanf(), and to make a relatively basic program. I’m just wondering if someone can tell me whats wrong… Thanks a lot if you can help!!!

PS. What’s the difference from printf("") and NSLog(@"")? All i noticed was the time stamp, which was kind of bothering me when i was testing this so i changed to printf("").

Posting the code didn’t work with formatting and such… so use this link to see it http://snipt.org/zNgj5

 
Flag Post

What are the compiler errors?

 
Flag Post

The program successfully builds. basically it SHOULD work like this…
It’ll say,

Welcome to this basic calculator! Choose your numbers and operations
NOTE – Accepts +, -, *, /, ^, and r for root (index in front, radicand after):

Pick first number…
(then you pick a number)

Operator…
(for some reason this is written, but it skips the step for me to input the operator. this causes the problem)

Another number….
(It let’s me input this too)

Then you should get the answer. but because the operator isn’t inputted, I get an “ERROR” message. I actually just realized it’s because in the switch, i made the default send the message “ERROR” haha. So there’s no actually crash in the program. It just seems to skip the step to let me input the operator. because operator is the only char variable, I wonder if that has to do with it…?

 
Flag Post

Maybe you should change switch(operator) to switch(&operator). Unfortunately, I don’t know objective-C, though it looks close to C and C++.

 
Flag Post

no that didn’t work. but thanks anyways

 
Flag Post

From this page on StackOverflow (I vaguely recalled something about this, but I haven’t touched C in roughly 20 years):

When you read keyboard input with scanf(), the input is read after enter is pressed but the newline generated by the enter key is not consumed by the call to scanf(). That means the next time you read from standard input there will be a newline waiting for you (which will make the next scanf() call return instantly with no data).

To avoid this, you can modify your code to something like:
scanf(“%c%*c”, &currentGuess);
The %*c matches a single character, but the asterisk indicates that the character will not be stored anywhere. This has the effect of consuming the newline character generated by the enter key so that the next time you call scanf() you are starting with an empty input buffer.

Caveat: If the user presses two keys and then presses enter, scanf() will return the first keystroke, eat the second, and leave the newline for the next input call. Quirks like this are one reason why scanf() and friends are avoided by many programmers.

 
Flag Post

so what could be an alternate way to have an input instead of “scanf()”. I assume there’re reasons similar to this as to why you’d use NSLog as opposed to printf()

 
Flag Post

You want a really easy way to get your programme to work? Just copy the scanf statement for the char and paste it again underneath. The linefeed will be consumed by the first char scanf, and then your input will go into the second, overwriting the value in the variable will the intended value.

As to an actual answer to your question, I don’t know. As I said, I haven’t touched it in 20 years, so I’d have to look up the answer, which you could do just as easily (and should do, since it is your project).

Addition:
From what I recall, it was an issue of different systems behaving differently: Some would send CR/LF pairs for Enter, others would only return CR. So the scanf would actually consume the CR, but leave the LF (if any). I believe this is why scanf for other values appears unaffected.

 
Flag Post

Thank you! The program is working now… for the most part. I turned it into a loop with two paths basically. you can keep doing computations with the answer, or you can clear everything and start over with a switch of ‘_’ which should set the result back to 0, and skip a few lines to mimic clearing the page. for some reason, this ends up turning into some kind of endless loop…. any ideas?

here’s the current code:

http://snipt.org/zOr4

 
Flag Post

fpurge seems to clear the buffer:

http://stackoverflow.com/questions/12024503/scanf-is-not-waiting-for-input-within-my-loop

Originally posted by 0ii0:

here’s the current code:

http://snipt.org/zOr4

 
Flag Post

So im gonna guess that means goto is bad…………..

 
Flag Post
Originally posted by 0ii0:

So im gonna guess that means goto is bad…………..

Yes.

If people try to talk you into believing there’s something good in it, it’s like pretending that the raptor’s ignorance whether you’re still alive while ripping your body apart is a welcome extension of your lifetime.

 
Flag Post

Why exactly is it bad? cause it seems very convenient in many respects….

 
Flag Post

There are already many answers online.

Why don’t you use Google next time you have a common question that already has a very long history.

(I found all of those links simply by typing “Why is goto bad?” in Google. It took less than a second!)

 
Flag Post
Originally posted by 0ii0:

Why exactly is it bad? cause it seems very convenient in many respects….

I cannot see any convenience in being devoured by a raptor.

Before this becomes entirely off topic, please tell us if fpurge solved your problem.