-2

I'm to write a code in C to let the user enter the first and last characters of the word "fantastic". If he is correct for the two answers it should print "Well done", if he gets one wrong then it should print "one of your answers is incorrect" if he gets both incorrect i tell him to try again later.

Below is the code I tried which doesn't allow me to enter the second character and also gets the answer wrong.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char word[] = "fantastic";
    char in1, in2;
    printf("Please enter the first letter of the word \'fantastic\': ");
    scanf("%c", &in1);
    printf("\nPlease enter the last letter of the word \'fantastic\': ");
    scanf("%c", &in2);
    if (in1 == word[0], in2 == word[8]) {
        printf("\nWell Done!\n");
    }
    else if (in1 == word[0], in2 != word[8], in1 != word[0], in2 == word[8]) {
        printf("\nOne of your answers is incorrect!\n");
    }
    else {
        printf("\nTry again next time!\n");
    }
    return 0;
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
3D0N
  • 13
  • 4

2 Answers2

3

code I tried which doesn't allow me to enter the second character

After

scanf("%c", &in1);

The newline ('\n') you entered after the character is still in the input buffer and will get consumed by

scanf("%c", &in2);

so you have no opportunity to enter another one. You can change both to

scanf(" %c" // ...
//     ^

to skip leading whitespace.


also gets the answer wrong

The next problems are with your if-statements: If you use the comma-operator between the tests for equality, the result for the whole expression will only be the rightmost subexpression:

in1 == word[0], in2 == word[8]

evaluates to in2 == word[8]. The part before the comma has no effect on it.

When you have two boolean expressions like tests for equality you have to link them with a logical and (&&) to make the whole expression true when both sides of && are true:

in1 == word[0] && in2 == word[8]

Similarly you should use a logical or (||) if you want the whole expression be true when at least one side of || is true.

So your code should look like that:

if (in1 == word[0] && in2 == word[8]) {
    printf("\nWell Done!\n");
}
else if (in1 == word[0] && in2 != word[8] || in1 != word[0] && in2 == word[8]) {
    printf("\nOne of your answers is incorrect!\n");
}
else {
    printf("\nTry again next time!\n");
}

As suggested by Fiddling Bits you don't have to define an array for the word if you have no other use for it other than checking for two letters:

char const first = 'f';
char const last  = 'c';

if (in1 == first && in2 == last) {
    printf("\nWell Done!\n");
}
else if (in1 == first && in2 != last || in1 != first && in2 == last) {
    printf("\nOne of your answers is incorrect!\n");
}
else printf("\nTry again next time!\n");

Less comparisons and logic is needed if you swap the order:

if (in1 == first && in2 == last) {  // both correct
    printf("\nWell Done!\n");
}
else if (in1 != first && in2 != last) {  // both wrong
    printf("\nTry again next time!\n");
}
else printf("\nOne of your answers is incorrect!\n");
Swordfish
  • 12,971
  • 3
  • 21
  • 43
0

I tried your suggestions and it's working out Thanks so much..... Below is the final code which is working:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char word[] = "fantastic";
char in1, in2;
printf("Please enter the first letter of the word \'fantastic\':");
scanf(" %c", &in1);
printf("Please enter the last letter of the word \'fantastic\':");
scanf(" %c", &in2);
if (in1 == word[0] && in2 == word[8]) {
printf("\nWell Done!\n");
}
else if (in1 == word[0] && in2 != word[8] || in1 != word[0] && in2 == word[8]) {
printf("\nOne of your answers is incorrect!\n");
}
else {
printf("\nTry again next time!\n");
}
return 0;
}
3D0N
  • 13
  • 4