Hi Everyone, Im' new in C. I was tasked to do a word game. Its a Guess a word game. The user inputs the letter through arrow keys. Then, my program supposed to check if each letter is correct. Then, display it in boxes. The problem is I cannot validate the letter chosen when it's duplicate. I used the strstr command in searching the letter. Below is the snippet, please help:

 substr=strstr(temp,temp1);
          if (substr != NULL)
            {
            if (prevchc != choices[pos1])
              {
               rightlet=rightlet+1;
               gotoxy(1+rightlet,18);
               printf(choices[pos1]);
               prevchc=choices[pos1];
              }
            //else
            //  {
            //  gotoxy(20,20);
            //  printf("You already guessed the letter");
            }
          if (rightlet==strlen(Easya[pos]))
            {
            gotoxy(1,19);
            printf(Easya[pos]);
            gotoxy(20,20);
            printf(commentsr[pos]);
            rightword=rightword+1;
            sleep(2);
            goto nextquestion;
            }
           if (tries==5)
             {
             gotoxy(20,20);
             printf("Exceeded the number of tries score is ");
             printf("%d",rightword);
             tries=0;
             sleep(2);
             goto newquestion;

             }

First problem I see is the use of GOTO. You should rework the program to use loops instead.

Since there is absolutely no context, no comments, and no explanations of what anything is in the code you posted, there is nothing we can tell you about your code.

I can only suggest to solve the duplicate problem is create an array of 26 values representing the alphabet. Zero the array. Every time a letter is guessed, check the array. If 0, change to 1. If not zero, it's already been guessed.

Many thanks for the reply. Below is the entire source code. It should run in Turbo C but I am currently using a C++ compiler. Everything seems to work fine though the cose is not very structured, except when the user repeats choosing the same letter. I would like to know the position of the correct letter from the correct word. The variables substr2 and checkdup even I assigned NULL is not putting a NULL value. Below is the code:

#include<iostream.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<bios.h>
#include<ctype.h>
#include<math.h>
#include<dos.h>
#include <string.h> using namespace std;

#define LTARROW 0x4B
#define RTARROW 0x4D
#define UPARROW 0x48
#define DNARROW 0x50
#define CR 0x0d
#define ESC 0x1b
#define ALT_X 0x2d

static char  *Easyq[]={"q","q","q","q","q","q","q","q","q","q"};
static char  *Easya[]={"a","a","a","a","a","a","a","a","a","a"};
static char  *Averageq[]={"q","q","q","q","q","q","q","q","q","q"};
static char  *Averagea[]={"a","a","a","a","a","a","a","a","a","a"};
static char  *Difficq[]={"q","q","q","q","q","q","q","q","q","q"};
static char  *Diffica[]={"a","a","a","a","a","a","a","a","a","a"};
static char  *commentsr[]={"r","r","r","r","r","r","r","r","r","r"};
static char  *commentsw[]={"w","w","w","w","w","w","w","w","w","w"};
static char  *choices[]={"A","B","C","D"};

