Text Twist Refix

Hello Guys, this is my code of text twist written in C.
I can't figure out this error:

a.) when you input a word that is not in the dictionary
it will say Oops Sorry, then after that try to random
the word by inputting 1. Then it will print a letter
at the upper left.

b.) my computation in scores is wrong, it does not add the
scores.

c.) the letter you've entered is accepted again, it must not be.

Thank you guys, for the help. By the way I have a text file named
wlist1.txt. This file contains the words to be compared by words
inputted by the user.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>

void randomiz(char*);
void Guess(char*,int*,int*);

void main()
{
  char *word= {"random"};
  long int x;
  int b, score=0,ctr2=0;
  time_t t;
  clrscr();
  srand((unsigned)time(&t));

  randomiz(word);
  score = ctr2 = 0;
  Guess(word,&score,&ctr2);
  gotoxy(10,11);
  printf("You got a %d", score);
  getch();
      }


void randomiz(char* y)
{
char A[256] = {'\0'};
int i=0, x, k, j, z;
int n[256]= {'\0'};
time_t t;
x = strlen(y);
srand((unsigned)time(&t));
clrscr();
printf("\n\n\n\t\t\t\t");
while (i<x)
{
a:;
j = rand()%x;
z = 0;
for (k=0,z=0; k<i; k++)
if (n[k]==j)
z++;

if (z!=0)
goto a;
else
{
n[i] = j;
A[i] = y[j];
gotoxy(27,30);
printf(" %c ", A[i]);
i++;
}
}
return;
}


void Guess(char* word,int* score,int* ctr2)
{
  int ctr;
  FILE* fp;
  char guess[256] = {'\0'};

  ctr = *score;
  while(1){
  q:
  gotoxy(10,10);
  printf(">> ");

  gets(guess);

  if (strcmp(guess,"-exit")==0)
      exit(1);

  if (strcmp(guess,"1")==0)
	{
	randomiz(word);
	goto q;
	}

	else

  fp = fopen("wlist1.txt", "r");

  while (!feof(fp))
  {
	fscanf(fp, "%s\n", word);
	if (stricmp( word, guess)==0)
	{
	*score += strlen(guess)*10;
	gotoxy(55,2);
	printf("score: %d", strlen(guess)*10);
	fclose(fp);
	goto q;
	  }
		}

		fclose(fp);

		if (*score==ctr)
		{
		gotoxy(10,12);
		printf("Oops Sorry!!");
		getch();
		goto q;
		}
		else if(*score>ctr && *score<100)
		{
			gotoxy(10,13);
			printf("Correct!!");
			getch();
			goto q;
		}
		else
		{
			gotoxy(45,30);
			printf("You Won!");
			return;
		}
	}
}
Last edited on
Please stop using goto.
In randomiz(), that goto can be replaced by continue.
In Guess(), those gotos cab be replaced by break (continue on line 82).

The string buffer isn't large enough and is only 7 bytes. You could fix that by replacing line 12 with (as you kind of do in Guess):
 
    char word[128] = "random"; // big buffer, initialised with random 

But it's still dangerous because you never pass the size of word, so Guess can't be sure how large (small actually) the buffer really is, making you susceptable to a buffer overrun. That's bad.

It would help if you used some soft of consistent indentation. The code's pretty much unreadable as it stands.

You don't need to keep calling srand(), you only need to do it once.

I have no idea how your score ought to be calculated, so can't comment on the correctness of your calculations.
@kbw
Thanks foe the help.

Uhm, can i get the real correction? Please. Thanks.
I think those were real corrections... just sayin'

the continue; will go to the top of the current loop while the break; will exit the current loop, that's why you don't need the goto... if you break from the second loop, it will hit the end of the first loop and start over, and in the first loop, all the goto's can be replaced by continue, because that will restart the loop...

the code is a mess though, I agree that you need to clean it up to make it more readable...
Weee..thanks for the info coders... You've helped me a lot.. :P
Topic archived. No new replies allowed.