Minor Fix

My code is supposed to prompt the user for how they are feeling.
If they say "good", "great" or "well" it is supposed to respond with "Super! Tell me more."
If the user says "bad", "sad", or "not", it is supposed to respond "Sorry to hear that. Tell me more."
If they say anything else it should say "I see. Tell me more."
It is then just supposed to loop asking that question forever.

Right now my code responds appropriately, but only if the user responds with exactly "good" or "great," but not otherwise. For example, if the user responds "I'm doing well" it goes to the default "I see..."

I want the code to work anytime one of the keywords is used by the user, not just when they say it verbatim. How do I fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <string.h>
/* the code starts here */
int main(void)
{
    char goodresponse1[] = "good";
    char goodresponse2[] = "great";
    char goodresponse3[] = "well";
    char badresponse1[]= "bad";
    char badresponse2[]= "sad";
    char badresponse3[]= "not";
    
    char response[100];
    
    while(1)
	{
        
        printf("How are you today?\n");
        gets(response);
        
        if (strcmp(response, badresponse1) == 0)
        {
			printf("I'm sorry to hear that. \n Tell me more.\n");
        }
        else if (strcmp(response, badresponse2) == 0)
        {
            printf("I'm sorry to hear that. \n Tell me more.\n");
        }
        else if (strcmp(response, badresponse3) == 0)
        {
            printf("I'm sorry to hear that. \n Tell me more.\n");
        }
        else if (strcmp(response, goodresponse1) == 0)
        {
            printf("Super! \n Tell me more.\n");
            
        }
        else if (strcmp(response, goodresponse2) == 0)
        {
			printf("Super! \n Tell me more.\n");
        }
        else if (strcmp(response, goodresponse3) == 0)
        {
            printf("Super! \n Tell me more.\n");
            
        }
        else
        {
            printf("I see. \n Tell me more. \n");
            
        }
    }
    
}
Last edited on
You do test whether the answer is exactly one of the expected words. You should test whether the answer contains one of the expected words. The C library must have something for that.
Topic archived. No new replies allowed.