Thread: help pls~

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    help pls~

    Code:
    #include<stdio.h>
    #define SENTINEL -1
    
    int rand();                /*function for random dice*/
    void srand();               /*function for random dice*/
    int first_throw();          /*function for first throw*/
    int firstthrowresult();     /*function for first throw result*/
    int second_throw();         /*function for second throw and its result*/
    
    int roll;           /*a variable to control the dice roll*/
    int roll_1;         /*roll dice 1*/
    int roll_2;         /*roll dice 2*/
    int sumofdices1;    /*first sum of dice roll*/
    int sumofdices2;    /*consecutive sum of dice roll*/
    int points;         /*the new total points*/
    
    srand(time(NULL));
    
    int main ()
    {
        printf("Enter Your Points Balance (-1 To Exit): ");
        scanf("%d", &points);
        
        while ( points != SENTINEL)
        {
         
            if ( points >= 80 )
            {printf("\nGreat! Lets Get Started Shall We?");
                 
                printf("\nKey in Any Number to Roll the Dice : ");
                scanf("%d", &roll);
                
                if(roll != SENTINEL)
                {
                first_throw ();
                printf("\nSum of Dice is %d", sumofdices1);
                firstthrowresult(); 
                }
                else
                    printf("Eh? Going Already? Come Back Soon~\n");
            } 
            else 
            printf ("Sorry, Insufficient Points To Play A Game\n");
        }
            
        
            if (points == SENTINEL)
             printf("Eh? Going Already? Come Back Soon~\n");
        
         return 0;
         
     }
        
    
    int first_throw()
    {
        
            roll_1 = rand() %6+1;
            roll_2 = rand() %6+1;
        
            sumofdices1 = roll_1 + roll_2;
        
        return (sumofdices1);
    
        
    }
    
    int firstthrowresult()
    {
        if( sumofdices1 == 2 || sumofdices1 == 7 || sumofdices1 == 12)
        {
            printf("\nWay Too Lucky! Those Dice Have To Be Loaded!");
            points = points + 20;
            printf("\nCongrats!!You Wins!!\nYour Points now is %d", points);
        }else if ( sumofdices1 == 6 || sumofdices1 == 8 || sumofdices1 == 10)
        {
            printf("\nHaha! Don't Give Up Now...Try Again!!");
            points = points - 10;
            printf("\nYou Loses~\nYour Points now is %d", points);
        }else 
        {  
            second_throw ();
        }
        return 0;
    }
    
    
    int second_throw()
    {
        roll_1 = rand() %6+1;
        roll_2 = rand() %6+1;
        
        sumofdices2 = roll_1 + roll_2;
            
        
        while ( sumofdices2 != sumofdices1)
        {
            printf("\nSum of Dice is %d", sumofdices2);
            printf("\nDon't Worry\nTake A Chance~\n");
            roll_1 = rand() %6+1;
            roll_2 = rand() %6+1;
        
            sumofdices2 = roll_1 + roll_2;
            
        }
        
        if ( sumofdices2 == 6 || sumofdices2 == 8 || sumofdices2 == 10)
        {
            printf("Haha! Don't Give Up Now...Try Again!!");
            points = points - 10;
            printf("\nYou Loses~\nYour Points now is %d", points);
            
        }else if ( sumofdices2 == 1 || sumofdices2 == 3 || sumofdices2 == 4 || sumofdices2 == 5 || sumofdices2 == 9 || sumofdices2 == 11)
        {
            printf("\nHoho~ Not Bad\n");
            points = points + sumofdices2;
            printf("Your Points Now is %d", points);
        }
        return 0;
    }
    something wrong wit my code as it keep repeating "Sorry, Insufficient Points To Play A Game" when the points in enter less than 80.(infinite loops) and any ideas how to make it prompt the user to key in any num to cont roll the dice or exit the program by entering the sentinel? i try something at de line 33 but it seems like not working.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to "catch" the return value of some of those functions you have.

    sum=firstThrowResult();
    sum+=secondThrowResult();

    Sum is "catching" the value returned by firstThrowResult(). That's the kind of thing you need to be doing on functions that return results that you need.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    err....right not de prob i facing is de infinite loop once de points i enter is less den 80 and when de points got deducted till less den 80....once it is below 80 de statement "sorry Insufficient point to play a game" keep repeating till none stop...

    *sry if i don get yr points....juz started programming class =="

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed