Friday 15 June 2012

Char comparison in C, if-statement -



Char comparison in C, if-statement -

i'm having problem code. thing need compare 2 chars n , a[p] result negative. little quiz programme assignment. q[]is array of questions , a[] array of answers. player enters t or f true or false if doesn't seem work prints 'you lose!' (even if status true).

char questions(){ const char *q [100]; q[0]= "centipedes have 100 feet."; //f q[1] = "marie curie’s hubby called pierre."; //t [...] q[99] = ""; const char *a[100]; a[0] = "f"; a[1] = "t"; [...] a[99] = ""; char n; int p, i; (i = 0; i<=7; ++i) { srand(time(null)); p = (rand() % 18); printf("\n"); printf(q[p]); printf("\n"); fflush(stdin); scanf("%c", &n); if (n == a[p]){ printf("correct answer!\n"); } else { printf("you lose!"); break; } }

it looks allocated a array of character pointers (i.e. array of c strings) instead of array of characters:

const char *a[100];

try allocating array of characters instead of array of character pointers , initialize values characters instead of c strings:

const char *a[100]; becomes char a[100]; - don't forget drop const since write values array later on.

a[0] = "f"; becomes a[0] = 'f';

c if-statement comparison

No comments:

Post a Comment