0

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;
}
7
  • 3
    What is the value of sent? How do you output the coordinates? Commented Mar 23, 2013 at 15:43
  • sent is just an array with the char's F, L, or R. I output the coordinates with another function that print's them one by one with "(%c,%c)" Commented Mar 23, 2013 at 15:46
  • 4
    Print coordinates with %d since they are int not char. Commented Mar 23, 2013 at 15:47
  • 2
    You seem to be very fond of strlen() Commented Mar 23, 2013 at 15:55
  • 1
    The expression test.coords==NULL will always be false. An array can never be NULL. Commented Mar 23, 2013 at 16:25

1 Answer 1

1

printf's %c specifier means print the corresponding character, so 100 -> d, etc. Since your numbers aren't anything to do with ASCII/Unicode, you get seemingly random characters.

Simply change %c for %d, which prints integer numbers. There are many other flags, such as %f for floats and doubles. You can also apply formatting such as leading zeroes, etc.

See this handy reference: http://www.cplusplus.com/reference/cstdio/fprintf/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.