# C Program Not Properly Calculating

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 09-18-2011
GReaper
Quote:

Originally Posted by andynov123
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. :mad: Here we go again...
• 09-18-2011
CommonTater
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;   }```
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12