Thread: if array trouble...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    if array trouble...

    This is a simple roulette game...but I'm sure I've got multiple issues(still a beginner)...If any one would be so kind to point out my errors or suggest a better way, I would appreciate it.
    Most every thing is working but I'm stuck on determining if red or black is ture...
    I think my trouble is here: if(BetOnNum[ii]=="red"): see code


    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <iostream.h>
    #define N 38
    #define buff MAX_PATH
    #define COMNUMBER 50
    
    int main()
    {
    //////////////////////...var 
    int ii=0, j,ni,addup=0;
    char ans;
    char BetOnNum[50][7]={0};
    int betamnt[50]={0};
    int betting=TRUE;
    int R=0;
    int B=0;
    int E=0;
    int O=0;
    int RR=0;
    int BB=0;
    int EE=0;
    int OO=0;
    int OE=0;
    int NT=0;
    int FT=0;
    int ST=0;
    int TT=0;
    int F=0;
    int S=0;
    int T=0;
    /*
    char betnum[COMNUMBER][7]={"0","00","1","2","3",
    	"4","5","6","7","8","9","10","11","12",
    	"13","14","15","16","17","18","19","20","21","22","23",
    	"24","25","26","27","28","29","30","31","32","33",
    	"34","35","36","even","odd","red","black","1-18","19-36","1-12","2-12","3-12","1-2to1","2-2to1","3-2to1"};
    */
    ///////////////////////...var
    
    //////////////////////////////////////////////////...main
    system("color 70");///black on white
    
    Sleep(1000);
    /*
    Bet Odds 
    A Straight up any one number...........35 to 1 
    B Split any two-number split...........17 to 1 
    C 3 numbers street.....................11 to 1 
    D 4 numbers corner.....................8 to 1 
    E 5 numbers only with 0 and 00.........6 to 1 
    F 6 numbers sixline....................5 to 1 
    G Any column...........................2 to 1 
    H Any dozen............................2 to 1 
    I 1-18, 19-36..........................1 to 1 
    J Red, Black...........................1 to 1 
    K Even, Odd............................1 to 1 
    L Courtesy wager position for..........17 to 1
    guests to bet on 0 and 00  
    */
    	        cout<<"odds on 0 # bet......= 2.63158%....pays 35 to 1"<<endl;
    	        cout<<"odds on 00 # bet.....= 2.63158%....pays 35 to 1"<<endl;
    	        cout<<"odds on one # bet....= 2.63158%....pays 35 to 1"<<endl;
    	        cout<<"odds on green # bet..= 5.26316%....pays 17 to 1"<<endl;
    	        cout<<"odds on red # bet ...= 47.3683%....pays 1 to 1"<<endl;
    	        cout<<"odds on black # bet..= 47.3683%....pays 1 to 1"<<endl;
    	        cout<<"odds on even # bet...= 47.3683%....pays 1 to 1"<<endl;
    	        cout<<"odds on odd # bet....= 47.3683%....pays 1 to 1"<<endl;
    	        cout<<"odds on 1-18 # bet...= 47.3683%....pays 1 to 1"<<endl;
    	        cout<<"odds on 19-36 # bet..= 47.3683%....pays 1 to 1"<<endl;
    	        cout<<"odds on 1-12 # bet...= 31.5789%....pays 2 to 1"<<endl;
    	        cout<<"odds on 2-12 # bet...= 31.5789%....pays 2 to 1"<<endl;
    	        cout<<"odds on 3-12 # bet...= 31.5789%....pays 2 to 1"<<endl;
    	        cout<<"odds on any 2to1 bet.= 31.5789%....pays 2 to 1"<<endl;
    cout<<endl;
    cout<<endl;
    		while(betting){
    			ii++;
    	        cout<<"Place your bets!"<<endl;
    	        cout<<"Enter number to bet on: ";
    			cin>>BetOnNum[ii];
    	        cout<<"Enter amount of bet: ";
    			cin>>betamnt[ii];
    	        cout<<"Place another bet? (y/n): ";
    			cin>>ans;
    			if(ans=='n'){
    			betting=FALSE;
    			}
    		}
    		printf("\n");
    		ni=ii;
    
    		printf("You have bets on : ");
    		for (ii = 1; ii <= ni ; ii++){
    		printf(BetOnNum[ii]);
    		printf(",");
    		if(BetOnNum[ii]=="red"){
    		RR=1;//not getting here
    		}//if
    		if(BetOnNum[ii]=="black"){
    		BB=1;
    		}//if
    		if(BetOnNum[ii]=="even"){
    		EE=1;
    		}//if
    		if(BetOnNum[ii]=="odd"){
    		OO=1;
    		}//if
    		if(BetOnNum[ii]=="1-12"){
    		F=1;
    		}//if
    		if(BetOnNum[ii]=="2-12"){
    		S=1;
    		}//if
    		if(BetOnNum[ii]=="3-12"){
    		T=1;
    		}//if
    		if(BetOnNum[ii]=="1-18"){
    		OE=1;
    		}//if
    		if(BetOnNum[ii]=="19-36"){
    		NT=1;
    		}//if
    		if(BetOnNum[ii]=="1-2to1"){
    		FT=1;
    		}//if
    		if(BetOnNum[ii]=="2-2to1"){
    		ST=1;
    		}//if
    		if(BetOnNum[ii]=="3-2to1"){
    		TT=1;
    		}//if
    		}///for
    		printf("\n");
    
    		printf("Your cash at stake : ");
    
    		for (ii = 1; ii <= ni ; ii++){
    		addup=(addup+betamnt[ii]);
    		}
    		cout<<addup<<endl;
    		printf("\n");
    		printf("Good Luck!\n");
    			printf("%d \n", RR);//using to see value of RR
    ///////////////////////////////...calc odds
    
    ///////////////////////////////...calc odds
    
    /////////////////////////////////////////////...ball in motion
    
    		printf("Ball now in motion");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    Sleep(100);
    		printf(".");
    		printf("\n");
        /* Set evil seed (initial seed) */
        srand( (unsigned)time( NULL ) );
    
        for (ii = 0; ii < 1; ii++) {//start of number color loop
    		    rand();
                if(((j = (int) N * rand() / (RAND_MAX + 1.0))==37)){
    	    	system("color 72");///green on white
    			j=00;
    	        printf("00 green\n");
    			}
    		    if(j!=1 &&j!=3&&j!=5&&j!=7&&j!=9&&j!=2 &&j!=4&&j!=6&&j!=8&&j!=10&&j!=11&&
    			j!=13&&j!=15&&j!=17&&j!=20&&j!=22&&j!=24&&j!=26&&j!=28&&j!=29&&j!=31&&
    			j!=33&&j!=35&&j!=37&&j!=0){
    			system("color 74");///red on white
    			R=1;
    			printf("%d red\n", j);
    			}
    			if(j==1){
    			system("color 74");///red on white
    			R=1;
    			printf(" %d red\n", j);
    			}
    			if(j==3){
    			system("color 74");///red on white
    			R=1;
    			printf(" %d red\n", j);
    			}
    			if(j==5){
    			system("color 74");///red on white
    			R=1;
    			printf(" %d red\n", j);
    			}
    			if(j==7){
    			system("color 74");///red on white
    			R=1;
    			printf(" %d red\n", j);
    			}
    			if(j==9){
    			system("color 74");///red on white
    			R=1;
    			printf(" %d red\n", j);
    			}
    		    if(j==0){
    		    system("color 72");///green on white
    		    printf(" %d green\n", j);
    			}
    		    if(j!=2 &&j!=4&&j!=6&&j!=8&&j!=1 &&j!=3&&j!=5&&j!=7&&j!=9&&j!=12&&j!=14&&
    			j!=16&&j!=18&&j!=19&&j!=21&&j!=23&&j!=25&&j!=27&&j!=30&&j!=32&&j!=34&&
    			j!=36&&j!=37&&j!=0){
    			system("color 70");///black on white
    			B=1;
    			printf("%d black\n", j);	
    			}
    			if(j==2){
    			system("color 70");///black on white
    			B=1;
    			printf(" %d black\n", j);
    			}
    			if(j==4){
    			system("color 70");///black on white
    			B=1;
    			printf(" %d black\n", j);
    			}
    			if(j==6){
    			system("color 70");///black on white
    			B=1;
    			printf(" %d black\n", j);
    			}
    			if(j==8){
    			system("color 70");///black on white
    			B=1;
    			printf(" %d black\n", j);
    			}
    Sleep(1000);
        }/////////////////////////////////////////////...ball stopped
    
    
    ///////////////////////////////////////////////////...what is j
    for (ii = 1; ii <= ni ; ii++){
    	if(atoi(BetOnNum[ii])==j){
    	printf("You Won on : ");
    	printf(BetOnNum[ii]);
    	printf("\n");
    	}
    	if(RR==1){
    		if(R==1){
    	printf("You Won on : ");
    	printf("red");
    	printf("\n");
    		}
    	}
    	if(BB==1){
    		if(B==1){
    	printf("You Won on : ");
    	printf("black");
    	printf("\n");
    		}
    	}
    	else{
    	printf("You Lost on : ");
    	printf(BetOnNum[ii]);
    	printf("\n");
    	}
    
    }
    
    ///////////////////////////////////////////////////...what is j
        return 0;
    }/////////////////////////////////////////////////...main end
    note: (I know printf() should not be used and I'm not finished coding this project. )

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    if(BetOnNum[ii]=="1-2to1"){
    Look up strcmp(). [edit=6] http://www.cplusplus.com/ref/cstring/strcmp.html [edit=7] But don't take their lead and use gets(). [/edit] [/edit]

    [edit]
    (I know printf() should not be used and I'm not finished coding this project. )
    Why? You're not using C++, are you? [edit=3] Okay, so you are using C++. I see a cout. In that case, you have the wrong forum. But don't do anything about it or you'll get in trouble for cross-posting. [/edit] Or do you mean putchar() for single characters?

    Also consider using some switch statements. [/edit]


    [edit=2]
    You have some buffer overruns; make your for loops start counting at zero.
    [/edit]


    [edit=4] iostream.h! What compiler are you using? No, never mind. Use <iostream> and using. [/edit]


    [edit=5]
    All these edits are confusing, aren't they? Maybe I'll colour-code them.
    [/edit]
    Last edited by dwks; 07-12-2006 at 08:52 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Ive mixed some C with C++ and I should have posted in the C++ forum but I've been workin in C and posting here for so long I didnt think about it, My bad. I'm using Microsoft Visual C++ 6.0...slipped up on the <iostream.h>, thanks.

    "You have some buffer overruns; make your for loops start counting at zero."...if I start at 0 Nothing is stored in BetOnNum[0] and it prints a blank space when in while loop...It highly possible I messed up some where but every thing works as expected, with the exception of finding if red or black...will strcmp() solve the issue of if (BetOnNum[ii]=="red") not being recognized?[edit]"why yes it will, many thanks!"
    Last edited by g1i7ch; 07-12-2006 at 09:19 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    if(((j = (int) N * rand() / (RAND_MAX + 1.0))==37)){
    To make better use of that cast you should cast the whole expression, not just N:
    Code:
    if((j = (int) (N * rand() / (RAND_MAX + 1.0)) ) == 37) {
    You might also want to consider naming your variables something other than one or two uppercase characters. You can call your variables something more like max, randnum, input, etc. It makes it easier to read.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    12
    I could be wrong about this but you seem to be addressing BetOnNum as a one-dimensional ( array_name[5] ) array instead of a matrix (or 2-dimensional array: array_name[5][5] ).

    Regardless, you might consider deciding on some naming rules for variables (such as in capitalization). Generally I use something like this:

    Code:
    // variables
    int variable_name;
    
    // functions
    void sayHello( void );
    
    // classes
    class Dog : public Animal
    {
     // stuff
    }
    Don't be afraid of long names. This isn't just math, it's a construction of logic, and sometimes the cost of typing out a long name is worth the clarity for the sake of reading/debugging and allowing others to read your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble moving lines up in 2D array
    By powell5487 in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 12:54 PM
  2. Array trouble
    By Cpro in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2006, 01:55 PM
  3. trouble creating an array
    By s_ny33 in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 11:18 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM