Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I had encounter some problem with mine programme project. I wish to store a string in the output. but with scanf i can't do it. if i use gets i will face some other problem too. this is mine source code.
the problem lays on add and edit record function of the source code. And i wish to want to create an error check on every integer or character input for incorrect input in char to int or int to char. I am a beginner. hope u guy can help.void add_edit_record(void)
{printf("\n\n\t*Note : Length of Title and Author Name cannot be\n");
printf("\t\tmore than 20 alphabet. Like wise book descrition\n");
printf("\t\tcannot tbe more than 100 alphabet use dot for spaces."); /***End of Note***/
printf("\n\n\n");
printf("\t\t\tTitle Name : ");
scanf("%s", &title);
fflush(stdin);
printf("\n\n");
printf("\t\t\tAuthor Name : ");
scanf("%s", &author);
fflush(stdin);
printf("\n\n");
printf("\t\t\tBuying Price : $");
scanf("%i", &b_price);
fflush(stdin);
printf("\n\n");
printf("\t\t\tSelling Price : $");
scanf("%i", &s_price);
fflush(stdin);
printf("\n\n");
printf("\t\t\tQuantity available : ");
scanf("%i", &quantity);
fflush(stdin);
printf("\n\n");
printf("\t\t\tBook Description : ");
scanf("%s", description);
fflush(stdin);
clrscr();printf("\n\n\n\n\n\n\n\n\n");
printf("\t\t\tDo you wish to save the record(y/n): ");
scanf(" %c", &save);
fflush(stdin);
printf("\n\n\n");system("PAUSE");
clrscr();
} /***Add/Edit Function End***/

I think that you need to use gets() or fgets() to input some of your values. When used with scanf(), the %s will input only up until the first whitespace. Thus fields like Title, Author & Description probably won't input correctly because in most cases those fields have whitespace in them.

I had try to use gets but it i do not why the first variable(title) was skipped and the last variable(description) cannot be shown
And about the error checking.. how do i do the error checking. for example i can onli input integer for int variable and character onli for char variable. Really need your help thanks.

I'd use fgets for all input, and convert other data types using the strto'X' family.
fflush(stdin) will lead to undefined behavior on some OSes.
char buf[64], book[64], *p;
int price;printf("enter book title: ");
fgets(book, sizeof(book), stdin);if((p = strchr(book, '\n')) != NULL)
*p = '\0'; /* strip away newline */printf("enter price: ");
fgets(buf, sizeof(buf), stdin);/* use isdigit in a loop to validate input */
price = (int)strtol(buf, &p, 10);

Thanks Ronin1, the source code you put is very useful to me. I got 1 more question.
That is i wish to do an error check for both int and char usage... It is like i can onli input int onli but not char... how i do it..
i try using the comparision of sizeof the variable, but failed. Can any one enlighten me.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |