C Program Not Properly Calculating

This is a discussion on C Program Not Properly Calculating within the C Programming forums, part of the General Programming Boards category; Originally Posted by andynov123 I cleaned up my code and ran it and one other problem is that I play ...

  1. #16
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by andynov123 View Post
    I cleaned up my code and ran it and one other problem is that I play 18 quarters at the end when I have 17 quarters left. See pic for reference
    And GReaper slot machine 1 is only supposed to pay out 25 quarters after it is played 33 times so why would it pay out 25 quarters at the beginning of the program?
    Here's Code updated

    Code:
    #include <stdio.h>
    #include <windows.h>
    int slot1(int, int  );
    int slot2(int, int  );
    double convertQuarters (int);
    
    
    void main(){
         
        int timesPlayed = 0;
        int timesPlayed2 = 0;
        int input3;
        double x;
        int quarters=1000;
        
        while(quarters>0) {
    //quarters--;
        quarters+= slot1(timesPlayed++, --quarters );
        quarters+= slot2(timesPlayed2++, --quarters );    
        quarters+= slot3(timesPlayed2++, --quarters );
              Sleep(50); 
            } //End While Loop
    system ("pause");
    }
    int slot1(int timesPlayed, int quarters)
    {
        int winnings=0;
        printf ("Slot Machine One\n");  
        if (timesPlayed%33==0){
        printf("You won on slot machine 1\n");
        quarters+=25;
        printf ("You have $%.2lf in quarters left\n", convertQuarters(quarters));
        printf ("You have %d quarters left\n", quarters);    
         winnings= 25;
        }    
        return winnings;    
    }
    int slot2(int timesPlayed, int quarters)
    {
        int winnings=0;
        printf ("Slot Machine Two\n");  
        if (timesPlayed%99==0){
        printf("You won on slot machine 2\n");
        quarters+=75;
            printf ("You have $%.2lf in quarters left\n", convertQuarters(quarters));
        printf ("You have %d quarters left\n", quarters);
         winnings= 75;
        }
        return winnings;
    }
    int slot3(int timesPlayed, int quarters)
    {
        int winnings=0;
        printf ("Slot Machine Three\n");  
        if (timesPlayed%9==0){
        printf("You won on slot machine 3\n");
        quarters+=7;
            printf ("You have $%.2lf in quarters left\n", convertQuarters(quarters));
        printf ("You have %d quarters left\n", quarters);
      winnings= 7;
        }    
        return winnings;    
    }
      double convertQuarters (int quarters)
      {
             double result;
             result=quarters*.25; 
             return result;
          
             }
    This seems like the code you posted at the beginning, with a few indenting fixed. Here we go again...
    Devoted my life to programming...

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... here's the code revision I did in message #8 with a little revision to stop you from starting with extra quarters...
    Also note that I've amalgamated the printf() calls from each function into Main() reducing your code size somewhat.
    I also corrected your broken main() function so it actually returns a value at the end.

    Code:
    int slot1(int, int  );
    int slot2(int, int  );
    int slot3(int,int);
    double convertQuarters (int);
    
    int main (void)
      {
         int rounds = 0;
         int input3;
         double x;
         int coins = 1000;  // start with 1000 quarters
    
         while(coins) 
           {
              ++rounds;
              coins = slot1(rounds, --coins );
              coins = slot2(rounds, --coins );	
              coins = slot3(rounds, --coins );
    
              printf ("You have %d quarters left\n", coins);
              printf ("Your winnings are $%.2lf\n", convertQuarters(coins));
    
              printf("Press Enter to play again..."
             getchar();
            }
        return 0;
    }
    
    
    int slot1(int timesPlayed, int quarters)
      {
          printf ("Slot Machine One\n");  
          if (! timesPlayed%33)
            { 
              printf("You won on slot machine 1\n");
              quarters+=25;
            }
        return quarters;
      }
    
    
    int slot2(int timesPlayed, int quarters)
      {
         printf ("Slot Machine Two\n");  
         if (! timesPlayed%99)
           {
              printf("You won on slot machine 2\n");
              quarters+=75;
           }
         return quarters;
      }
    
    
    int slot3(int timesPlayed, int quarters)
      {
         printf ("Slot Machine Three\n");  
         if (! timesPlayed%9)
          {
            printf("You won on slot machine 3\n");
            quarters+=25;
         }
       return quarters;
      }
    
    
    double convertQuarters (int quarters)
      {
        double result;
        return quarters*0.25;
      }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program properly
    By Richardcavell in forum C Programming
    Replies: 13
    Last Post: 02-23-2011, 02:55 PM
  2. why is math not calculating properly
    By kburyanek in forum C Programming
    Replies: 9
    Last Post: 10-15-2009, 07:46 AM
  3. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 09:59 PM
  4. Replies: 2
    Last Post: 11-10-2003, 08:12 PM
  5. plz hlp me. program not running properly
    By jfl in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21