Number generator

Discussion in 'Mac Programming' started by dbwrobel, Nov 24, 2010.

  1. dbwrobel, Nov 24, 2010
    Last edited: Nov 24, 2010

    dbwrobel Guest

    May 11, 2010
    Hi there, I'm a newbie and trying to go through a tutorial question that I can't seem to get. The book I'm using did a random number generator/guessing game and I rewrote it using my own understanding of the code. However, I can't figure out why the code runs twice after the initial run. That is, the first time it runs, if you hit y to run it again, it outputs to the console as though it has gone through twice each time.

    I have attached my code below; I apologize in advanced if I have made any silly mistakes:

    #import <Foundation/Foundation.h>

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

    int randomNumber;
    int guessNumber;
    char playAgain = ' ';
    BOOL looper = TRUE;

    NSLog(@"Welcome to the random number generator!");

    while (looper)

    randomNumber = (random()%101);
    NSLog(@"The random number is %d",randomNumber);
    /*NSLog(@"Guess a number:");

    /*if (guessNumber > randomNumber)
    NSLog(@"Your guess was too high, guess again:");

    /*if (guessNumber < randomNumber)
    NSLog(@"Your guess was too low, guess again:");


    NSLog(@"Play again?");

    if (playAgain == 'n')
    looper = FALSE;


    [pool drain];
    return 0;

    Also, forgot to add that when I uncomment the code at the top of the main while loop, I can no longer input a character for scanf("%c",&playAgain); the code just runs right through.
  2. ShortCutMan macrumors member

    Aug 8, 2005
    It looks like when you scan the input, it is also picking up the newline character. So after the first iteration (reading the 'y' character to playAgain) it then gets to scanf and reads in the newline character and loops through again. You probably need to read in the entire input and check the first character only.
  3. ytk macrumors regular

    Jul 8, 2010
    I'm not sure if you're saying you're a newbie to Objective-C, or a programming newbie. If you're in fact new to programming, stop what you're doing right now and start by learning a language that's more forgiving and less unwieldy than Obj-C. I suggest Ruby, but Python has its adherents as well and either one will be much easier to learn to program with, and far more useful to you than Obj-C.

    Any C based language is going to force you to learn to deal with managing the internal resources of the computer far more than it is reasonable to expect of a beginner. It's sorta like learning to fly on a 747. But don't be fooled into thinking higher level languages like Ruby or Python are less "powerful" than Obj-C. Believe it or not, you can write full blown Mac GUI applications strictly in Ruby if you so choose--I've written several myself. What's more, you'll be more productive as you spend more time writing code and less time chasing down obscure bugs such as the one you're facing here. Once you are comfortable with the concepts of programming, you will have a much easier time delving deeper into the bowels of the system to understand how things actually work at the lower levels, which is largely where Obj-C operates.

    My advice would be to learn Ruby first, then C. Straight up plain old C, not Obj-C, C++, C#, or pretend-C (Java). With those two under your belt, you will understand 95% of Obj-C, and you will understand programming and computers far better than if you'd just tried to dive head on into Obj-C.

    I know you were just asking for help with your code and not a rant. :D But hearing about a "newbie" trying to learn Obj-C just sets me off for the same reason that hearing about "introductory" programming classes being taught in C++ makes my blood boil. There may be no better way to turn people off of programming than by giving them the impression that you have to understand C++ to be a programmer.
  4. gnasher729 macrumors G5


    Nov 25, 2005
    You are attacking the problem from the wrong angle. Something happens that you don't understand. The thing that probably works differently from what you think is scanf. Instead of trying to fix your program, write some code that enables you to understand _exactly_ what scanf ("%d") and scanf ("%c") does. Without understanding that, you can't fix your code. When you understand it, it should be easy.
  5. lostp macrumors newbie


    Nov 23, 2010
    Bangalore, India
    Well since you mentioned you were a newbie, why dont you try debugging? you will learn more that way. I wont tell you what's wrong with your code, but I will tell you a simple way of debugging(without using a debugger). Just output the currently read character as integer to know where you're going wrong. Also use getc(stdin) instead of scanf("%c") or even getchar() and test again.
  6. dbwrobel thread starter Guest

    May 11, 2010
    Thanks for your help everyone; my situation is that I had taken a course on procedural things in C back many years ago when I was in my first year of undergrad :p ... since then I had been playing around with Python here and there as well as MATLAB. I was now looking to get some knowledge/know-how of an object oriented language to be able to work on some apps for the Mac.

    I guess I'll just go back and review those basics before diving into Objective-C as well as look into Ruby and Python a bit more.
  7. Rodimus Prime macrumors G4

    Rodimus Prime

    Oct 9, 2006
    something I do when tracking down the bug is after every scanf line print out what it reading.
    you will be surprised at how much easier that makes debugging.

    I now on a project I did for my Java class I was having issues but then told the computer to show me the output it was reading in and then comparing it with. That showed me what the issue was really quickly and from there I was able to fix the problem with very little trouble and it beat pulling my hair out trying to track it down.

    if some one look at the some of the stuff I code for class during debugging phases they would see tons of random printf or system.out.println statements all over the place some comment out others active. Reason they are there is I will be trying to track down a bug and seeing were the data is going wrong that or they are in there to confirm something is working correctly.

    Now I go back and remove that junk as I am done tracking down bugs but always do that. Also putting break points in the code is also very helpful because then you can have it go one step at a time and make sure everything is working. It really depends on what you are doing to see which is better for you at the time. print statements are quick but not always useful for larger chunks of data as it dumps out way to much. I use it for smaller things. Break points are generally a slower but better for tracking things.
    In this case print statements would be the way to go.

Share This Page