Hello.
I am trying to write a simple program which accepts input string from the user and compares the scanned string to each string in an array of character strings. For the time being, I assume that the input string,just as the strings in the defined array, has just single spaces between the words so that I can leave that checking to a later part.
So the scanned string is compared to each of those strings, a message is sent to the user telling him what he wrote ( ..i know you may be thinking whats the use of letting the user know what he just typed a moment ago...but thats not the point here, I will be writing sample functions for each of these choices later on but for the moment I am just checking the comparison part and it is not working the way i want it to work ..though the syntax seems to be right i suppose).
Heres my program:
Code:
#include <stdio.h>
#include <string.h>

int main()
{
	const char *idioms[] = {"create array","free memory",
							 "find smallest","check for palindrome"};
	char choice;
	int i=0,flag=0,equal;
	do
	{
		char string1[100] = {0};
		printf("enter the string\n");
		fgets(string1,100,stdin);
		if (string1[strlen(string1)-1] == '\n') string1[strlen(string1)-1] = '\0';
		printf("\n\n");
		i=0;
		do
		{
			equal = (_strnicmp(idioms[i],string1,strlen(idioms[i])));
			if(equal==0)
				{
				printf("you opted for entry number %d , %s \n",i,idioms[i]);
				flag = !equal;//flag indicates that a match was found.
				};
			++i;
		}
		while(!flag && i<=3);
		if (flag == 0)  // no matching entries found
			fputs ("\nThe input string didnt match any of the entries!\n\n", stdout);
		fputs("do u wish to continue?\n",stdout);
		choice = getchar();
	}
	while(choice != 'n' && choice != 'N');
	return 0;
}
It has the following problems though:
The program works fine during when the outer do-while loop is being executed the first time. It then asks me if I'd like to continue the program. If I enter anything other than a 'n' or a 'N' ,what happens is that it asks me for the string but it doesn't wait for me to enter it again and it keeps on asking the same question again and again whether Id like to continue or not until I enter a 'N' or a 'n'.

Here are the sample out for various cases.
Say if I enter 'check for palindrome' and I indicate that I would like to continue then heres what I see on my console:
Code:
enter the string
check for palindrome


you opted for entry number 3 , check for palindrome
do u wish to continue?
y
enter the string


do u wish to continue?
n
Press any key to continue
As you can see, if it not waiting for any input from the user until it asks the same question again. It only responds to a n in that the program quits properly.

Heres the sample output if I enter anything to generate a mismatch:
Code:
enter the string
sdnfsnfddf



The input string didnt match any of the entries!

do u wish to continue?
y
enter the string



The input string didnt match any of the entries!

do u wish to continue?
n
Press any key to continue
As you can see, somehow the compiler tends to remember the contents of string1. To counter that I added a line of code to define and initialize all values of string1 to zero each time the outer do while loop is executed.

But we cannot directly jump to the conclusion that it is remembering the contents of string1 because it is behaving differently when a match is found. As can be seen for the case when I entered 'check for palindrome' for the first time and I indicated that I wished to continue, it didn't repeat the message "you opted for entry number %d, %s\n........."

This is in contrast with the case when a match wasn't found wherein it kept on repeating the message "The input string didnt match any of the entries!" after the 1st execution of the outer do-while loop.
Has it got to do anything with the stream here?
I hope that I have been simple and clear enough to put my problem so that you may have understood what I meant here. In addition i would like to inform you that I am using VC++ version 6, just in case if that might help you in diagnosing the problem.

Hope to hear from you soon,

Best Regards,
Aijaz.