Jump to content

Comparing strings to an array?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
souldout

souldout

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

For some reason I can't figure out how to compare the input of a string to the value in an array.

What I'm trying to do is compare the input from the user to the correct answer in the array to see how many they got right.

I'll provide an example of the problem to which I'm trying to solve:

David enters his answers TTTTT

--- the program compares Davids input to the array: TFTFT
--- the program calculates how many David got right in this case 3.
--- the program displays output to the user.



Here is my code (I left out the obvious printf and scanf and a few variable declarations to not crowd up the page):

char correctAnswer[] = "TFTFTFFTFTFT"

char input[15][2]={""}

int f;

int l;

f = strcmp(input[0],correctAnswer);


if (f <= 0)

{

f += 14;

}



printf("you got this amount right: %d\n", f);



#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
strncmp - C++ Reference
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users