473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Re: AFL (Australian Football League)

Bert said:
On Jul 11, 9:07 am, Bert <albert.xtheunkno...@gmail.comwrote:
<snip>
> FILE* in = fopen("aflin.txt" , "r");
<snip>
>
'You fail to check whether this call succeeded - fopen returns a null
pointer if it cannot open the file. You need to decide what you should
do in that circumstance, and then add code to carry out that plan if the
file fails to open. For example, you might prompt the user to give you a
different filename, or you might simply print an error message and
quit. But you can't just *assume* that the file will be opened
successfully, not if your goal is to become a good programmer. '

FILE* in = fopen("aflin.txt" , "r");
if (in == NULL) { printf("Error"); return 0; }
Just don't go crazy over its neatness.
Your next line is:

FILE* out = fopen("alfout.txt", "w");

You fail to check whether this call succeeded - fopen returns a null
pointer if it cannot open the file. You need to decide what you should
do in that circumstance, and then add code to carry out that plan if the
file fails to open. For example, you might prompt the user to give you a
different filename, or you might simply print an error message and
quit. But you can't just *assume* that the file will be opened
successfully, not if your goal is to become a good programmer.

Moving on... you read from your input file as follows:

fscanf(in, "%d", &nseats );

but you don't check whether this call succeeded. The fscanf function yields
a result that is either EOF (if an input error occurs before any
conversion) or 0 (if, say, the input doesn't make sense to fscanf in the
light of the format specifier, e.g. SIX instead of 6), or the number of
fields successfully converted. In your case, you want one field to be
converted, so you want fscanf to return 1. Your code should check that it
does, and you should decide what to do in the event that it doesn't. Many
learners decide simply to abort the program at this point. That's
acceptable while you're learning, but a more robust response would involve
explaining the problem to the user and offering another chance to provide
good input.

You have just provided some evidence that you are prepared to respond to
advice, which is why I've provided two tips this time rather than just
one. If you fix up your code again, next time I'll give you three. If you
don't want all three of them to be "check the result of /this/ fscanf
call", you can avoid that by adding code to check the results of all of
the fscanf calls, not just the one I mentioned above, before posting again
- this will save you a fair bit of time.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 13 '08 #1
5 3018
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Your code should check that it
does, and you should decide what to do in the event that it doesn't. Many
learners decide simply to abort the program at this point. That's
acceptable while you're learning, but a more robust response would involve
explaining the problem to the user and offering another chance to provide
good input.
Nah, programming comps don't need crap.
You have just provided some evidence that you are prepared to respond to
advice, which is why I've provided two tips this time rather than just
one. If you fix up your code again, next time I'll give you three. If you
don't want all three of them to be "check the result of /this/ fscanf
call", you can avoid that by adding code to check the results of all of
the fscanf calls, not just the one I mentioned above, before posting again
- this will save you a fair bit of time.
-Little child to mummy-'I feel so special, mother'

[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.

FILE* in = fopen("aflin.txt" , "r");
if (in == NULL) { printf("Error"); return 0; }
FILE* out = fopen("alfout.txt", "w");
if (in == NULL) { printf("Error"); return 0; }

int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }

richard_heathfield = fscanf(in, "%d", &t );
if (richard_heathfield != 1) { printf("Error"); return 0; }

