Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf and loop

Reply
Thread Tools

scanf and loop

 
 
Owner
Guest
Posts: n/a
 
      03-28-2011
int v[31];
int dayc = 0; /* day count read */
int daytotal = 0; /* day total */


while (scanf("%*s %*s %d%*c", &daytotal) == 1){
v[dayc++] = daytotal;

data file contains

3-13-2011 tue 2234.93
4-30-2011 wed 4233.43
5-23-2011 mon 2314.98
..
..
..

it only read first line from data file and then quits.
I'm suspecting something out of argument format.
I added "%*c" for newline character at the end of line.
Is this becuase in dos, newline character is cr and lf.
should I put "%*2c"? then it will fix the problem?


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-28-2011
Owner <(E-Mail Removed)> writes:

> int v[31];
> int dayc = 0; /* day count read */
> int daytotal = 0; /* day total */
>
>
> while (scanf("%*s %*s %d%*c", &daytotal) == 1){
> v[dayc++] = daytotal;
>
> data file contains
>
> 3-13-2011 tue 2234.93
> 4-30-2011 wed 4233.43
> 5-23-2011 mon 2314.98
> .
> .
> .
>
> it only read first line from data file and then quits.
> I'm suspecting something out of argument format.


I suggest you "play computer". Take your input and follow the
description (say from a manual page or a C book) of what the scanf call
does. You will be surprised, I think.

I suggest this because I think you'll get more from working it out than
by someone telling you. If that does not work for you, post what you
think happens and I a sure someone explain where you've gone wrong.

> I added "%*c" for newline character at the end of line.
> Is this becuase in dos, newline character is cr and lf.
> should I put "%*2c"? then it will fix the problem?


No, reading one character is enough. If the input is a text string (it
will be by default) line endings a dealt with automatically -- an input
function will see a single \n character no matter how line endings are
represented on the host system.

--
Ben.
 
Reply With Quote
 
 
 
 
Owner
Guest
Posts: n/a
 
      03-28-2011
On Mon, 28 Mar 2011 03:48:33 +0100, Ben Bacarisse wrote:

> Owner <(E-Mail Removed)> writes:
>
>> int v[31];
>> int dayc = 0; /* day count read */
>> int daytotal = 0; /* day total */
>>
>>
>> while (scanf("%*s %*s %d%*c", &daytotal) == 1){
>> v[dayc++] = daytotal;
>>
>> data file contains
>>
>> 3-13-2011 tue 2234.93
>> 4-30-2011 wed 4233.43
>> 5-23-2011 mon 2314.98
>> .
>> .
>> .
>>
>> it only read first line from data file and then quits.
>> I'm suspecting something out of argument format.

>
> I suggest you "play computer". Take your input and follow the
> description (say from a manual page or a C book) of what the scanf call
> does. You will be surprised, I think.
>
> I suggest this because I think you'll get more from working it out than
> by someone telling you. If that does not work for you, post what you
> think happens and I a sure someone explain where you've gone wrong.
>
>> I added "%*c" for newline character at the end of line.
>> Is this becuase in dos, newline character is cr and lf.
>> should I put "%*2c"? then it will fix the problem?

>
> No, reading one character is enough. If the input is a text string (it
> will be by default) line endings a dealt with automatically -- an input
> function will see a single \n character no matter how line endings are
> represented on the host system.


I tried this, too

while (scanf("%*s %*s %d[\n]", &daytotal) == 1)
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-28-2011
Owner <(E-Mail Removed)> writes:

> On Mon, 28 Mar 2011 03:48:33 +0100, Ben Bacarisse wrote:
>
>> Owner <(E-Mail Removed)> writes:

<snip>
>>> while (scanf("%*s %*s %d%*c", &daytotal) == 1){
>>> v[dayc++] = daytotal;
>>>
>>> data file contains
>>>
>>> 3-13-2011 tue 2234.93
>>> 4-30-2011 wed 4233.43
>>> 5-23-2011 mon 2314.98

<snip>
>> I suggest you "play computer". Take your input and follow the
>> description (say from a manual page or a C book) of what the scanf call
>> does. You will be surprised, I think.

<snip>
> I tried this, too
>
> while (scanf("%*s %*s %d[\n]", &daytotal) == 1)


Did you mean %[\n]? What you wrote is something else altogether. You
can keep trying various options in the hope of hitting the correct one
eventually (the odds are not good) or you can follow through exactly
what it is that your code is doing, step by step. The specification of
what a scanf format does are not difficult but they are brutally exact
and you need to understand them to know what you've asked scanf to do
and what you should have asked scanf to do.

If, on the pother hand, you just need to pull some data out of a text
file and learning the details of scanf is not important to you, then you
should switch to using a more suitable tool. I know you are using a
Windows system but presumably there is something better than C for text
manipulation that could do the job. Awk or Perl come to mind.

--
Ben.
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      03-28-2011
On 03/28/2011 02:01 PM, Ben Bacarisse wrote:
> ... You
> can keep trying various options in the hope of hitting the correct one
> eventually (the odds are not good) or you can follow through exactly
> what it is that your code is doing, step by step.


Or you can post your attempts up to a newsgroup and hope someone will
fix them for you, so that you don't fail your assignment...

"The Gods do not protect fools. Fools are protected by more capable
fools." (Larry Niven)
 
Reply With Quote
 
Owner
Guest
Posts: n/a
 
      03-28-2011
On Mon, 28 Mar 2011 14:05:15 +0100, Mark Bluemel wrote:

> On 03/28/2011 02:01 PM, Ben Bacarisse wrote:
>> ... You
>> can keep trying various options in the hope of hitting the correct one
>> eventually (the odds are not good) or you can follow through exactly
>> what it is that your code is doing, step by step.

>
> Or you can post your attempts up to a newsgroup and hope someone will
> fix them for you, so that you don't fail your assignment...
>
> "The Gods do not protect fools. Fools are protected by more capable
> fools." (Larry Niven)


I finally got it

while (scanf("%*s %*s %d%*s\n", &daytotal) == 1)

Thank you all for replying


 
Reply With Quote
 
Peter 'Shaggy' Haywood
Guest
Posts: n/a
 
      04-01-2011
Groovy hepcat Owner was jivin' in comp.lang.c on Tue, 29 Mar 2011 3:13
am. It's a cool scene! Dig it.

> On Mon, 28 Mar 2011 14:05:15 +0100, Mark Bluemel wrote:
>
>> On 03/28/2011 02:01 PM, Ben Bacarisse wrote:
>>> ... You
>>> can keep trying various options in the hope of hitting the correct
>>> one eventually (the odds are not good) or you can follow through
>>> exactly what it is that your code is doing, step by step.

>>
>> Or you can post your attempts up to a newsgroup and hope someone will
>> fix them for you, so that you don't fail your assignment...
>>
>> "The Gods do not protect fools. Fools are protected by more capable
>> fools." (Larry Niven)

>
> I finally got it
>
> while (scanf("%*s %*s %d%*s\n", &daytotal) == 1)


Yes, but do you actually understand why that works? If not, then
perhaps you should review your C reference manual. What does the %d
conversion specifier of scanf() match? And what is the third field of
your input? So how does %d treat the third field of your input? And so,
what is left unread at the end of the line after the %d has matched? So
what, then, is the next scanf() call reading, and what effectively is
the third field of that?

--
Dig the sig!

----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
using scanf() in while loop obdict C Programming 6 03-02-2006 07:17 PM
scanf in a for loop C Programming 15 12-29-2004 04:07 PM



Advertisments