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
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 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
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.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
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![]()




Reply With Quote