Thread: Trouble exiting do-while

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    19

    Trouble exiting do-while

    Alright I figured out my first problem, so now I can't figure out why I can't exit the 'checkGuess' function with out it adding as many "white pegs" as it pleases. Any ideas?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define GPEGS 4 /*Guess Pegs*/
    #define GUESSES 8 /*Table Rows*/
    #define COLORS 6 
    #define TRUE 1
    #define FALSE 0
    
    typedef struct{
            char correct[GPEGS+1];
            char guessed[GPEGS+1];
    }display;
    
    void setDivider(char divider[], int pegs);
    void menu(int *option);
    void runGame(int pegs, char code[], char divider[], display row[]);
    void getCode(char code[]);
    void playGame(char code[], char divider[], display row[]);
    void getGuess(char guess[], char guess2[], char divider[], display row[], int tries);
    void printTable(char guess[], char divider[], display row[], int tries);
    void checkGuess(char guess[], char guess2[], char divider[], char code[], display row[], int tries, int *win);
    
    main(){
    
            int pegs=(5+GPEGS*2);
            int option=0, i, j;
            char code[GPEGS+1];
            char divider[pegs];
            display row[GUESSES+1];
    
            setDivider(divider, pegs);
    
            for(i=0; i<GUESSES; i++){
              for(j=0; j<=GPEGS; j++){
                (row[i].guessed[j])=' ';
                (row[i].correct[j])=' ';
              }
            }
    
            /*Bases randomly generated numbers on time.*/
            srand( (unsigned)time( NULL ) );
    
            do{
              menu(&option);
              if(option==1)
                runGame(pegs, code, divider, row);
              }while(option!=2);
            
            system("clear");
            printf("\n\nThank you for playing Master Mind!\n\n");
            exit(0);
    }
    
    void setDivider(char divider[], int pegs){
    
            int i, dashes=pegs-1;
    
            for(i=0; i<dashes; i++){
              divider[i]='-';
            }
            
            divider[dashes]=0;
    }
    
    void menu(int *option){
    
            int select;
    
            printf("\nMaster Mind Menu\n");
            printf("\n1)Play Game\n");
            printf("2)Exit Game\n");
            printf("\nPlease chose an option: ");
            fflush(stdin);
            scanf("%d", &select);
            
            switch(select){
              case 1:
              case 2:
                *option=select;
                break;
              defualt:
                while((select!=1)&&(select!=2)){
                  system("clear");
                  fflush(stdin);
                  printf("That is an invalid entry!");
                  printf("\nPlease select a number from the menu list.");
                  printf("\n\n1)Play Game");
                  printf("\n2)Exit Game\n");
                  scanf("%d", &select);
                }
              *option=select;
            }
    }
    
    void runGame(int pegs, char code[], char divider[], display row[]){
    
            
            getCode(code);
            playGame(code, divider, row);
            
            
    }
    
    void getCode(char code[]){
    
            int numcode[GPEGS+1], i;
    
            for(i=0; i<GPEGS; i++){
                    numcode[i]=(rand()%(COLORS))+1;
            }
    
    puts(code);
    
            for(i=0; i<GPEGS; i++){
    
              if(numcode[i]==1)
                code[i]='r';
              if(numcode[i]==2)
                code[i]='o';
              if(numcode[i]==3)
                code[i]='y';
              if(numcode[i]==4)
                code[i]='g';
              if(numcode[i]==5)
                code[i]='b';
              if(numcode[i]==6)
                code[i]='p';
            }
            code[i]=0;
    }
    
    void playGame(char code[], char divider[], display row[]){
    
            int tries, win=FALSE, i, j;
            char guess[GPEGS+1], guess2[GPEGS+1], menu;
    
            tries=0;
    
            /*Set/Resets table to blanks*/
            for(i=0; i<GUESSES; i++){
              for(j=0; j<=GPEGS; j++){
                (row[i].guessed[j])=' ';
                (row[i].correct[j])=' ';
              }
            }
    
            while((tries<=GUESSES)&&(win==FALSE)){
              puts(code);
              getGuess(guess, guess2, divider, row, tries);
              checkGuess(guess, guess2, divider, code, row, tries, &win);
              tries++;
            }
    
            if(win==TRUE){
              system("clear");
              printTable(guess, divider, row, tries);
              printf("\n\nCongratulations!  You beat mastermind!");
              printf("\n\nPress enter to return to menu.");
              scanf("%c", menu);
            } 
    }
    void getGuess(char guess[], char guess2[], char divider[], display row[], int tries){
            
            int i, option, fill=0, quit=FALSE, reminder=0;
            char peg;
    
            for(i=0; i<GPEGS; i++)
              guess[i]=' ';
              guess2[i]=' ';
            guess[GPEGS]=0;
            guess2[GPEGS]=0;
    
            do{
    
              system("clear");
              printTable(guess, divider, row, tries);
              if(reminder)
                printf("***Please fill all slots.***");
              printf("\nChose a slot 1-%d OR",  GPEGS);
              printf("\n Enter %d to submit guess.\n", (GPEGS+1));
              printf("Enter slot/option: ");
              fflush(stdin);
    
              while(!scanf("%d", &option)||(option<1)||(option>GPEGS+1)){
                system("clear");
                printTable(guess, divider, row, tries);
                printf("\n\n***That is not a valid entry, try again.***");
                fflush(stdin);
                printf("\n\nEnter slot/option: ");
              }
              if(option<=GPEGS){
                printf("Enter Guess");
                printf("(r, b, g, y, o, p): ");
                fflush(stdin);
                  while(!scanf("%c", &peg)||((peg!='r') 
                                          &&(peg!='b')
                                          &&(peg!='g')
                                          &&(peg!='y')
                                          &&(peg!='o')
                                          &&(peg!='p'))){
    
                    system("clear");
                    printTable(guess, divider, row, tries);
                    printf("\n\n***That is not a valid entry, try again.***");
                    fflush(stdin);
                    printf("\n\nEnter a Color: ");
                  }
                guess[option-1]=peg;
              }
              else{
                fill=FALSE;
                for(i=0;i<GPEGS;i++){
                  if(guess[i]==' ')
                    fill=TRUE;
                  }
              }
              if(fill){
                quit=TRUE;
              }
              if(!quit){
                system("clear");
                printTable(guess, divider, row, tries);
                }
              
            reminder=1;
            }while(!quit);
    
            strcpy(guess2, guess);
    
    }       
    
    void printTable(char guess[], char divider[], display row[], int tries){
            
            int i, j;
    
            for(i=0; i<GPEGS; i++){
              (row[tries].guessed[i])=guess[i];
            }
    
            puts(divider);
            for(i=GUESSES-1; i>=0; i--){
              printf("|");
              for(j=0; j<GPEGS; j++){
                printf("%c", (row[i].guessed[j]));
              }
              printf("||");
              for(j=0; j<GPEGS; j++){
                printf("%c", (row[i].correct[j]));
              }
              printf("|\n");
              puts(divider);
            }
    
    }
    
    void checkGuess(char guess[], char guess2[], char divider[], char code[], display row[], int tries, int *win){
    
            int i, j, black, white, place, stop;
    
            black=0;
            white=0;
            place=0;
            stop=0;
    
            for(i=0; i<GPEGS; i++){
              if(guess2[i]==code[i]){
                black++;
                guess2[i]==' ';
              }
            }
    
            for(i=0; i<GPEGS; i++){
              for(j=0, stop=0; (j<GPEGS)&&(!stop); j++){
                if(guess2[i]==code[j]){
                  white++;
                  guess2[i]=' ';
                  stop=1;
                }
              }
            }
    
            for(i=0; i<black; i++){
              (row[tries].correct[i])='b';
              place++;
            }
    
            for(i=0; i<white; i++){
              (row[tries].correct[place])='w';
              place++;
            }
            
            if(black==GPEGS)
              *win=TRUE;
    }
    Last edited by Volair; 12-11-2005 at 07:15 PM. Reason: Figured it out original problem. Another problem still exist.

  2. #2
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    I don't see you trying to exit the checkGuess() function anywhere in the function. If you want the function to exit somewhere, you can simply write
    Code:
    return;
    If you want to exit a loop, you write
    Code:
     break;
    Oh,
    Code:
    fflush(stdin);
    does not do what you think. X strike one, or, in better words, fflush is undefined on input buffers.
    Last edited by Silfer; 12-12-2005 at 06:00 AM.
    -S

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    OK I have it now where it will exit, but even when a "peg" is in the correct spot, a white peg is still added (white peg being that that you guessed a right color in the wrong spot). I checked it out and added a printf statement that tells me what number is stored in white, and every time it comes out 4 when I enter all the correct pegs in the correct order ><

    Like this:
    Code:
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |pprb||    |
    ------------
    
    You are on guess 1.
    
    Chose a slot 1-4 OR
    Enter 5 to submit guess.
    Enter slot/option: 5
    white == 4!------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |    ||    |
    ------------
    |wwwb||bbbb|
    ------------
    
    
    Congratulations!  You beat mastermind!
    
    Press enter to return to menu.^C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:34 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM