hmkla
hmkla

Reputation: 1

Why does code yields no results on this char comparison?

Evening, sorry for beginner question but I have to do a code that receives 7 salutations in different languages, compare to a database, and, if they match, tell which language the salutation was on, if they don't, tell the user the language is unknown.

I think i understood the problem, but my code below doesn't show any results and just closes, can someone tell me why? I know it is very sketchy coding but cant find exactly the mistake. (Telling me a substitute for the multiple variables inside scanf would be much appreciated too).

#include<stdio.h>
#include<string.h>
int main(){
    char en[7][15];
    char id[7][15] = {"HELLO","HOLA","CIAO","HALLO","BOUNJOUR","ZDRAVSTVUJTE","."};
    char re[7][15] = {"INGLES","ESPANOL","ITALIANO","ALEMAN","FRANCES","RUSO","NLS"};
    scanf("%s %s %s %s %s %s %s",en[0],en[1],en[2],en[3],en[4],en[5],en[6]);
    int i = 0,j=0,m=1,k=0;
    while(i<8){
        for(j=0;j<7;j++){
            m=strcmp(en[i],id[j]);
            if(m==0 || j==6){
                strcpy(en[i],re[j]);
                k=i+1;
                printf("Caso %s: %s /n",k,en[i]);
                break;
            }
            }
            i++;
    }
    }

Thank you

Upvotes: 0

Views: 57

Answers (1)

simonJarr
simonJarr

Reputation: 28

The problem is your printf function. You are using %s format specifier for k instead of %d. Just change that line to this:

printf("Caso %d: %s \n",k,en[i]);

Also, the while condition i<8 should be changed to i<7 since there are 7 cases and not 8.

As for the scanf, don't think it's much of an improvement but you can use a for loop as well.

Upvotes: 1

Related Questions