Why doesn't it work when I enter "NO"?

do{
      	printf("\n\nDo you wish to continue (yes or no): ");
	   	uppercase(fgets(progress, sizeof progress, stdin));
         fflush(stdin);

         ((strcmp(progress,"YES")!= 0 && strcmp(progress,"NO")!= 0)?printf("\nOnly 'yes' or 'no' will be accepted!\n"):printf("\n"));

      }while(strcmp(progress,"YES")!= 0 && strcmp(progress,"NO")!= 0);

at least some of your problem may be due to fgets( ) retains the newline character.

but im not sure how some of the other stuff you're doing would work... anyhow, I think you'll find it's best to try to keep code basic and readable for the next guy, rather than using clever logical constructions.

FWIW, my own style is that i don't like "do/while" loops when a simple "while" will suffice.

while (1)
{ 	
    printf("\nDo you wish to continue ('yes' or 'no'): ");		
    fgets(progress, sizeof progress, stdin);
    fflush(stdin);

    for (a=0; a<strlen(progress)-1; a++)
        progress[a] = toupper(progress[a]);
    progress[a] = 0;				

    if (!strcmp(progress,"YES") || !strcmp(progress,"NO"))
        break;			

    printf("\nOnly 'yes' or 'no' will be accepted!\n");	
}

jephthah, "try to keep code basic and readable for the next guy, rather than using clever logical constructions" is very good advice. But then using if (!strcmp(progress,"YES") || !strcmp(progress,"NO")) is not easily understandable. It assumes you understand that strcmp() returns FALSE on equality, which is illogical. Better to use if ((strcmp(progress,"YES") == 0) || (strcmp(progress,"NO") == 0))

when i say "readable" i meant for the average programmer. not for someone who can't grasp that "!" is the same as "== 0 "

perhaps we should

#define YES_INDEED_THE_STRINGS_ARE_EQUAL            0

?

when i say "readable" i meant for the average programmer. not for someone who can't grasp that "!" is the same as "== 0 "

perhaps we should

#define YES_INDEED_THE_STRINGS_ARE_EQUAL            0

?

That works for me.

Programming is not just being able to put variables and operations together. There's a logic that you need to follow to be good. The thing I'm pointing out is a good programmer will not use a boolean test for a non-boolean comparison. strcmp() is not a boolean function, therefore it's bad form to treat it as such.

The problem was actually in my uppercase function. The length of the returning string was always +1 than what it should be or at the array limit depending on what the user entered. In my case if I entered "yes" in my char progress[4] array the length would be three + the null character but when I entered no the size of the modified array would be three, hence different from "NO" which is only two characters.

I settled with using a simple "y" for yes and "n" for no combined with the toupper() function in the ctype.h file.

Yes, that's exactly what i was trying to tell you in my first post.

fgets( ) returns the newline character, which is why you believed the the string was was always +1 from what you "thought" it should be.

because that's what fgets( ) does, according to the C programming language.

you need to change the newline into a NULL using the line of code i provided, or use strncmp( ) to ignore the newline.

otherwise your "fix" will never allow you to ever parse a string from stdin.

Hope I could help you...

instead of
strcmp

just use
strcmpi

for example

char a[5];

printf("\n\nDo you wish to continue yes or no): ");

if(strcmpi(a,"yes")==0{ printf("you choose YES");getch();}

else if(strcmpi(a,"no")==0{ printf("you choose NO");getch();}

else { printf("Invalid");getch();}

}

char a[5];

printf("\n\nDo you wish to continue yes or no): ");

if(strcmpi(a,"yes")==0{ printf("you choose YES");getch();}

else if(strcmpi(a,"no")==0{ printf("you choose NO");getch();}

else { printf("Invalid");getch();}

three problems with your post ^


(1) strcmpi is a Microsoft-specific implementation. it wont work on "real" C.

(2) strcmpi was deprecated as of 2005.

(3) your usage of getch( ) within the loops is irretrievably broken.

what on earth even makes you think that such a construct would work? have you ever written a program to get input from a terminal?

This article has been dead for over six months. Start a new discussion instead.