read a string..writing a string

H

happy

How will I read a string using scanf in that function :
to the write what scanedf will be written using fprintf

customer_input(cust)
/*---------------------*/
struct customer_record *cust;
{
int i=0;
printf("\nEnter customer number :");
scanf("%4d",&(*cust).customer_no);
printf("\nEnter customer name :");
scanf("\n");
for (i=0;(*cust).customer_name != '\n' || (i<=20);i++)
scanf("%c",&(*cust).customer_name);
printf("\nEnter number of weeks rent due :");
scanf("%2d",&(*cust).no_of_weeks);
printf("Enter type of rental -c for colors TV");
printf("\n -b for black and white TV");
printf("\n -v for video");
printf("\n -o for other : ");
scanf("\n");
scanf("%c",&(*cust).tv_type);
return 0;


}
 
J

Jens.Toerring

happy said:
How will I read a string using scanf in that function :
to the write what scanedf will be written using fprintf

Sorry, but I am not able to parse this sentence in a way that would
make sense.
customer_input(cust)
/*---------------------*/
struct customer_record *cust;

Why do you use a method for function arguments that's at least
15 years outdated? Make that

int customer_input( struct customer_record *cust )
{

If you got this from a book it's really time to donate that book
to a museum. It's not good for anything else anymore.
{
int i=0;

This variable is never used in the function.
printf("\nEnter customer number :");

You need here a

fflush( stdout );

or the output may get stuck in the internal buffers of printf()
and friends. Or you must put a '\n' at the end of the output
string.
scanf("%4d",&(*cust).customer_no);

Do you know that "(*cust).customer_no" is the same as "cust->customer_no"
but the later is much easier to read?

But it would be much better if you would read in the user input as
a whole line, using fgets(), and then extract the number you are
looking for. That als gives you a chance to deal with garbage input.
Here you don't even attempt to do any checking...
printf("\nEnter customer name :");
scanf("\n");

I don't see what that's good for. I guess you want to skip everything
that might still be in the input buffer, but I am not sure.
for (i=0;(*cust).customer_name != '\n' || (i<=20);i++)
scanf("%c",&(*cust).customer_name);


Again, use fgets() to get everything at once instead. Also, did you
consider what would happen if there's already a '\n' stored in the
first element of the 'customer_name' array before you start reading?
Unless you initialized the array that's something that could happen.
And I also have some suspicion that the "i<=20" isn't correct. This
would allow the user to enter 21 characters and you would need the
'customer_name' array to be at least 22 characters long (don't forget
about the trailing '\0' scanf() will add). But I fear that the array
is only 20 characters long...
printf("\nEnter number of weeks rent due :");
scanf("%2d",&(*cust).no_of_weeks);

Again, use fgets() and then get the number out of that buffer.
printf("Enter type of rental -c for colors TV");
printf("\n -b for black and white TV");
printf("\n -v for video");
printf("\n -o for other : ");
scanf("\n");
scanf("%c",&(*cust).tv_type);

You tell the user to enter two characters, e.g. "-b", but you only
read the first one. If you want the user to only enter a single
character you should make this clearer. You also don't check if
what you got is what you need, i.e. either 'b', 'v' or 'o'. What
happens if the user enters 'X' instead?
return 0;
}
Regards, Jens
 
C

CBFalconer

happy said:
How will I read a string using scanf in that function :
to the write what scanedf will be written using fprintf
.... snip ...

I give up. You seem to be unable to take any instruction from
anybody, insist on posting things under brand new threads without
quotations, change subject lines, etc. Since you ignore all the
advice given you, PLONK. That means I, and others, will never see
your messages again, they will be automatically discarded.
 
T

Thomas Matthews

happy said:
How will I read a string using scanf in that function :
to the write what scanedf will be written using fprintf

In your C language reference manual, open the index
and search for "format specifiers". A good book will
have them listed for scanf and printf. Study them well,
especially for the side-effects and limitations.

Many people avoid scanf and instead input the data
into a buffer, then extract the data from the buffer.

Again, read the FAQs below, in their entirety, and
absorb the information. Soak the FAQ in water, then
use osmosis if you have to.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top