Thread: False statement evaluation?

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    False statement evaluation?

    Hi, I have the following source:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    	srand(time(NULL));
    	int userNumber, rouletteNumber, bet, temp, tempChar;
    
    	enum betType {byNumber, byOddEven};
    	enum oddEven {odd, even};
    
    	typedef enum betType betType;
    	typedef enum oddEven oddEven;
    
    	betType typeOfGame;
    	oddEven oddOrEven;
    
    	while(1==1)
    	{
    		//system("clear");
    		printf("\nHow much would you like to bet? ");
    		scanf("%d", &bet);
    
    		printf("\nEnter 1 to bet by odd/even, or 2 to bet by number: ");
    		scanf("%i", &temp);
    		
    		//spin roulette
    		rouletteNumber = rand() % 35;
    	
    		typeOfGame = (temp == 1) ? byOddEven : byNumber;
    		switch(typeOfGame)
    		{
    			case byNumber:
    				printf("\nEnter your number: ");
    				scanf("%d",&userNumber);
    
    				printf("\nThe roulette spun a %i",
    					rouletteNumber);
    
    				if (rouletteNumber == userNumber)
    					printf("\nYou win $%i", bet * 35);
    				else
    					printf("\nYou lose");
    				break;
    
    			case byOddEven:
    				printf("\nEnter 1 for odd, 2 for even: ");
    				scanf("%i", &temp);
    				oddOrEven = (temp == 1) ? odd : even;
    
    				printf("\nThe roulette spun a %i",
    						rouletteNumber);
    
    				switch(oddOrEven)
    				{
    					case odd:
    						if (rouletteNumber%2 != 0)
    							printf("\nYou win $%i",
    								bet*2);
    						else
    							printf("\nYou lose");
    						break;
    					case even:
    						if (rouletteNumber%2 == 0)
    							printf("\nYou win $%i",
    								bet*2);
    						else
    							printf("\nYou lose");	
    				}//end inner switch
    		}//end outter switch
    		printf("\nWould you like to quit? Press y or n : ");
    
    		scanf("%c", &tempChar);
    
    		if ((tempChar == 'Y') || (tempChar == 'y'))
    			exit(0);
    	}//end while
    
    	return 0;
    }
    Why does the bold statement always evaluate to false? Even when I type a 'y' at runtime.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    just place a getchar() above the scanf function. To clear the input buffer. You should be fine

    Code:
    getchar();
    scanf("&#37;c", &tempChar);
    ssharish

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It might be an idea to input to a char, rather than an int too.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And perhaps it would be better to use getchar function to read a char rather than crapy scanf function.

    ssharish

  5. #5
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    lol oops.. didnt see i had that defined as an int

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Also, you should come up with a better way to control your program flow.
    Code:
    do
    {
    ...
    } while (tempChar != 'y');
    From a psychological standpoint, I think most people are more used to being asked 'Do you want to play again' rather than 'Do you want to quit'?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the chess project
    By Hussain Hani in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 05-28-2007, 02:33 AM
  2. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM