Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Sorry for the NOOB question.. (http://www.velocityreviews.com/forums/t537118-sorry-for-the-noob-question.html)

Zach Heath 09-14-2007 12:32 AM

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


user923005 09-14-2007 12:50 AM

Re: Sorry for the NOOB question..
 
On Sep 13, 5:32 pm, Zach Heath <heat...@gmail.com> wrote:
> 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;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


No need for three fgets() calls per iteration.
Call fgets() once and then parse the string.
Strtok() is probably fine to peel apart the tokens.
You can also use the is*() functions declared in ctype.h such as
isdigit(), isalpha(), ispunct()

In summary:
1. Read a line of input
2. Separate the contents of the line into components. You could use
strtok() or even sscanf()
3. Process the components
4. Get the next line (if any) and repeat.



Lafatus 09-14-2007 12:58 AM

Re: Sorry for the NOOB question..
 
On Sep 13, 7:50 pm, user923005 <dcor...@connx.com> wrote:
> On Sep 13, 5:32 pm, Zach Heath <heat...@gmail.com> wrote:
>
>
>
> > 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;

>
> > }- Hide quoted text -

>
> > - Show quoted text -

>
> No need for three fgets() calls per iteration.
> Call fgets() once and then parse the string.
> Strtok() is probably fine to peel apart the tokens.
> You can also use the is*() functions declared in ctype.h such as
> isdigit(), isalpha(), ispunct()
>
> In summary:
> 1. Read a line of input
> 2. Separate the contents of the line into components. You could use
> strtok() or even sscanf()
> 3. Process the components
> 4. Get the next line (if any) and repeat.


Thanks for the help!


Barry Schwarz 09-14-2007 02:38 AM

Re: Sorry for the NOOB question..
 
On Fri, 14 Sep 2007 00:32:01 -0000, Zach Heath <heathzd@gmail.com>
wrote:

>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


Since the only input in your code is from stdin, what does this file
have to do with anything.

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


Exactly what did you type in at this point?

> myTot = inHrs * inRt;
>
> fgets( inName, sizeof( inName ), stdin);


And what did you type here?

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


And what did you type here?

I think at least part of your problem is you have inputs without
prompts and lost track of where you were. Did you remember to type a
dummy name for this last fgets (because the real name comes after you
input the figures)?

>/*
> };
>*/
> return 0;
>}



Remove del for email

Army1987 09-14-2007 10:09 AM

Re: Sorry for the NOOB question..
 
On Thu, 13 Sep 2007 19:38:16 -0700, Barry Schwarz wrote:

> On Fri, 14 Sep 2007 00:32:01 -0000, Zach Heath <heathzd@gmail.com>
> wrote:


>>I have an input file that looks like:
>>
>>1.5 2.5 Bob, Joe
>>4.5 5.5 Bob, Jolene

>
> Since the only input in your code is from stdin, what does this file
> have to do with anything.

[snip]
>>command line: ./a.out < inputFile.c

[snip]
> Exactly what did you type in at this point?


Maybe stdin isn't the keyboard in this program?

--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen


Army1987 09-14-2007 10:16 AM

Re: Sorry for the NOOB question..
 
On Fri, 14 Sep 2007 00:32:01 +0000, Zach Heath wrote:

> 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

[removing comments]
> int main()
> {
> float inHrs, inRt, myTot;
> char inName[16];
>
> do {
> scanf ( "%f%f", &inHrs, &inRt );

The first time, you read 1.5 and 2.5. All fine.
> myTot = inHrs * inRt;
>
> fgets( inName, sizeof( inName ), stdin);

Now you read up to the end of the line. Still fine.
> 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;

Here you assign 0 to those.
> } while ( fgets(inName, sizeof(inName), stdin) != NULL );

This will read the first 15 characters of the next line. After
them, scanf won't be able to find any float in the input, and will
fail. inHrs and inRt stay zero. myTot becomes 0, and fgets() reads
up to 15 characters till the end of the line, that is "Jolene\n".

You didn't actually mean do { ... } while (fgets(...)), did you?
> return 0;
> }


--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen


Lafatus 09-14-2007 03:53 PM

Re: Sorry for the NOOB question..
 
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.

I'm supposed to use stdin, however, it isn't the keyboard. It's
supposed to be the input from the file.


Charlie Gordon 09-14-2007 05:25 PM

Re: Sorry for the NOOB question..
 
"Lafatus" <heathzd@gmail.com> a crit dans le message de news:
1189785206.621202.105850@50g2000hsm.googlegroups.c om...
>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.

Your problem can typically be solved with a while loop:

while (fgets(buf, sizeof buf, fp)) {
// parse the buffer and perform appropriate task or report error
}

--
Chqrlie.



CBFalconer 09-14-2007 06:55 PM

Re: Sorry for the NOOB question..
 
Lafatus wrote:
>
> 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.
>
> I'm supposed to use stdin, however, it isn't the keyboard. It's
> supposed to be the input from the file.


This is a meaningless message, because Usenet is a 'best efforts'
delivery mechanism, with no guarantees. There is no reason to
assume your readers have ever seen, or ever will see, any previous
messages in the thread. This is why the practice is to always
quote enough of the previous message so that the complete message
makes sense standing by itself.

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.

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


Keith Thompson 09-14-2007 07:58 PM

Re: Sorry for the NOOB question..
 
"Charlie Gordon" <news@chqrlie.org> writes:
> "Lafatus" <heathzd@gmail.com> a crit dans le message de news:
> 1189785206.621202.105850@50g2000hsm.googlegroups.c om...
>>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?

(In case anyone is confused, that was a joke; "far loops" is a typo
for "for loops".)

--
Keith Thompson (The_Other_Keith) kst-u@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"


All times are GMT. The time now is 07:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.