Comparing array elements

137 views Asked by At

Can you help me what's wrong with this, everytime i input 1, nothing happens. i need to compare a single element of the string which i am getting to 1 but i could not do it.

#include<stdio.h>
#include<conio.h>
#include<string.h>

main()
{

    int k,;
    char x[10],array[10];
    array[0]='X';
    array[1]='C';
    array[2]='O';
    array[3]='M';
    array[4]='P';
    array[5]='U';
    array[6]='T';
    array[7]='E';
    array[8]='R';
    array[9]='S';

    gets(x);

    if(x[0]==1)
        printf("%c",array[1]);

    getch();
}
1

There are 1 answers

7
Charles Duffy On

If you're entering 1 as input, then you need to check for the character 1, not the ordinal 1.

Thus:

if (x[0] == '1')

not

if (x[0] == 1)