return to menu problem

This is a discussion on return to menu problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm having a small hickup with my program. Everything works fine except this bit I'm going to mention. One ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    38

    return to menu problem

    Hi, I'm having a small hickup with my program. Everything works fine except this bit I'm going to mention.

    One of the functions that my program has to do is return to menu when an empty line is entered. as you will see in the code below, this works fine for the perfect square method. but when i do it in the tic tack toe method it doesn't work.

    here are my codes(I'm not going to list all the files, just the necessary ones):

    Utility files

    Code:
    void readRestOfLine()
    {
       int c;
    
       /* Read until the end of the line or end-of-file. */   
       while ((c = fgetc(stdin)) != '\n' && c != EOF)
          ;
    
       /* Clear the error and end-of-file flags. */
       clearerr(stdin);
    }
    
    
       int getInteger(int* integer, unsigned length, char* prompt, int min, int max)
    {
       int finished = FALSE;
       char tempString[TEMP_STRING_LENGTH + 2];
       int tempInteger = 0;
       char* endPtr;
    
       /* Continue to interact with the user until the input is valid. */
       do
       {
          /* Provide a custom prompt. */
          printf("%s", prompt);
          
          /* Accept input. "+2" is for the \n and \0 characters. */
          fgets(tempString, length + 2, stdin);
    
          if( tempString[0] == '\n')
          {
              return SUCCESS; 
          }
          /* A string that doesn't have a newline character is too long. */
       
          if (tempString[strlen(tempString) - 1] != '\n')
          {
             printf("Input was too long.\n");
             readRestOfLine();
          }
          else
          {
             /* Overwrite the \n character with \0. */
             tempString[strlen(tempString) - 1] = '\0';
    
             /* Convert string to an integer. */
             tempInteger = (int) strtol(tempString, &endPtr, 10);
    
             /* Validate integer result. */
             if (strcmp(endPtr, "") != 0)
             {
                printf("Input was not numeric.\n");
             }
             else if (tempInteger < min ||tempInteger > max)
             {
                printf("Input was not within range %d - %d.\n", min, max);
             }
    
             else
             {
                finished = TRUE;
             }
          }
       } while (finished == FALSE);
    
       /* Make the result integer available to calling function. */
       *integer = tempInteger;
    
       return SUCCESS;
    }
    
    int getString(char* string, unsigned length, char* prompt)
    {
       int finished = FALSE;
       char* tempString;
    
       /* Allocate temporary memory. */
       if ((tempString = malloc(sizeof(char) * (length+2))) == NULL)
       {
          fprintf(stderr, "Fatal error: malloc() failed in getString().\n");
          exit(EXIT_FAILURE);
       }
    
       /* Continue to interact with the user until the input is valid. */
       do
       {
          /* Provide a custom prompt. */
          printf("%s", prompt);
          
          /* Accept input. "+2" is for the \n and \0 characters. */
          fgets(tempString, length + 2, stdin);
    
          /* A string that doesn't have a newline character is too long. */
          if (tempString[strlen(tempString) - 1] != '\n')
          {
             printf("Input was too long.\n");
             readRestOfLine();
          }
          else
          {
             finished = TRUE;
          }
    
       } while (finished == FALSE);
    
       /* Overwrite the \n character with \0. */
       tempString[strlen(tempString) - 1] = '\0';
       
       /* Make the result string available to calling function. */
       strcpy(string, tempString);
       
       /* Deallocate temporary memory. */
       free(tempString);
    
       return SUCCESS;
    }
    methods for menu options:

    Code:
    void perfectSquares(int * optionStats, int i)
    {
         int num1 = 0;
         float num2;
         float num3;
         int test2;
         int min = 1;
         int max = 1000000;
         char prompt[PROMPT_LENGTH+1];
         
         printf("Perfect Squares\n");
         printf("\n");
         sprintf(prompt,"Enter a positive integer (%d - %d): ", min, max);
         getInteger(&num1, MAX_INPUT_PS, prompt, min, max);
        
         
         num2 = sqrt(num1);
         test2 = sqrt(num1);
         num3 = num2 - test2;
         
         if(num1>0)
         {
                   if(num3 == 0)
                   {
                           printf("'");
                           printf("%d", num1);
                           printf("'");
                           printf(" is perfect square!\n");
                           
                           int num4 = sqrt(num1);
                           int ps1 = pow(((num4)+1),2);
                           int ps2 = pow((num4-1),2);
                           printf("Perfect square before: ");
                           printf("%d",ps2);
                           printf("\n");
                           printf("Perfect square after: ");
                           printf("%d",ps1);
                           printf("\n\n");
                           
                           }
                           
                           else
                           {
                               printf("'");
                               printf("%d", num1);
                               printf("'");
                               printf(" is not a perfect square!\n");
                               
                               int num4 = sqrt(num1);
                               int ps1 = pow(((num4)+1),2);
                               int ps2 = pow((num4-1),2);
                               printf("Perfect square before: ");
                               printf("%d",ps2);
                               printf("\n");
                               printf("Perfect square after: ");
                               printf("%d",ps1);
                               printf("\n\n");
                               }
                               }
                               
         return;
    }
    
    
    /* See requirement #3 "ASCII to Binary Generator". 
     */
    void asciiToBinary(int * optionStats, char *string)
    {
         char string1[INPUT_LENGTH + 1];
         char prompt[PROMPT_LENGTH + 1];
         int i;
         int y;
         int x;
         int length;
         
         printf("ASCII to Binary Generator \n\n");
         
    
    
       sprintf(prompt, "Enter a string (max %d characters): ", INPUT_LENGTH);
    
       getString(string1, INPUT_LENGTH, prompt);
    
       printf("Binary representation: ");
        
      for(i=0; i<strlen(string1); i++)
      {
         x = (int)string1[i];
    
         
         
       for(y = 7; y>=0; y--) {
             
            if((1<<y)&x) printf("1");
            else printf("0");
            
        }
        
        printf(" ");
        
         }
         
    printf("\n");
        return;
    }
    
    
    
    /* See requirement #4  "Matching Brackets". 
     */
    void matchingBrackets(int * optionStats, char *string)
    {
    }
    
    
    /* See requirement #5  "Tic Tac Toe Game". 
     */
    void ticTacToe(int * optionStats)
    {
     int player = 0;
     int winner = 0;
     int i;
     int min = 1;
     int max = 9;
     char prompt[PROMPT_LENGTH+1];
     int choice =0;
     int row = 0;
     int column = 0;
     int line = 0;
     
     char board[3][3] = {
          {'1','2','3'},
          {'4','5','6'},
          {'7','8','9'}
                };
    
                        
         for(i=0; i<9 && winner == 0; i++)
         {
                 printf("\n\n");
                 printf("---+---+---\n");
                 printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
                 printf("---+---+---\n");
                 printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
                 printf("---+---+---\n");
                 printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
                 printf("---+---+---\n");            
                 
                 player = i%2 +1;
                 
                 do
                 {
                        sprintf(prompt,"\nPlayer  # %d select square: " ,player);
                        getInteger(&choice, MAX_INPUT_TTT, prompt, player);
                        
                        row = --choice/3;
                        column = choice%3;
                        
                 }
                 while(choice<0 || choice>9 || board[row][column]>'9');
                 {
                 board[row][column] = (player == 1) ? 'X' : '0';
                 
                 if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) ||
                    (board[0][2]==board[1][1] && board[0][2]==board[2][0]))
                    winner = player;
                    
                    else
                      for(line = 0; line <=2; line++)
                      {
                      if((board[line][0]==board[line][1]&& board[line][0]==board[line][2])||
                         (board[0][line]==board[1][line] && board[0][line]==board[2][line]))
                         winner = player;
                         }
                         }
                         }
                 printf("\n\n");
                 printf("---+---+---\n");
                 printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
                 printf("---+---+---\n");
                 printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
                 printf("---+---+---\n");
                 printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
                 printf("---+---+---\n");       
                 
                 if(winner == 0)
                 printf("Draw");
                 else 
                 printf("Player %d is the winner!", winner);        
                    
                        
    
     return;
    }
    
    
    /* See requirement #6  "Formating Text" of the assignment specs. 
     */
    void formattingText(int * optionStats, int lineLength, char *string)
    {
    }
    
    
    
    /* See requirement #7  "Session summary" of the assignment specs. 
     * Function sessionSummary() reports the program usage.  
     */
    void sessionSummary(int * optionStats)
    {
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by spikestar View Post
    Hi, I'm having a small hickup with my program. Everything works fine except this bit I'm going to mention.

    One of the functions that my program has to do is return to menu when an empty line is entered. as you will see in the code below, this works fine for the perfect square method. but when i do it in the tic tack toe method it doesn't work...
    What have you tried to debug this program??
    Develop a bag of debugging aids for a sizeable program size as this one.
    Start by tracing the program ie printing out values of key variables at critical points.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spikestar View Post
    One of the functions that my program has to do is return to menu when an empty line is entered. as you will see in the code below, this works fine for the perfect square method. but when i do it in the tic tack toe method it doesn't work.
    Since you know where the problem is, and you have a similar case which is not problematic, why don't you isolate the relevant code in a small test program to try and figure it out in more detail? Even if you cannot fix it yourself, you will the at least be able to ask a short, specific question rather than expecting someone else to take an interest in your entire program, which is probably mostly irrelevant to the problem.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Please post a minimal complete example showing the problem.
    Of course if you can't be bothered doing that, then we probably can't be bothered looking at it.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  5. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21