main()
{
int x,i=0,grd,grm,key,j,sagot,rightlet=0,wronglet=0,rightword=0,wrongword=0,tries=0;
char *ans;
char a [1];
static char  *temp;
static char *temp1;
static char *substr;
static char *prevchc;
static char *checkdup;
char *substr2;

newquestion:

clrscr();
tries=0;
printf("W O R D   G A M E    ");
printf("1-Easy 2-Average 3-Difficult 4-QUIT\n");
printf("Choose Difficulty\n");
scanf("%d",&x);
//gotoxy(35,1);
//printf("W O R D   G A M E");

commentsr[1]="Yeah,right!";
commentsr[2]="Right, I'm guessing that you area Lasallian";
commentsr[3]="Perfect!";
commentsr[4]="Wow, you may be a Lasallian";
commentsr[5]="Very good!";
commentsr[6]="Right on target";
commentsr[7]="You're a genius, that's a Lasallian mind";
commentsr[8]="That was good";
commentsr[9]="You got it";
commentsr[10]="Bravo! Are you Lasallian?";

commentsw[1]="Incorrect";
commentsw[2]="Sorry";
commentsw[3]="Oh, it's wrong";
commentsw[4]="You missed it";
commentsw[5]="That was a bad guess";
commentsw[6]="Try again";
commentsw[7]="That was a bad guess";
commentsw[8]="You may be out of your mind";
commentsw[9]="Incorrect, think deeply";
commentsw[10]="Not a good guess";


if(x==1)
{
Easyq[1]="is a discussion or informational \n site published on the World Wide Web.";
Easyq[2]="A quantity measured with respect to \n another measured quantity";
Easyq[3]="A hole for the escape of gas or air";
Easyq[4]="Run away suddenly out of control";
Easyq[5]="Char is a type of bar char that  \n illustrates a project schedule";
Easyq[6]="A fixed regular payment receive by employees.";
Easyq[7]="is redundant array of independent disks; a way of \n storing the same data in different places (thus, redundantly) on multiple hard disks.";
Easyq[8]="meaning 1,000,000,000,000 (one trillion) 10^12";
Easyq[9]="A quantity of paper, formerly 480 sheets, now 500 sheets or, \n in a printer's ream, 516 sheets.";
Easyq[10]="a marine bivalve mollusk (subclass Heterodonta) \n with shells of equal size.";

Easya[1]="blog";
Easya[2]="rate";
Easya[3]="vent";
Easya[4]="bolt";
Easya[5]="gant";
Easya[6]="wage";
Easya[7]="raid";
Easya[8]="tera";
Easya[9]="ream";
Easya[10]="clam";



nextquestion:

clrscr();
gotoxy(50,5);
printf("abcdefghijklmnopqrstuvwxyz");
gotoxy(45,1);
printf("Easy Level (4 letter words) \n");

int pos,pos1=1;


//for(i=1;i<=10;i++)
//{
//counter for i
i=i+1;
//gotoxy(1,5);
//printf("%d",i);
rightlet=0;
tries=0;
checkdup=NULL;
substr2=NULL;
printf("\n");
srand ( time(NULL) );
pos=rand() % 10+1;
gotoxy(1,10);
printf(Easyq[pos]);
gotoxy(1,20);
//printf(Easya[pos]);
gotoxy(1,30);


while(1)

      {

        key = bioskey(0);

        if( (key & 0x00FF) > 0)

          key = key & 0x00FF;

        else

          key = (key & 0xFF00) >> 8;



        switch(key)

        {

        case LTARROW:
          gotoxy(50,5);
          pos1=pos1-1;
          gotoxy(49+pos1,5);
          break;



        case RTARROW:
          gotoxy(50,5);
          pos1=pos1+1;
          gotoxy(49+pos1,5);
          break;



        case DNARROW:
          gotoxy(50,5);
          break;



        case UPARROW:

          gotoxy(1,18);
          choices[1]="a";
          choices[2]="b";
          choices[3]="c";
          choices[4]="d";
          choices[5]="e";
          choices[6]="f";
          choices[7]="g";
          choices[8]="h";
          choices[9]="i";
          choices[10]="j";
          choices[11]="k";
          choices[12]="l";
          choices[13]="m";
          choices[14]="n";
          choices[15]="o";
          choices[16]="p";
          choices[17]="q";
          choices[18]="r";
          choices[19]="s";
          choices[20]="t";
          choices[21]="u";
          choices[22]="v";
          choices[23]="w";
          choices[24]="x";

          choices[25]="y";
          choices[26]="z";

          //printf(choices[pos1]);
          //static char *temp1;
          //static char *substr;
          //static char *prevchc;
          //static char *checkdup;
          //char *substr2;


          substr2=NULL;
          tries = tries + 1;


          temp=Easya[pos];s1];



          substr=strstr(temp,temp1);
          if (substr != NULL)
            {
            //{
            //if (prevchc != choices[pos1])
            //  {
            //   rightlet=rightlet+1;
            //   gotoxy(1+rightlet,18);
            //   printf(choices[pos1]);
            //   prevchc=choices[pos1];
            //  }
            //else
            //  {
            //  gotoxy(20,20);
            //  printf("You already guessed the letter");
            //}
            //

            substr2=strstr(checkdup,temp1);
            gotoxy(1,5);
            substr2=NULL;
            checkdup=NULL;
            //printf(substr2);
            //printf(checkdup);
            if (substr2 != NULL)
              {
               gotoxy(20,20);
               printf("You already guessed the letter");
               sleep(1);
               gotoxy(20,20);
               printf("                               ");
              }

            else
              {
               rightlet=rightlet+1;
               gotoxy(1+rightlet,18);
               printf(choices[pos1]);
               prevchc=choices[pos1];

               checkdup=strcat(checkdup,temp1);

              }


              }


           if (rightlet==strlen(Easya[pos]))
             {
              gotoxy(1,19);
              printf(Easya[pos]);
              gotoxy(20,20);
              printf(commentsr[pos]);
              rightword=rightword+1;
              sleep(2);
              substr2 = NULL;
              checkdup =NULL;
              goto nextquestion;
             }

            if (tries==5)
             {
             gotoxy(20,20);
             printf("Exceeded the number of tries score is ");
             printf("%d",rightword);
             tries=0;
             sleep(2);
             rightword=0;
             goto newquestion;

             }

             //if (i>=2)
               //x=2;
               //goto newquestion8;

          break;



        case ALT_X:

        case ESC:

        case CR:


          exit(0);

        }

// for      }



}

}

