Thread: Some errors and warnings i dont understand

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    Some errors and warnings i dont understand

    Ok ive tried to create a simple craps game, yet there are some errors and warnings i cant figure out.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int money=200;
    
    void menu();
    void rules();
    initbet();
    roll();
    secbet();
    char secroll(int);
    
    void main()
    {
    	srand((int)time(NULL));
    
    	printf("---------------------------\n");
    	printf("WELCOME TO THE CRAPS TABLE\n");
    	printf("---------------------------\n");
    
    	menu();
    }
    
    void menu()
    {
    	int bet;
    	int backbet;
    	int point;
    	int gain;
    	char choose;
    	char win;
    
    	do
    	{
    		printf("Would you like to see the rules? y/n.\n\n");
    		fflush(stdin);
    		scanf("%c\n",&choose);
    	}
    	while(choose!='y' && choose!='n');
    	if(choose=='y')
    		rules();
    
    	while(1)
    	{
    		bet = initbet();
    		point = roll();
    		
    		switch(point)
    		{
    		case 7:
    		case 11:
    			printf("Congrats! You've won %i!\n",bet);
    			money+=bet;
    			point=1;
    			break;
    		case 2:
    		case 3:
    		case 12:
    			printf("Sorry, you lose %i!\n\n",bet);
    			money-=bet;
    			point=1;
    			break;
    		default:
    			break;
    		}
    
    		if(money<= 0)
    		{
    			printf("Sorry you lost,but now you have no money! Goodbye.\n\n");
    			break;
    		}
    		if(point==1)
    		{
    			do
    			{
    				printf("Would you like to play again?y/n.\n");
    				fflush(stdin);
    				scanf("%c\n",&choose);
    			}
    			while(choose != 'y' && choose != 'n');
    
    			if(choose=='y')
    				continue;
    			else if(choose=='n')
    				break;
    		}
    
    		printf("Your point roll is %i\n\n",point);
    
    		backbet = secbet(bet);
    
    		win = secroll(point);
    
    		if(win==0)
    		{
    			printf("Seven! Sorry, you lost $%i.\n\n",bet+backbet);
    			money=money-(bet+backbet);
    			
    			if(money<=0)
    			{
    				printf("Sorry you dont have the bills\n\n");
    				break;
    			}
    
    			if(point=1)
    			{
    				do
    				{
    					printf("Do you want to play again? y/n\n");
    					fflush(stdin);
    					scanf("%c\n",&choose);
    				}
    				while(choose != 'y' && choose != 'n');
    				if(choose=='y')
    					continue;
    				else if(choose=='n')
    					break;
    			}
    		}
    		else
    		{
    			printf("Congrats! You Win!\n");
    			switch(point)
    			{
    			case 6:
    			case 8:
    				gain=(int) (bet+(1.2*backbet));
    				printf("Here is a 6:5 pay! Thats $%i!\n\n",gain);
    				money+=gain;
    				break;
    			case 5:
    			case 9:
    				gain=(int) (bet+(1.5*backbet));
    				printf("Here is a 3:2 pay! Thats $%i!\n\n",gain);
    				money+=gain;
    				break;
    			case 4:
    			case 10:
    				gain=(int) (bet+(2*backbet));
    				printf("Here is a 2:1 pay! Thats $%i!\n\n",gain);
    				money+=gain;
    				break;
    			default:
    				printf("If this appears dana, youre screwed");
    				break;
    			}
    
    			do
    			{
    				printf("Do you want to play again? y/n.\n");
    				fflush(stdin);
    				scanf("%c\n",&choose);
    			}
    			while(choose!='y' && choose!='n');
    			if(choose=='y')
    				continue;
    			if(choose=='n')
    				break;
    		}
    }
    
    
    
    void rules();
    {
    	printf("rules\n");
    }
    
    
    
    initbet()
    {
    	int bet;
    
    	printf("\nYou have $%i.\n",money);
    	printf("Place your bet between 0 and 200 dollars\n");
    
    	fflush(stdin);
    	scanf("%i\n",&bet);
    
    	while(bet > money || bet < 0)
    	{
    		if(bet>money)
    			printf("Get outta here...you only have $%i! Bet again please\n",money);
    		else
    			printf("Bet again...loser");
    
    		fflush(stdin);
    		scanf("%i\n",&bet);
    
    		if(bet==0)
    			printf("You wimp...you bet nothing, but youre special, you can still play!\n");
    		else if(bet==money)
    			printf("Goin for it eh?Good luck, youll need it!\n");	
    	}
    
    }
    
    
    
    roll();
    {
    	int die1;
    	int die2;
    	int sum;
    
    	die1=rand()%6+1;
    	die2=rand()%6+1;
    	sum=die1+die2;
    
    	printf("\nRolling in progress...\nyou rolled a %i\n",sum);
    	return sum;
    }
    
    secbet(int bet);                                                 /*3*/
    {
    	int backbet;
    	int high;
    
    	high= 2*bet;
    	if((money-bet) < high)
    		high = (money-bet);
    	printf("Place your backbet...\nIf you do not wish to make a backbet,enter 0\n");
    	printf("You can bet up to %i\n",high);
    
    	fflush(stdin);
    	scanf("%i\n",&backbet);
    
    	while(backbet>high || backbet<0)
    	{
    		if(backbet>high)
    			printf("No more than %i dollars please\n",high);
    		else
    			printf("Please type a valid bet\n");
    
    		fflush(stdin);
    		scanf("%i\n",&backbet);
    	}
    		return backbet;
    	
    }
    
    char secroll(int point,int sum)                  /*2*/
    {
    	int sum;
    	char win;
    	do
    	{
    		sum = roll();             /*1*/
    	}
    	while(sum!=point && sum!=7);
    
    	if(sum==point)
    		win = 1;
    	else
    		win = 0;
    	return win;
    
    
    }
    ok it is saying a bunch of stuff: (look above to see where it is)
    1. error C2065: 'sum' : undeclared identifier
    I dont understand cause i just declared it.
    2.error C2143: syntax error : missing ';' before 'type'
    It says this in numerous places and i dont understand why...
    3.error C2059: syntax error : ')'

    If anyone could fix or help me understand my errors, I would appreciate it a lot. Or if you see anything else that is sketchy. Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    1) In secroll(), it is not good to declare int sum while your input is called sum as well.

    2) I am not sure if you can declare variable before the funciton prototypes.

    3) missing; - usually when you miss a single ; or put wrong braces in one place, you will get lots of errors.

    Hope this helps

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    ok thank you

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Champion
    1) In secroll(), it is not good to declare int sum while your input is called sum as well.

    2) I am not sure if you can declare variable before the funciton prototypes.

    3) missing; - usually when you miss a single ; or put wrong braces in one place, you will get lots of errors.

    Hope this helps
    1) Yeah, that's a 'BadThing(TM)' in C.
    2) Yes, you can declare function prototypes and global variables any place you feel like. They're global so it doesn't matter where they're declared really, except for the fact that they need to be declared before you actually use them. This is valid code:
    Code:
    #include stuff
    
    void func1( void );
    int var1;
    void func2( void );
    int var 2;
    
    int main( void )
    ...
    3) Yeah, those really suck. What's even worse is a missing quotation mark.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    ok on to question two
    Code:
    initbet(int money)
    {
    	int bet;
    
    	printf("\nYou have $%i.\n",money);
    	printf("Place your bet\n");
    	fflush(stdin);
    	scanf("%i\n",&bet);
    
    	
    	do
    	{
    		initbet(money);
    	}
    	while(bet > money || bet < 0);
    
    	do
    	{
    		roll(bet);
    	}
    	while(bet>=0 && bet<=money);
    }
    whenever I run the program i insert my bet into a scanf yet when i press enter nothing happens. I have to press another key to make the rest of the program to happen. What is happening? did I do something wrong with my scanf? Thanks in advance

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It could be because fflush is not made for input streams!!!

    fflush(stdin); == bad
    while(fgetc(stdin)!='\n'); == good


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by lakai02
    Code:
    	scanf("%i\n",&bet);
    whenever I run the program i insert my bet into a scanf yet when i press enter nothing happens. I have to press another key to make the rest of the program to happen. What is happening? did I do something wrong with my scanf? Thanks in advance
    See Question 12.17.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I fix these annoying warnings and errors?
    By zyphirr in forum C Programming
    Replies: 18
    Last Post: 11-12-2008, 06:19 PM
  2. Replies: 6
    Last Post: 11-06-2008, 04:06 AM
  3. Definitions for gcc errors and warnings?
    By cpjust in forum C Programming
    Replies: 12
    Last Post: 10-04-2007, 09:18 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM