CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    19

    Question menu of character strings

    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.

  2. #2
    Join Date
    May 2006
    Posts
    327

    Re: menu of character strings

    I think you have to add "flag=0" before the inner loop:

    Code:
    flag=0;
    do
    {
        . . .
    } while(!flag && i<=3);
    Probably the getchar at the end leaves the '\n' character in the keyboard buffer, which is then read by next gets function. I think you should use another gets at the end in order to avoid this.

    I hope this helps.

  3. #3
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    19

    Question Re: menu of character strings

    Huy there.
    I corrected some mistakes and the program below seems to work. Atleast, it behaves better than the one above. In the program above, I initialised flag to 0 just once at the beginning of the outer do while loop.
    Now assume that when the program is run for the 1st time,flag becomes 1. This value of 1 gets carried over to the next run of the program if the user decides to continue.
    During the next run,the inner do-while executes once and if there is no match, the loop still terminates as the value of flag is still 1 from the previous run of the outer loop although there was no match found in the inner loop's first run.
    This value of flag also doesn't allow the inner if structure to execute.

    Hence the only major change I made was that I added a line of code to initialise flag to 0 during every run of the outer do-while loop. This took care of the problem stated above.
    Additionally I replaced the getchar with getche. (I don't know how that helped anyway ) but still I just did). If I use getchar instead of getche then the same old problems happen if I decide to continue. It doesn't wait for the user to input another string and keeps on asking if he still wants to continue.

    So heres the final working version and if someone can point out something where it still goes wrong (assuming that you enter the string with only single spaces between the words as I am not checking for multiple spaces for the time being).

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    {
    	const char *idioms[] = {"create array","free memory",
    							 "find smallest","check for palindrome"};
    	char choice;
    	int i,flag,equal;
    	//flag=0; removed this line..
    	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");
    		flag=0;//added this line...
    		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?\nenter a y or a Y for yes\n\n",stdout);
    		choice = getche();//replaced getchar with getche but I don't
    		fputs("\n",stdout);//understand why getchar isn't working fine
    	}
    	while(choice == 'y' || choice == 'Y');
    	return 0;
    }
    .

    Heres a sample of what I should be seeing as it is observed if I use getche instead of getchar
    Code:
    enter the string
    chdfsfasaf
    
    
    
    The input string didnt match any of the entries!
    
    do u wish to continue?
    enter a y or a Y for yes
    
    y
    enter the string
    check for palindrome
    
    
    you opted for entry number 3 , check for palindrome
    do u wish to continue?
    enter a y or a Y for yes
    So the only remaining question that I have now is that why is it not working with getchar. Heres a sample of what I see if I decide to use getchar instead of getche:
    Code:
    enter the string
    afsgmasf
    
    
    
    The input string didnt match any of the entries!
    
    do u wish to continue?
    enter a y or a Y for yes
    
    y
    
    enter the string
    
    
    
    The input string didnt match any of the entries!
    
    do u wish to continue?
    enter a y or a Y for yes
    
    y
    
    enter the string
    
    
    
    The input string didnt match any of the entries!
    
    do u wish to continue?
    enter a y or a Y for yes
    I press a y and press enter after which it prints "enter the string" and everything below that without waiting for the user to input anything.



    Hope this thread hasn't gone too far and that I don't sound like a grumpy old man stuck on finding out why something doesn't work when something else works perfectly fine
    Last edited by aijazbaig1; July 17th, 2006 at 09:19 AM.
    Best Regards,
    Aijaz Baig.

  4. #4
    Join Date
    Mar 2006
    Location
    New Jersey
    Posts
    120

    Smile Re: menu of character strings

    Hi aijazbaig1,

    Try using fflush at the beginning of the first do - while() loop.

    Code:
     
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    {
    const char *idioms[] = {"create array","free memory",
    "find smallest","check for palindrome"};
    char choice;
    int i,flag,equal;
    //flag=0; removed this line..
    do
    {
    fflush(stdin);
    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");
    flag=0;//added this line...
    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?\nenter a y or a Y for yes\n\n",stdout);
    choice = getchar();
    fputs("\n",stdout);//understand why getchar isn't working fine
    }
    while(choice == 'y' || choice == 'Y');
    return 0;
    }
    I hope I've been of some help.

  5. #5
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    19

    Lightbulb Re: menu of character strings

    Hello there.
    If u read the specification for the fflush function it very clearly mentions that its effect is undefined for input streams making the function a not too good choice for cleaning input streams.
    I do not know if standard C has any other functions which can do that.

    However I used a different method and it worked. I used to read and ignore characters until a newline character is encountered. I only check for the 1st character and make my decision on that.

    If you know of any other function which is a part of the standard C library which can flush and whose effect is defined even for input streams, please do let me know.

    Heres the code:
    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,flag,equal;
    	//flag=0; removed this line..
    	do
    	{
    		char string1[100] = {0};
    		do
    		{
    			fputs("enter the string\n",stdout);
    			fgets(string1,100,stdin);
    		}
    		while(*string1 == '\n' || *string1 == '\r');
    		if (string1[strlen(string1)-1] == '\n') string1[strlen(string1)-1] = '\0';
    		printf("\n\n");
    		flag=0;//added this line...
    		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);
    		do
    		{
    			fputs("do u wish to continue?\nenter a y or a Y for yes\n\n",stdout);
    			choice = getchar();
    		}
    		while (choice == '\n' || choice == '\r');
    		while (getchar() != '\n');
    		fputs("\n",stdout);
    	}
    	while(choice == 'y' || choice == 'Y');
    	return 0;
    }
    Best Regards,
    Aijaz Baig.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured