473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorry for the NOOB question..

I'm just starting C and haven't done programming for a few
years...could you guys please take a look at this? Thanks for your
time!

I have an input file that looks like:

1.5 2.5 Bob, Joe
4.5 5.5 Bob, Jolene

The first time through the do while loop it works fine but the second
time it looks like this:
Bob, Joe 38.50 12.75 490.88
Jolene 0.00 0.00 0.00
, Janet 0.00 0.00 0.00
- It cuts off last name and all of the floats show up zeros...

command line: ./a.out < inputFile.c

int main()
{
float inHrs, inRt, myTot;
char inName[16];

do {
/* while ( fgets(inName, sizeof(inName), stdin) ) {
*/ scanf ( "%f%f", &inHrs, &inRt );
myTot = inHrs * inRt;

fgets( inName, sizeof( inName ), stdin);

char * p = strchr( inName,'\n' );
if ( p != NULL ){
*p = '\0';
};

printf ( "%s\t%4.2f %4.2f %4.2f \n", inName, inHrs,
inRt, myTot );
inHrs = 0;
inRt = 0;
} while ( fgets(inName, sizeof(inName), stdin) != NULL );
/*
};
*/
return 0;
}

Sep 14 '07
19 1809
"Keith Thompson" <ks***@mib.or ga écrit dans le message de news:
ln************@ nuthaus.mib.org...
"Charlie Gordon" <ne**@chqrlie.o rgwrites:
>"Lafatus" <he*****@gmail. coma écrit dans le message de news:
11************* *********@50g20 00...legro ups.com...
>>>I wasn't really planning on using a do/while, I wanted to use a while
loop but reached that point where I was trying everything and was
getting frustrated that I couldn't complete such a simple program.

As a rule of thumb, do/while loops are more difficult master than regular
while or far loops.

Let me guess, "far loops" are a DOS-specific extension that allows the
code for the loop body to span more than one memory segment, right?
You too remember the Pharlap days ;-)
(In case anyone is confused, that was a joke; "far loops" is a typo
for "for loops".)
yes, many typos in this post (un loupé)

As a rule of thumb, do/while loops are more difficult to master than regular
while or for loops.

--
Chqrlie.
Sep 14 '07 #11
CBFalconer <cb********@yah oo.comwrites:
[...]
You should also realize that google is not the messageing system,
it is only a rather poor form of message reader, with which you can
interface via http. It is so bad, and attracts so many Usenet
ignorant newbies, that many readers simply ban all messages
originating on google. So you would be well advised to get a
proper Usenet message reader, such as Thunderbird.
That's a trifle overstated. It is possible to post properly to Usenet
using Google Groups. (But I think the folks who manage to do so are
mostly people who have used real newsreaders in the past.)

The previous poster erred in posting a followup with no context, but
that's not Google's fault; the misfeature that encouraged this was
corrected some time ago, and it's a mistake that can easily be made
with a real newsreader as well.

See <http://cfaj.freeshell. org/google/>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '07 #12
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
[...]
>You should also realize that google is not the messageing system,
it is only a rather poor form of message reader, with which you
can interface via http. It is so bad, and attracts so many Usenet
ignorant newbies, that many readers simply ban all messages
originating on google. So you would be well advised to get a
proper Usenet message reader, such as Thunderbird.

That's a trifle overstated. It is possible to post properly to
Usenet using Google Groups. (But I think the folks who manage to
do so are mostly people who have used real newsreaders in the past.)
But finding out how to post properly through Google will not get
anyone past the traps set to eliminate all google posters. A
proper newsreader will.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 15 '07 #13
Charlie Gordon wrote:
>
.... snip much banditry ...
>
yes, many typos in this post (un loupé)

As a rule of thumb, do/while loops are more difficult to master
than regular while or for loops.
Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 15 '07 #14
"CBFalconer " <cb********@yah oo.coma écrit dans le message de news:
46************* **@yahoo.com...
Charlie Gordon wrote:
>>
... snip much banditry ...
>>
yes, many typos in this post (un loupé)

As a rule of thumb, do/while loops are more difficult to master
than regular while or for loops.

Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.
My own experience proofreading millions on lines of C code tells me that.
It is not a matter of complexity... it is just more error prone.
do/while loops written by average programmers tend to have more bugs than
regular for or while loops.
do/while loops produced by newbies tend to be always wrong.
A great classic is this:

int c;
/* skip to end of line */
do {
c = getc(fp);
} while (c != '\n');

Of course regular while loops can be bogus too:

while (!feof(fp)) {
fgets(buf, sizeof buf, fp);
/* do something or naught */
}

Please do the test, take any project at random to which you can access the
source code and examine the do/while loops, you will find bugs!

--
Chqrlie
Sep 15 '07 #15
"Charlie Gordon" <ne**@chqrlie.o rgwrites:
"CBFalconer " <cb********@yah oo.coma écrit dans le message de news:
46************* **@yahoo.com...
>Charlie Gordon wrote:
>>>
... snip much banditry ...
>>>
yes, many typos in this post (un loupé)

As a rule of thumb, do/while loops are more difficult to master
than regular while or for loops.

Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.

My own experience proofreading millions on lines of C code tells me
that.
....and my experience teaching programming leads to the same
conclusion. There seems to be something seductive about do ... while.
So much so that I just dropped the form from lecture notes as soon as
I saw the problem.

It may be that it reads naturally. It might also be that one of the
rare cases when it is correct:

do {
print prompt;
get reply;
} while (reply not suitable);

crops up in beginners programs more often than production code and the
idea sticks.

<snip>
A great classic is this:

int c;
/* skip to end of line */
do {
c = getc(fp);
} while (c != '\n');
Yes, but that is not wrong because of the do .. while. You probably
intended to leave out the declaration of c (or at least to add
.... suggesting more intervening code) so that this code comes after
some other input that might already have set c to '\n' (at least that
is the form of this bug I've seen repeatedly in student code).

--
Ben.
Sep 15 '07 #16
"Ben Bacarisse" <be********@bsb .me.uka écrit dans le message de news:
87************@ bsb.me.uk...
"Charlie Gordon" <ne**@chqrlie.o rgwrites:
>"CBFalconer " <cb********@yah oo.coma écrit dans le message de news:
46************* **@yahoo.com...
>>Charlie Gordon wrote:

... snip much banditry ...

yes, many typos in this post (un loupé)

As a rule of thumb, do/while loops are more difficult to master
than regular while or for loops.

Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.

My own experience proofreading millions on lines of C code tells me
that.

...and my experience teaching programming leads to the same
conclusion. There seems to be something seductive about do ... while.
So much so that I just dropped the form from lecture notes as soon as
I saw the problem.
I'm glad to see I'm not alone in the desert.
It may be that it reads naturally. It might also be that one of the
rare cases when it is correct:

do {
print prompt;
get reply;
} while (reply not suitable);

crops up in beginners programs more often than production code and the
idea sticks.

<snip>
>A great classic is this:

int c;
/* skip to end of line */
do {
c = getc(fp);
} while (c != '\n');

Yes, but that is not wrong because of the do .. while. You probably
intended to leave out the declaration of c (or at least to add
... suggesting more intervening code) so that this code comes after
some other input that might already have set c to '\n' (at least that
is the form of this bug I've seen repeatedly in student code).
That's a good point, but there is worse: this loop will run forever if the
stream ends without a new-line.
The classic idiom is safer:

while ((c = getc(fp)) != EOF && c != '\n')
continue;

or if EOF is a condition worth reporting:

while ((c = getc(fp)) != '\n') {
if (c == EOF) {
report_error("u nexpected end of file);
break;
}
}

For some reason, people tend to overlook special cases when they program
even the simplest tasks with do/while loops.

--
Chqrlie.
Sep 15 '07 #17
Ben Bacarisse <be********@bsb .me.ukwrites:
"Charlie Gordon" <ne**@chqrlie.o rgwrites:
>"CBFalconer " <cb********@yah oo.coma écrit dans le message de news:
46************* **@yahoo.com...
>>Charlie Gordon wrote:

... snip much banditry ...

yes, many typos in this post (un loupé)

As a rule of thumb, do/while loops are more difficult to master
than regular while or for loops.

Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.

My own experience proofreading millions on lines of C code tells me
that.

...and my experience teaching programming leads to the same
conclusion. There seems to be something seductive about do ... while.
So much so that I just dropped the form from lecture notes as soon as
I saw the problem.
[...]

I learned Pascal before I learned C. In Pascal, I used
REPEAT
...
UNTIL <condition>;
(similar to C's do-while) much more than I used
WHILE <conditionDO
BEGIN
...
END;

In C, I think the last time I used a do-while loop was in my IOCCC
entry.

Pascal's I/O model, unlike C's, encourages test-at-the-bottom loops.
(Well, C's I/O model seems to encourage such loops too, but not in
correct code.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #18
On Sep 15, 1:10 pm, Keith Thompson <ks...@mib.orgw rote:
Ben Bacarisse <ben.use...@bsb .me.ukwrites:
"Charlie Gordon" <n...@chqrlie.o rgwrites:
"CBFalconer " <cbfalco...@yah oo.coma écrit dans le message de news:
46EB2D39.3E79C. ..@yahoo.com...
Charlie Gordon wrote:
>... snip much banditry ...
>>yes, many typos in this post (un loupé)
>>As a rule of thumb, do/while loops are more difficult to master
than regular while or for loops.
>Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.
My own experience proofreading millions on lines of C code tells me
that.
...and my experience teaching programming leads to the same
conclusion. There seems to be something seductive about do ... while.
So much so that I just dropped the form from lecture notes as soon as
I saw the problem.

[...]

I learned Pascal before I learned C. In Pascal, I used
REPEAT
...
UNTIL <condition>;
(similar to C's do-while) much more than I used
WHILE <conditionDO
BEGIN
...
END;

In C, I think the last time I used a do-while loop was in my IOCCC
entry.

Pascal's I/O model, unlike C's, encourages test-at-the-bottom loops.
(Well, C's I/O model seems to encourage such loops too, but not in
correct code.)
To me, the choice is clear:

1. If you always want to perform the code _at least_ once, then use
do {} while(cond);
2. If you may not want to perform the code even one time (because
cond is false), then use while (cond) {} ;

I don't see any reason why one is easier or harder to understand than
the other. To me the complexity is identical.

Sep 17 '07 #19
On Sep 15, 1:10 pm, Keith Thompson <ks...@mib.orgw rote:
>Ben Bacarisse <ben.use...@bsb .me.ukwrites:
>>"Charlie Gordon" <n...@chqrlie.o rgwrites:
"CBFalcone r" <cbfalco...@yah oo.coma écrit dans le message de news:
46EB2D39.3E7 9C...@yahoo.com ...
Charlie Gordon wrote:
>>
>As a rule of thumb, do/while loops are more difficult to master
>than regular while or for loops.
>>>>Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.
>>>My own experience proofreading millions on lines of C code tells me
that.
>>...and my experience teaching programming leads to the same
conclusion. There seems to be something seductive about do ... while.
So much so that I just dropped the form from lecture notes as soon as
I saw the problem.

[...]

I learned Pascal before I learned C. In Pascal, I used
REPEAT
...
UNTIL <condition>;
(similar to C's do-while) much more than I used
WHILE <conditionDO
BEGIN
...
END;

In C, I think the last time I used a do-while loop was in my IOCCC
entry.

Pascal's I/O model, unlike C's, encourages test-at-the-bottom loops.
(Well, C's I/O model seems to encourage such loops too, but not in
correct code.)

To me, the choice is clear:

1. If you always want to perform the code _at least_ once, then use
do {} while(cond);
2. If you may not want to perform the code even one time (because
cond is false), then use while (cond) {} ;

I don't see any reason why one is easier or harder to understand than
the other. To me the complexity is identical.
The debate is not so simple.
The example I gave is typical of erroneous uses do/while:

people write

do {
operation;
maybe_more_oper ations;
} while (cond);

where they should write

while (operation, cond && other_cond) {
maybe_more_oper ations;
}

or

for (;;) {
operation;
if (!cond)
break;
if (!other_cond)
break;
}

definitely the do/while *seems* simpler and more straightforward , but
it tends to be bogus with conditions ignored or tested at the wrong
place.

--
Chqrlie.
Sep 18 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1950
by: Dave Williams | last post by:
First off...total noob to VB. So far have learned a lot in 1.5 days and feel fairly comfortable at throwing screens up. However, I want to start writing forms that revolve around an access database. From what I have read to date it looks like VB can only display 1 record at a time. I suspect this is not the case. How easy is it to display 10 records at a time (in a table format) that can be scrolled up and down ? When the current...
10
2142
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character (no, I don't use perl, never have). Not possible in python. What are some good options that people commonly use so that they can easily and quickly identify variable with just a glance? When I was writing stuff in SoftImage XSI/JScript, many of...
1
1809
by: davestrike | last post by:
I am a noob to sql and asp programming. I am working on a db for the gaming squad I am a member of. One of the pages I created is a roster list of all the squad members. Part of this roster is listing each member's email address. What several people have asked of me is to make it so the email addresses can be clicked on to open their email programs, just as html allows the mailto function to work. Here is a copy of the coding I am...
8
2147
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what option.
2
1915
by: Dan McCollick | last post by:
Hi All, Noob question that I can not seem to figure out: I am trying to implement a screenscraper to pull data from two seperate websites, here is some test code so far: public static void Main(string args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);
0
1488
by: AndyW | last post by:
Hey folks. I am trying to get a soap wsdl service working and have a bit of a noob php programming question for it. I'm using PHP 5.x btw. I have written a soap server that contains a single method in the form: SayHelloWorld( $name ) { return "Hello " .$name; };
4
1100
by: jobs | last post by:
1. How do I pass a subroutine a reference of an object? For example I have variable datef type datetime. I want to pass to pass datef the variable, not it's value to the sub? 2. In ADO.NET, how can I see what the parsed value of of a SQL command is after @parameters have been passed in?
1
3555
by: Ray Buck | last post by:
I've been trying to install Mailman, which requires a newer version of the Python language compiler (p-code generator?) than the one I currently have on my linux webserver/gateway box. It's running a ClarkConnect 2.01 package based on Red Hat 7.2 linux. I downloaded the zipped tarball (Python-2.4.4.tgz), ran gunzip, then un-tarred it in /usr/local. Then (logged in as root) from /usr/local/Python-2.4.4 I ran the configure script which...
6
1636
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going to be more specific... All I need to do at the moment is figure out how to replace our usage of .ini files as configuration files for our small, utility type apps. I'm not looking to use XML as a data stream or anything like that. Not right now,...
1
3544
by: SCRIPT KEEPER | last post by:
Hello, I am a complete noob and just starting off with csharp so I apologize for my basic question. I am wanting to start powershell from inside a batch script and then to pass the powershell args from the batch, so from the batch I can use the name of the compiled csharp app without having to include the "call" command. Something like "Compiled Csharp app.exe" "PowerShell Args" etc..... Thank You for any help.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.