So this is part of the code in one of my functions, and whenever I enter either yes, no, or something else, no matter what the response the do loop will loop again. I don't think my "if(response == "yes")" is working. Help please? :D

char response[10];
int answer;
int check;
   do
   {
      printf("\nWould you like you enter different hours? (yes/no)");
      scanf("%s", response);
      if(response == "yes")
         {
      	answer = 1;
         check = 0;
         }
      else if(response == "no")
         {
      	answer = 0;
         check = 0;
         }
      else
      	{
         check = 1;
         }
   }
   while(check == 1);
   return (answer);

Recommended Answers

All 3 Replies

So this is part of the code in one of my functions, and whenever I enter either yes, no, or something else, no matter what the response the do loop will loop again. I don't think my "if(response == "yes")" is working. Help please? :D

char response[10];
int answer;
int check;
   do
   {
      printf("\nWould you like you enter different hours? (yes/no)");
      scanf("%s", response);
      if(response == "yes")
         {
      	answer = 1;
         check = 0;
         }
      else if(response == "no")
         {
      	answer = 0;
         check = 0;
         }
      else
      	{
         check = 1;
         }
   }
   while(check == 1);
   return (answer);

You need to include string.h and use the strcmp() function.
Your code becomes:

#include <string.h>

char response[10];
int answer;
int check;
   do
   {
      printf("\nWould you like you enter different hours? (yes/no)");
      scanf("%s", response);
      if(strcmp(response, "yes") == 0)
         {
      	answer = 1;
         check = 0;
         }
      else if(strcmp(response, "no") == 0)
         {
      	answer = 0;
         check = 0;
         }
      else
      	{
         check = 1;
         }
   }
   while(check == 1);
   return (answer);

Note that strcmp returns 0 if it's found.

That worked perfectly. Thanks.

That worked perfectly. Thanks.

Very glad I could help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.