I'm trying to make a function that given a string, it processes the string like I show below, modifying the value of a struct variable. In simple language, given a string, it modifies the coordinates of a variable.
typedef struct coo {
int x;
int y;
} Coord;
typedef struct exer{
char ray[1000];
Coord coords[1000];
} exercise;
exercise test;
int coordinates(char *sent){
int i=0,j=1;
test.coords[0].x=0;
test.coords[0].y=0;
if(strlen(test.ray)>=strlen(sent)){
for(;((int) strlen (test.ray) >= j && sent[i]!='\0');i++){
if(sent[i]=='F'){test.coords[j].x = test.coords[j-1].x+1;
test.coords[j].y = test.coords[j-1].y;} else{
if(sent[i]=='L'){test.coords[j].y = test.coords[j-1].y+1;
test.coords[j].x = test.coords[j-1].x;} else{
if(sent[i]=='R'){test.coords[j].y = test.coords[j-1].y-1;
test.coords[j].x = test.coords[j-1].x;} else{
return erromsg(SENT);}
}
}
j++;
}
for(;(int) strlen (test.ray) > i && sent[i]=='\0';){
for(;j<(int) strlen(test.ray);j++){
test.coords[j].x = test.coords[j-1].x+1;
test.coords[j].y = test.coords[j-1].y;
}
}
}
else return errormsg(SENT);
return 1;
}
The problem is that when I later call a function to show the output on the screen, it gives me coordinates with characters like this: � and others who won't even copy to this page :)
I'm new to C, so any advice will be welcome.
edit: code to print the coordinates
int showcoords(){
int k=0;
if(test.coords==NULL) return errormsg(COLOC); else{
while((int) strlen (test.ray)>k){
printf("(%c,%c) ",test.coords[k].x,test.coords[k].y);
k++;
}
}
printf("\n");
return 1;
}
sent? How do you output the coordinates?%dsince they areintnotchar.test.coords==NULLwill always be false. An array can never beNULL.