#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
struct Position
{
char* x1[2];
char* x2[2];
};
int main(void)
{
struct Position positions[4];
struct Position p1[4];
char* p;
matrix[10][10];
if(r==0) //stores horizontal words in matrix
{
emptyH=0;
do
{
x=(rand()%size);
y=(rand()%10);
emptyH=0;
for(k=0;k<ans;k++)
{
if (matrix[y][x+k]!='1')
{
emptyH=1;
k=ans;
} // checks whether word fits in matrix and position is empty
}
}while(emptyH);
token = strtok(words[random],search); //strtok to seperate char by char
matrix[y][x]=token;
if(x!=0)
{
p=(char)x;
p1[i].x1[0] = p;//SAVING the positions of x and y
}
else
p1[i].x1[0]='\0';
if(y!=0)
{
p=(char)y;
p1[i].x1[1]=p;
}
else
p1[i].x1[1]='\0';
x++;
while(token!=NULL)
{
token = strtok(NULL, search);
if(token!=NULL)
{
matrix[y][x]=token;
p=(char*)x;
p1[i].x2[0]=p;
p=(char*)y;
p1[i].x2[1]=p; // saving positions of x and y
x++;
positions[i] = p1[i];
}
}
}
correct=0;
do
{
puts("\nenter the word:");
scanf("%s",&name);
printf("\n Enter the start and end position of the word");
puts("\nstart:");
scanf("%s",&start);
puts("\n\tend:");
scanf("%s",&end);
for(k=0;k<4;k++)
{
if((strcmp(positions[k].x1,start)==0)&&(strcmp(positions[k].x2,end)==0))
{
puts("viola!");
correct=1;
}
else
puts("incorrect answer! try again");
}
}
while(correct!=1);
}
Ceci est mon code. Cependant, la comparaison de chaînes donne toujours une mauvaise réponse. Il y a parfois une erreur d'exécution car je stocke les positions dans le 'p1 [i] .x1 [0]'. des idées pourquoi? Merci d'avance pour votre aide.strcmp ne fonctionne pas en c
ce qui se passe avec le format de code, et avez-vous essayé de compiler encore votre code? – artm
il n'y a pas d'erreurs de syntaxe. cependant le strcmp me donne toujours "réponse incorrecte essayez encore!" même s'il est correct – Mar
Qu'est-ce que 'words'? Qu'est-ce que 'random'? Qu'est-ce que 'search'? –