# C Program Not Properly Calculating

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-18-2011
andynov123
C Program Not Properly Calculating
I'm writing a slot machine program where you are supposed to start with 1000 quarters but for some reason its counting down from some random number higher than the quarters=1000; I declared. The program is supposed to run till you run out of quarters. Can someone shed light on my problem?
The first machine pays 25 quarters every 33th time it is played;
the second machine pays 75 quarters every 99th time it is played;
the third pays 7 quarters every 9th time it is played.

Code:

```#include <stdio.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 ); //timesPlayed++;         /*        printf ("\nSlot Machine Two\n");                 printf("Press 1 to Insert Quarter\n");                 scanf ("%d", &input2);                 printf("Sorry You Lose\n");         //        winnings = .25*75;                 //quarters=quarterTotal-99;         //        total=.25*quarterTotal;                 if (input1==99){                         printf ("You've Won 75 Quarters on machine 1!\nYou've won \$%.2lf\n", winnings);                 printf ("You Have %.2lf quarters left which equals \$%.2lf\n\n", quarters, total);                 }                 printf ("\nSlot Machine Three");                 printf("\nPress 1 to Insert Quarter\n");                 scanf ("%d", &input3);                 printf("Sorry You Lose\n");                         quarters=quarterTotal-9;                 total=.25*quarterTotal;                 if (input1==9){                         printf ("You've Won 7 Quarters on machine 1!\nYou've won \$%.2lf\n", winnings);                         printf ("You Have %.2lf quarters left which equals \$%.2lf", quarters, total);                         system ("pause");                 }                 */               printf ("You have %d quarters left\n", quarters);                 } //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 %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 %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(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+=25;         printf ("You have %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(quarters));     winnings= 25;         }                 return winnings;         }   double convertQuarters (int quarters)   {         double result;         result=quarters*.25;                         return result;                                 }```
• 09-18-2011
stahta01
Code:

```double convertQuarters (int quarters)   {         int result;         result=quarters*.25;                         return result;                                 }```
Declare result to be double if you are returning double.

Suggest: you learn to use code tags and post your updated code in this thread; you are then likely to get more replies.

I will not be able to help you any/much more today.

Tim S.
• 09-18-2011
andynov123
Thanks for the suggestion.
Changing result to a double didn't do anything in regards to starting quarter countdown at 1000 like it was set to be.
• 09-18-2011
GReaper
You're adding winnings to quarters multiple times, both in main() and in slot1() slot2() slot3()
• 09-18-2011
andynov123
I removed a bunch of commented out code and winnings =25, winnings =75 and winnings =7. Not sure if this is what you meant but it countdowns the quarters at 196 now. Is that what you meant? Code still needs to countdown from 1000 quarters.

Code:

```#include <stdio.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 );               printf ("You have %d quarters left\n", quarters);                 } //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 %d quarters left\n", quarters);                   }                 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 %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(quarters));           }                 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+=25;         printf ("You have %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(quarters));         }                 return winnings;         }   double convertQuarters (int quarters)   {         double result;         result=quarters*.25;                         return result;                                 }```
• 09-18-2011
GReaper
You know what?! It does count from 1000, but it prints so fast down the console that you lose most of the output. Try putting a Sleep(50) for Windows or an equivalent for your OS to view the results more clearly. ( To have time to view them )
• 09-18-2011
CommonTater
Quote:

Originally Posted by GReaper
You know what?! It does count from 1000, but it prints so fast down the console that you lose most of the output. Try putting a Sleep(50) for Windows or an equivalent linux to view the results more clearly. ( To have time to view them )

Or, for mor authenticity...
Code:

```printf("Press ENTER to play again..."); getchar();```
• 09-18-2011
CommonTater
Quote:

Originally Posted by andynov123
Code:

```#include <stdio.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 );               printf ("You have %d quarters left\n", quarters);                 } //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 %d quarters left\n", quarters);                   }                 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 %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(quarters));           }                 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+=25;         printf ("You have %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(quarters));         }                 return winnings;         }   double convertQuarters (int quarters)   {         double result;         result=quarters*.25;                         return result;                                 }```

Just by compacting the code to remove the random vertical whitespaces and cleaning up your indentiations, it became easy to spot and correct several inconsistencies... such as your functions all returned 0... because you were returning winnings but never actually calculated the winnings and your functions were actually all acting directly upon the variable quarters from your main(), instead of the value passed into the function. And, of course the missing prototype for slot3().

My point is that readability of source code greatly enhances one's ability to troubleshoot and modify...

Code:

```#include <stdio.h> int slot1(int, int  ); int slot2(int, int  ); int slot3(int,int); double convertQuarters (int); int main (void)   {     int timesPlayed = 0;     int input3;     double x;     int coins = 1000;  // start with 1000 quarters     while(coins)       {         coins = slot1(timesPlayed++, --coins );         coins = slot2(timesPlayed++, --coins );                coins = slot3(timesPlayed++, --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;   }```
• 09-18-2011
andynov123
I've never used the sleep function before. When I place in my program I just return errors. Is this the wrong place for the function after
"printf ("You have %d quarters left\n", quarters);"?
And I added winnings as well.
Code:

```#include <stdio.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 );             printf ("You have %d quarters left\n", 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 %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 %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(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 %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(quarters));   winnings= 7;         }                 return winnings;         }   double convertQuarters (int quarters)   {         double result;         result=quarters*.25;                         return result;                                 }```
• 09-18-2011
GReaper
You need to:
Code:

`#include <windows.h>`
Assuming that you use Windows, of course.
• 09-18-2011
CommonTater
Sleep is a Windows API call... to use it you must #include <Windows.h>
• 09-18-2011
andynov123
Oh Ok. Well I got the sleep function working now, but now I'm starting with more than 1000 quarters instead of a 1000 quarters.
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 );             printf ("You have %d quarters left\n", 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 %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 %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(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 %d quarters left\n\n", quarters);         printf ("You have %.2lf quarters left\n", convertQuarters(quarters));   winnings= 7;         }                 return winnings;         }   double convertQuarters (int quarters)   {         double result;         result=quarters*.25;                         return result;               }```
• 09-18-2011
iMalc
What's with the overly generous amounts of whitespace? Are you trying to tire out my scrolling finger?

How about a one line gap between functions though...
• 09-18-2011
GReaper
Quote:

Originally Posted by andynov123
Oh Ok. Well I got the sleep function working now, but now I'm starting with more than 1000 quarters instead of a 1000 quarters.

Yes because 0 % 33 == 0, meaning quarters = 999 + 25 = 1024
• 09-18-2011
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;               }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last