Computing.Net > Forums > Programming > simple c programming problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

simple c programming problem

Reply to Message Icon

Name: VinCny
Date: October 12, 2003 at 04:19:17 Pacific
OS: win xp
CPU/Ram: 450/384
Comment:

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***/



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: October 12, 2003 at 06:03:14 Pacific
Reply:

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.



0

Response Number 2
Name: VinCny
Date: October 12, 2003 at 06:29:09 Pacific
Reply:

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.


0

Response Number 3
Name: Ronin1
Date: October 12, 2003 at 20:01:59 Pacific
Reply:

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);



0

Response Number 4
Name: VinCny
Date: October 13, 2003 at 03:39:09 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: simple c programming problem

simple C++ compile problem in Linux www.computing.net/answers/programming/simple-c-compile-problem-in-linux-/1282.html

C Program Simple Question (HELP...) www.computing.net/answers/programming/c-program-simple-question-help/10559.html

Problem in running C programs www.computing.net/answers/programming/problem-in-running-c-programs/14538.html