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[i] != '\n' || (i<=20);i++)
scanf("%c",&(*cust).customer_name[i]);
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;
}
|
|
0
|
|
|
|
Reply
|
ehab_aziz2001 (252)
|
2/11/2005 12:50:54 PM |
|
happy <ehab_aziz2001@yahoo.com> wrote:
> 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[i] != '\n' || (i<=20);i++)
> scanf("%c",&(*cust).customer_name[i]);
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
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
|
|
0
|
|
|
|
Reply
|
Jens.Toerring (807)
|
2/11/2005 1:53:56 PM
|
|
happy wrote:
>
> 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.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
2/11/2005 6:16:06 PM
|
|
happy wrote:
> 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
|
|
0
|
|
|
|
Reply
|
Thomas_MatthewsSpamBotsSuck (408)
|
2/12/2005 4:17:00 PM
|
|
|
3 Replies
32 Views
(page loaded in 0.06 seconds)
Similiar Articles: reading scientific notation numbers from string - comp.soft-sys ...I am trying to read some numbers that are in scientific notation out of a string. An example string is: # Beam sigy = 0 m, muy = -2e-06 m, dy = ... converting a character string into a variable name - comp.lang ...You might need to preformat here CASE ('...') .... END SELECT WRITE(*,*) output_string ... names for n = 1:length(v) eval(['read(11,*) ' v(n)]) ; % -->read(11 ... Arena - Reading Formatted Input from File - comp.simulation ...Hi, I am working on a project in Arena. In this I need to read a text file which has rows starting with a string followed by 5 integers. The string i... Shortest way to read all lines (one by one) from a text file ...Ok, I know in general a way to read text lines ony-by-one from a file into a string variable. But I miss somehow a short one-liner like: foreach... write an greek symbol - comp.soft-sys.matlabHow can i write an greek symbol on a string. I read the matlab instructions(Text Properties), but i believe that doesn't work for uicontrol. How it ... read() error - Bad Address - comp.unix.programmer>> Many systems use write-protected memory for string literals, and you're >> trying to fill this in with read(). > I'm using two string literals. Some text processing questions - comp.lang.vhdlHello all, In an effort to read and process non-trivial command files for a transactor in a testbench, I came upon a couple of questions regarding text / string ... To combine string and integer in F90 - comp.lang.fortran ...Hello all, I would like to combine string and an integer number to write the result as string. Suppose, my string is 'str' and the integer numb... Iterating over a String - comp.lang.java.helpIt has bugged me that the for:each syntax would not let me write code of the form: String ... If you read the quotes above, you'll notice that Daniel wrote "64 bit ... error for netcdf.putVar when writing data to netcdf file - comp ...Reading and Writing FORTRAN Data - The IDL ... GRIB API, for accessing data ... of the ... to save the files ... taken to catch or ... >Well this error ... write read string ... How to: Write Characters to a String - Microsoft Corporation ...How to: Read and Write to a Newly Created Data File ... How to: Write Characters to a String How to: Read Characters from a String - Microsoft Corporation ...Console.WriteLine(b); // Read the rest of the string from the current ... How to: Write Characters to a String 7/20/2012 7:01:58 AM
|