473,799 Members | 3,085 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 #1
19 1808
On Sep 13, 5:32 pm, Zach Heath <heat...@gmail. comwrote:
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.
Sep 14 '07 #2
On Sep 13, 7:50 pm, user923005 <dcor...@connx. comwrote:
On Sep 13, 5:32 pm, Zach Heath <heat...@gmail. comwrote:
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!

Sep 14 '07 #3
On Fri, 14 Sep 2007 00:32:01 -0000, Zach Heath <he*****@gmail. com>
wrote:
>I'm just starting C and haven't done programming for a few
years...coul d 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
Sep 14 '07 #4
On Thu, 13 Sep 2007 19:38:16 -0700, Barry Schwarz wrote:
On Fri, 14 Sep 2007 00:32:01 -0000, Zach Heath <he*****@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

Sep 14 '07 #5
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

Sep 14 '07 #6
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.

Sep 14 '07 #7
"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.

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.
Sep 14 '07 #8
"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?

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

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

Sep 14 '07 #10

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

Similar topics

1
1949
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
2138
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
1808
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
2145
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
1486
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
1634
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
3542
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
9541
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
10251
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
10228
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
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
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
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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
2938
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.