Word Trivia Problem

This is a discussion on Word Trivia Problem within the C Programming forums, part of the General Programming Boards category; Hey guys! I'm having a problem with this Word Trivia program.. It's uh.... Well... the mechanics are easy... I think. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Word Trivia Problem

    Hey guys! I'm having a problem with this Word Trivia program.. It's uh.... Well... the mechanics are easy... I think. The user is shown a board consisting of letters. To win, the player must correctly answer at least 1 trivia question per row. The letter on the board is the starting letter of the answer to the trivia question. If the player is not able to answer all questions in an entire row, the game is over. Assuming the player chose the letters in the shaded blocks and answered the trivia correctly, since there’s a location answered correctly per row, the player wins. The outlined letters signify that the player answered incorrectly on those blocks. Since, all questions in a row were incorrectly answered, the game ends with the player losing. Note that there could be different configurations of the board, depending on the player’s preference. The smallest board would be 3x3. The largest is 10x10. However, the matrix need not have the same number of rows and columns. You should also allow the user (the game master) to enter the collection of terms/words and the trivia/clues that will be used in the game. It is, thus, important to note that each term (the answer) can be at most 20 letters. And there can be at most 10 trivia per term. The trivia/clue is represented by a relation and the relation value pair. The relation and relation value can each be a phrase and each can have at most 30 characters.

    I'm currently working on the program, and I have a problem when it comes to the termination thingy...

    To clarify what I say, just enter 1, 1 and enter any word, enter any clue, enter trivia, then if it asks you to make another trivia, say no, and it STILL goes back. DX Please help me!

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef char string20[21];
    typedef struct {
       string20 relation;
       string20 relationvalue;
        /* other members here */
    }relationtype;
    
    struct nodeTag {
       struct nodeTag *pNext;
       struct nodeTag *pPrev;
       string20 term;
       relationtype trivia[10];
       int ctrtriv;
    };
    
    typedef struct nodeTag nodestructtype;
    typedef nodestructtype *ptrnode;
    /*
    ptrnode * arrange(ptrnode *pFirst)
    {
         ptrnode pNew, pTail, pRun;
         pNew= 
    //arrange alphabeticall
       if(pFirst == NULL)
       return(pNew); 
       else if(strcmp(pNew->term, (*pFirst)->term) < 0)
       {
            pNew -> pNext = *pFirst;
        return(pNew); 
    }
       else { 
             pTail = *pFirst;
          while(strcmp(pNew->term, pTail->term) >0 && pTail -> pNext != NULL)
          { 
                                   pRun = pTail;
            pTail = pTail-> pNext;
             }
          if(strcmp(pNew->term, pTail->term) < 0)
          {
                                 pRun->pNext = pNew;
            pNew->pNext = pTail;
            pNew->pPrev = pRun; 
            pTail->pPrev = pNew;
            }
            else
           { 
                pTail->pNext = pNew;
             pNew->pPrev = pTail;
           }
           return(pFirst);
           } 
         
         
         }
    
    void viewterm(ptrnode pFirst)
    {
         ptrnode pTail, pRun = pFirst,pTemp;
         
    
        pTail = pRun;
       do{
               printf("%s",pFirst -> term);system("pause");
               pRun = pRun -> pNext;
    }while(pFirst -> pNext != NULL);
    
    
     } 
     
    void  addtrivia (ptrnode *pFirst)
    {  
        int temp=1,y=0;
        char extra;
        ptrnode pNew = *pFirst, pRun, pTail;
        
    printf("Enter Trivia:");
    fgets(pNew -> trivia[y].relation,sizeof(string20),stdin);
    printf("Enter Clue:");
    fgets(pNew -> trivia[y].relationvalue,sizeof(string20),stdin);
              
              
              // insert node at front of list 
             pNew->pNext = *pFirst;
            *pFirst = pNew;     
            
       printf("Trivia = %s\nClue = %s",  pNew -> trivia[y].relation,  pNew -> trivia[y].relationvalue);
            y++;
            system("pause");
    }*/
    
    ptrnode addterm (ptrnode pFirst)
    {  
        ptrnode pNew, pRun, pTail;
        string20 stemp;
        int ctr1 = 0,ctr2=0;
        char opt,temp;
        int found = 0;
           
         pRun = pFirst;
        pNew = malloc(sizeof(nodestructtype));  
       if(pNew != NULL)
        {  pNew->pNext=NULL;
        pNew->pPrev=NULL;
        pNew->ctrtriv = 0;
     do{
         pRun = pFirst;
              
              printf("\n\n Enter term:  ");
              fgets(stemp,sizeof(string20),stdin);
               //check if word is unique
           
            while(pRun != NULL)    
               { if(strcmp(stemp, pRun -> term) == 0)
                      strcpy(stemp, "another");
                 pRun = pRun -> pNext;
               }
                printf("\nWord is unique!\n ");        
           }while(strcmp(stemp, "another") == 0 || strlen(stemp) < 1 || strlen(stemp) > 20);
          //  (pNew->ctrterm)++;
            strcpy(pNew -> term, stemp);
           
     //pTemp = arrange(pRun); 
     
      printf("Enter maximum of 10 trivias\n\n\n");
           
    do { printf("%dst/nd/rd/th trivia\n",ctr1+1);
    
     do{   
    printf("Enter trivia:");
     fgets(stemp,sizeof(string20),stdin);//fgets(pNew->trivia[ctr-1].relation,sizeof(string20),stdin);
    if(strlen(stemp) > 0 && strlen(stemp) < 20)
    {
      strcpy(pNew -> trivia[ctr1].relation, stemp);
               ( pNew -> ctrtriv)++;
               }
     }while(strlen(stemp) < 1 || strlen(stemp) > 20);
     
     do{
                            
                                printf("Enter clue to trivia:");
                                fgets(stemp,sizeof(string20),stdin);
                             //   fgets(pNew->trivia[ctr1-1].relationvalue,sizeof(string20),stdin);
     while(ctr2 < pNew->ctrtriv)
               { if(strcmp(pNew -> trivia[ctr2].relationvalue, stemp)== 0)//check if clue already entered
                    {  printf("\n Clue already used. Enter another.\n");
                       found = 1;
                    }
                  ctr2++;
               }
       if(found != 1)
              strcpy(pNew -> trivia[ctr1].relationvalue, stemp);
    
     }while(strlen(stemp) < 1 || strlen(stemp) > 20 || found == 1); 
     ctr1++;
     
     printf("\nDo you want to enter another trivia?[Y/N]\n");
    scanf("%c%c", &opt, &temp);
     } while((opt != 'n' || opt != 'N' )&& ctr1 < 10 );
    
    /* //Sort alphabetically
       if(pFirst == NULL)
       return(pNew); 
       else if(strcmp(pNew->term, pFirst->term) < 0)
       {
            pNew -> pNext = pFirst;
        return(pNew); 
    }
       else { 
             pTail = pFirst;
          while(strcmp(pNew->term, pTail->term) >0 && pTail -> pNext != NULL)
          { 
                                   pRun = pTail;
            pTail = pTail-> pNext;
             }
          if(strcmp(pNew->term, pTail->term) < 0)
          {
                                 pRun->pNext = pNew;
            pNew->pNext = pTail;
            pNew->pPrev = pRun; 
            pTail->pPrev = pNew;
            }
            else
           { 
                pTail->pNext = pNew;
             pNew->pPrev = pTail;
           }
           return(pFirst);
           } 
         */
    }
    
    }
     
     
     
    ptrnode addtrivia(ptrnode pFirst)
     {
           int ctr1 = 0,ctr2=0;
           string20 stemp;
           ptrnode pNew ,pRun,pTail;
           char opt,temp;
           int found = 0;
           pRun = pFirst;
           
        printf("\nTerms: \n");
           while(pRun != NULL)
               {  printf("- %s \n", pRun -> term);
                  pRun = pRun -> pNext;
               }    
        do{ 
        pRun = pFirst;
        printf("\n Enter term you want to add trivia to: "); 
        fgets(stemp,sizeof(string20),stdin);
        while(pRun != NULL && strcmp(pRun->term, stemp) != 0)
              pRun = pRun -> pNext;
              
        if(pRun == NULL)
                printf("\nNot available\n");
        }while(pRun == NULL && strcmp(stemp, "exit") != 0);
        
        if(strcmp(stemp, "exit") != 0)
        {  ctr1 = pRun -> ctrtriv;
            printf(" Type exit in trivia to stop. Note: max is 10 trivia. \n\n");
            do {
          printf("%dst/nd/rd/th trivia\n",ctr1 + 1);
          printf(" Enter trivia: "); 
           fgets(stemp,sizeof(string20),stdin);
          
          if(strcmp(stemp, "exit") != 0 && strlen(stemp) > 1 && strlen(stemp) < 20)
            {  strcpy(pRun -> trivia[ctr1].relation, stemp);
              ( pRun -> ctrtriv )++;
              
              do {  
          
          printf(" Enter clue to trivia: "); 
         fgets(stemp,sizeof(string20),stdin);
          
            while(ctr2 < pRun->ctrtriv)
               { if(strcmp(pRun -> trivia[ctr2].relationvalue, stemp)== 0)
                    {  printf("\n Clue already in use. Input again.\n");
                       found = 1;
                    }
                  ctr2++;
               }
          
            if(found != 1)
              strcpy(pRun -> trivia[ctr1].relationvalue, stemp);
            }while(strlen(stemp) < 1 || strlen(stemp) > 20 || found == 1);
            
          }  
             ctr1++;
           } while(strcmp(pRun -> trivia[ctr1-1].relation, "exit") != 0 && pRun -> ctrtriv < 10 || strlen(stemp) < 1 || strlen(stemp) > 20);
       }   
          return(pFirst);
    }
          
          
     
          
    void maintenance(ptrnode pFirst)
    { char temp;
       int opt;
       
        do{
               system("cls");
        printf("Choose what to do:\n");
         printf("1. Add term\n");
         printf("2. Add trivia\n");
         printf("3. View term\n");
         printf("4. View trivia\n");
         printf("5. Back to main\n");
         scanf("%d%c",&opt,&temp);
         switch(opt){
                     case 1: pFirst = addterm(pFirst);printf("check3 "); break;
                     case 2: pFirst =  addtrivia(pFirst);printf("check3 ");break;
                     //case 3: viewterm(pFirst);break;
                     //case 4: viewtrivia(pFirst);break;
                     case 5: system("cls");break;
         }
         
         }while(opt != 5);
         }
         
    void freelist(ptrnode *pFirst)
    {
         ptrnode pRun;
         pRun = *pFirst;
         while(pFirst != NULL)
         {
                      pRun = *pFirst;
         pRun = pRun -> pNext;
         free(pRun);
         }
         }
      
    main()
    {
            char temp;
       int opt;
       ptrnode pFirst;
       pFirst = NULL;
    
           printf("\t\tWelcome to Word Trivia 2.0\n");
          do{ 
           
              printf("Enter what phase?\n");
           printf("1. Maintenance Phase.\n");
           printf("2. Application Phase.\n");
           printf("3. Exit Program\n");
           scanf("%d%c",&opt,&temp);
         
            switch(opt)
           {
                      case 1:system("cls");
                             maintenance(pFirst);
                             system("cls");
                             break;
                      case 2:system("cls");
                      //application();break; 
                      case 3:system("cls");printf("\n\t\tGoodbye!\n");system("pause");break;
                      }
                      }while(opt != 3);
                      freelist(&pFirst);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Next time you post, please make sure your code is indented in a manner that doesn't make me want to stab my eyes out, and only post the relevant code (no extra functions or large swaths of commented out code). It just makes it harder for us to debug your stuff.

    Code:
     } while((opt != 'n' || opt != 'N' )&& ctr1 < 10 );
    That line tells your loop to continue while opt is either not 'n' or not 'N', and ctr1 is less than 10. Since opt can never be both 'n' and 'N' at the same time, the first part of that statement is always true. Thus, so long as ctr1 is less than 10, you will continue. You probably want:
    Code:
    } while (opt != 'n' && opt != 'N' && ctr1 < 10);

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Next time you post, please make sure your code is indented in a manner that doesn't make me want to stab my eyes out, and only post the relevant code (no extra functions or large swaths of commented out code). It just makes it harder for us to debug your stuff.

    Code:
     } while((opt != 'n' || opt != 'N' )&& ctr1 < 10 );
    That line tells your loop to continue while opt is either not 'n' or not 'N', and ctr1 is less than 10. Since opt can never be both 'n' and 'N' at the same time, the first part of that statement is always true. Thus, so long as ctr1 is less than 10, you will continue. You probably want:
    Code:
    } while (opt != 'n' && opt != 'N' && ctr1 < 10);
    You could force the issue...
    Code:
    } while (((opt != 'n') && (opt != 'N')) && ctr1 < 10);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. New problem [Beginner]
    By Vintik in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2005, 11:33 PM
  3. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 04:38 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

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