int booked[t];
int i = 0;
for (; i < t; i++)
{
richard_heathfield = fscanf(in, "%d", &booked[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}

richard_heathfield = fscanf(in, "%d", &b);
if (richard_heathfield != 1) { printf("Error"); return 0; }
int bookings[nseats];

for (i = 0; i < b; i++)
{
richard_heathfield = fscanf(in, "%d", &bookings[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}
Jul 13 '08 #2
Bert said:
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Your code should check that it
does, and you should decide what to do in the event that it doesn't.
Many learners decide simply to abort the program at this point. That's
acceptable while you're learning, but a more robust response would
involve explaining the problem to the user and offering another chance
to provide good input.

Nah, programming comps don't need crap.
And yet you seem determined to supply it. Fair enough.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 13 '08 #3
Bert <al*****************@gmail.comwrites:
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Your code should check that it
does, and you should decide what to do in the event that it doesn't. Many
learners decide simply to abort the program at this point. That's
acceptable while you're learning, but a more robust response would involve
explaining the problem to the user and offering another chance to provide
good input.

Nah, programming comps don't need crap.
I want my program to check that my test input is OK even if the
customer never cares.
[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.
int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }

richard_heathfield = fscanf(in, "%d", &t );
if (richard_heathfield != 1) { printf("Error"); return 0; }

int booked[t];
int i = 0;
for (; i < t; i++)
{
richard_heathfield = fscanf(in, "%d", &booked[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}

richard_heathfield = fscanf(in, "%d", &b);
if (richard_heathfield != 1) { printf("Error"); return 0; }
int bookings[nseats];

for (i = 0; i < b; i++)
{
richard_heathfield = fscanf(in, "%d", &bookings[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}
Good programmers never choose to repeat code like this. There is an
obvious candidate for a function here. In fact my solution to this
problem includes:

int read_num(FILE *fp)
{
static unsigned long line = 0;
int n;
line += 1;
char first[2];
if (fscanf(fp, "%1[0-9+-]", first) != 1 ||
ungetc(first[0], fp) == EOF ||
fscanf(fp, "%d\n", &n) != 1) {
fprintf(stderr, "Input error on line %lu.\n", line);
exit(EXIT_FAILURE);
}
return n;
}

I was prepared to discuss methods and compare code, but you seem to
have gone of the rails with sarcasm.

--
Ben.
Jul 14 '08 #4
Bert <al*****************@gmail.comwrites:
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Your code should check that it
does, and you should decide what to do in the event that it doesn't. Many
learners decide simply to abort the program at this point. That's
acceptable while you're learning, but a more robust response would involve
explaining the problem to the user and offering another chance to provide
good input.

Nah, programming comps don't need crap.
I want my program to check that my test input is OK even if the
customer never cares.
[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.
int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }

richard_heathfield = fscanf(in, "%d", &t );
if (richard_heathfield != 1) { printf("Error"); return 0; }

int booked[t];
int i = 0;
for (; i < t; i++)
{
richard_heathfield = fscanf(in, "%d", &booked[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}

richard_heathfield = fscanf(in, "%d", &b);
if (richard_heathfield != 1) { printf("Error"); return 0; }
int bookings[nseats];

for (i = 0; i < b; i++)
{
richard_heathfield = fscanf(in, "%d", &bookings[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}
Good programmers never choose to repeat code like this. There is an
obvious candidate for a function here. In fact my solution to this
problem includes:

int read_num(FILE *fp)
{
static unsigned long line = 0;
int n;
line += 1;
char first[2];
if (fscanf(fp, "%1[0-9+-]", first) != 1 ||
ungetc(first[0], fp) == EOF ||
fscanf(fp, "%d\n", &n) != 1) {
fprintf(stderr, "Input error on line %lu.\n", line);
exit(EXIT_FAILURE);
}
return n;
}

I was prepared to discuss methods and compare code, but you seem to
have gone of the rails with sarcasm.

--
Ben.
Jul 14 '08 #5
Bert wrote:
>
.... snip ...
>
-Little child to mummy-'I feel so special, mother'

[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.

FILE* in = fopen("aflin.txt" , "r");
if (in == NULL) { printf("Error"); return 0; }
FILE* out = fopen("alfout.txt", "w");
if (in == NULL) { printf("Error"); return 0; }

int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }
Congratulations. You have achieved my PLONK file. Bye.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 14 '08 #6

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

Similar topics

2
by: Domestos | last post by:
Hi all, Does anybody know of a fantasy football application written in php/mysql I could purchase and run a league with? Thanks Andrew Makinson
6
by: binger | last post by:
Okay, Superbowl is only a few days away, but I thought it would be cool to set up an online registration for a football squares pool. My thoughts are to have people go into anywhere on the 10x10...
0
by: BrainKr | last post by:
Still have 4 openings in our CBS SportsLine.com Fantasy Football league. Champ wins $200 prize. If interested, go to: http://football.sportsline.com/splash/football Register for the GOLD...
3
by: Ram | last post by:
Hi, I am trying to set up a DB for a race series where a riders best 6 of 10 rides count towards a league position. I have a table of Riders ( say 300 ) I want to use a form to select which...
2
by: edwgib2002 | last post by:
Does anyone have any information related to a scheduling algorithm that can be written in VBA and used in an access database to create balanced team schedules for a league?
2
by: steve.falzon | last post by:
Hi Folks The new footy season is almost with us, in England anyway :) I've just created a public league on the Times **FREE** fantasy football league game so if anyone wants to join up please...
3
by: Ben Bacarisse | last post by:
Bert <albert.xtheunknown0@gmail.comwrites: <snip> Do you have an example of a "slow" data set. Obviously I can make my own, but such puzzles often come with data sets. I have a simple...
2
by: minus | last post by:
public class Test { public static void main (String arg) { // "012345678910" alignment guide String results = {"AR 4 BR 5", "CR 0 DR 1",...
4
by: minus | last post by:
import java.util.Arrays; import java.util.Collections; public class Test { public static void main (String arg) { // "012345678910" alignment guide String ...
1
by: alan5 | last post by:
I want to answer on the topic australian postcode db with longitude/latitude posted by jack. He wrote: not sure exactly where I should ask this question, but im looking for a database of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.