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; I'm writing a slot machine program where you are supposed to start with 1000 quarters but for some reason its ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    10

    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;
             
             
             
             }
    Last edited by andynov123; 09-18-2011 at 12:23 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    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.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    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.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    You're adding winnings to quarters multiple times, both in main() and in slot1() slot2() slot3()
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    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;
             
             
             
             }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    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 )
    Last edited by GReaper; 09-18-2011 at 12:52 PM.
    Devoted my life to programming...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GReaper View Post
    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();

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by andynov123 View Post
    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;
      }
    Last edited by CommonTater; 09-18-2011 at 03:20 PM.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    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;
             
             
             
             }
    Last edited by andynov123; 09-18-2011 at 01:17 PM.

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    You need to:
    Code:
    #include <windows.h>
    Assuming that you use Windows, of course.
    Devoted my life to programming...

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sleep is a Windows API call... to use it you must #include <Windows.h>

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    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;
          
             }

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    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...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by andynov123 View Post
    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
    Devoted my life to programming...

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    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;
          
             }
    Attached Images Attached Images  
    Last edited by andynov123; 09-18-2011 at 01:49 PM.

Page 1 of 2 12 LastLast
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