newquestion8:

if(x==2)
{
clrscr();
gotoxy(45,1);
printf("Average Level (5 letter words) \n");


Averageq[1]="A negatively charged ion, especially the ion that migrates to an anode in electrolysis." ;
Averageq[2]=" is a derived unit of energy, work, or amount of heat in the International System of Units.";
Averageq[3]="Showing or characterized by quick and inventive verbal humor.";
Averageq[4]="make a sibilant sound.";
Averageq[5]="the hipbone or hip joint.";
Averageq[6]="means taking the fundamental code structure (for instance of an image) and putting it into another code object";
Averageq[7]="Spend time making minor adjustments to (one’s hair, makeup or clothes)";
Averageq[8]="to use abusive language; speak disrespectfully";
Averageq[9]="any of a class of compounds that are analogous to alcohols and phenols but contain sulfur in place of oxygen.";
Averageq[10]="widely distributed lichens usually having a greyish or yellow pendulous freely branched thallus.";

Averagea[1]="anion";
Averagea[2]="Joule";
Averagea[3]="witty";
Averagea[4]="voled";
Averagea[5]="coxae";
Averagea[6]="embed";
Averagea[7]="primp";
Averagea[8]="snash";
Averagea[9]="thiol";
Averagea[10]="usnea";


}
if(x==3)
{
clrscr();
gotoxy(45,1);
printf("Difficult Level (6 letter words) \n");

printf("Difficult Level");
Difficq[1]=" a colorless flammable gas used chiefly in welding and in organic synthesis.";
Difficq[2]="A necrontic, fungal disease of apple and other trees causing damage to the bark.";
Difficq[3]="A vowel change, characteristic of Indo-European languages, that accompanies a change in grammatical function.";
Difficq[4]="To make wider or larger; cause to expand.";
Difficq[5]="Any of several usually white herons of the genera Bubulcus, Casmerodiusand, Egretta.";
Difficq[6]="kidney bean: the common bean plant grown for the beans rather than the pods.";
Difficq[7]="a white cotton or linen robe worn by Orthodox Jews on Rosh Hashana and Yom Kippur and at the Passover Seder";
Difficq[8]="To flow or leak out slowly, as through small openings.";
Difficq[9]="not actually but having the appearance of; pretended; false or spurious; sham.";
Difficq[10]="A punishment identical to the offense, as the death penalty for murder";

Diffica[1]="Alkyne";
Diffica[2]="canker";
Diffica[3]="ablaut";
Diffica[4]="dilate";
Diffica[5]="egrets";
Diffica[6]="frijol";
Diffica[7]="kittel";
Diffica[8]="oozing";
Diffica[9]="pseudo";
Diffica[10]="talion";
}
if (x==4)
exit(0);

if(x==0 || x>4)
printf("Invalid Input\n");

getch();
}

Many thanks,

Petra

How do I validate repeated correct letter? Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.