5 Replies - 2121 Views - Last Post: 22 August 2012 - 08:33 PM

#1 cbrei2310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-August 12

Not sure why this is breaking

Posted 22 August 2012 - 05:35 AM

I have a very simple program, which was working fine but would only run once. I have been trying to add a loop to keep running the program until the user says they no longer want to. The program runs through once, but when asked if the user would like to roll again, regardless of input t is returning to the beginning of the while statement and does not continue.

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        
        int sides;   //Variable used to store the number of sides on the dice
        BOOL rollOn = TRUE;   //Holds a true or false value which controls the continue roll option
        char keepRolling = 'n';   //Variable used when asking the user wether they would to keep rolling
        
        while (rollOn == TRUE)
        {
        NSLog(@"How many sides does your dice have?");   //Asks user how many sides the dice has
        scanf("%i", &sides);   //Accepts input from keyboard and stores it into variable sides
        
        int roll = arc4random() % sides + 1;  //Returns a random value based on how many sides the dice has
        
        NSLog(@"You rolled a %i", roll);   //Prints your roll to the screen
        
    
        NSLog(@"Would you like to roll another dice?  y / n");  //Asks user if they want to keep rolling
            
            keepRolling =fgetc(stdin);
            
            if (keepRolling == 'n' || keepRolling == 'N')
            {
                rollOn = FALSE;
            }
            else
                rollOn = TRUE;
        }
    }
    return 0;
}



Should I be using a different kind of look for this, or is my code just wrong?

Sorry, this is in the wrong forum, I didn't notice the Objective-C subforum, could a mod move this.

Is This A Good Question/Topic? 0
  • +

Replies To: Not sure why this is breaking

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Not sure why this is breaking

Posted 22 August 2012 - 06:00 AM

Moved to Objective-C
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Not sure why this is breaking

Posted 22 August 2012 - 06:19 AM

scanf("%i" will leave any trailing whitespace in the input stream. So if the user hits enter after inputting the number (which he has to in order for the program to continue), there will be a '\n' left in the buffer. Your subsequent call to fgetc will return that '\n' without reading any input.

The easiest fix would be to replace your call to fgetc with another call to scanf with " %c" as the format specifier. This will skip any leading whitespace and consume input until it encounters a non-whitespace character.
Was This Post Helpful? 0
  • +
  • -

#4 cbrei2310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-August 12

Re: Not sure why this is breaking

Posted 22 August 2012 - 10:53 AM

Thanks,

I thought that fgetc would clear out the buffer... is there a way i can clear the buffer, The code is still breaking after asking if the user wants to roll again. It looks like /n is being entered in as the answer to that question.

Could I have the program output what remains in the buffer to confirm this?

Thanks again.

Also, can you suggest any good resources or books for someone who is completly new to Objective-C?
Was This Post Helpful? 0
  • +
  • -

#5 cbrei2310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-August 12

Re: Not sure why this is breaking

Posted 22 August 2012 - 12:15 PM

GOT IT!!!

Thanks for the help.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        
        int sides;   //Variable used to store the number of sides on the dice
        BOOL rollOn = TRUE;
        char keepRolling = ' ';
        
        while (rollOn == TRUE)
        {
        
            NSLog(@"How many sides does your dice have?"); 
            scanf("%i", &sides);   
        
            int roll = arc4random() % sides + 1;  
            NSLog(@"You rolled a %i", roll);   
            
            getchar();
            
            NSLog(@"Would you like to roll again? y / n");
            scanf("%c", &keepRolling);
            
            if (keepRolling == 'y'|| keepRolling == 'Y')
                rollOn = TRUE;
            else if (keepRolling =='n' || keepRolling == 'N')
                rollOn = FALSE;
            else
            {
                NSLog(@"invalid answer, Goodbye");
                exit(0);
            }
        }
        NSLog(@"Goodbye");
        
    }
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Not sure why this is breaking

Posted 22 August 2012 - 08:33 PM

Note the difference between " %c" (which discards leading whitespace) and "%c" (which doesn't). If you use " %c", you don't need the call to getchar to discard the '\n' (and it will also work if there should be other whitespace in the buffer).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1