|
-
July 18th, 2006, 11:44 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|