![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
display problem
Hi, I have a problem in the display of my program of cards(you guys helped me with):
I have in my input file 50 lines of cards. I am supposed to read every five cards. The problem I have is when I want to enumerate the hands. According to my file of 50 lines, I must have 10 hands. but when I execute the program, after displaying the tenth card, it also prints 'hand 11'. My output is like this: Hand 1 (prints the five cards) Hand2 (prints the five cards) . . . Hand 10 (prints the five cards) Hand 11 It is supposed to stop at Hand 10 (with 50 lines)!! How can I get rid of this last line "Hand 11"(without changing the whole code)??? My code is #include <stdio.h>
#include <string.h>
uhOh(char * error)
{
fprintf(stderr, "%s\n", error);
return 1;
}
main()
{
FILE * fin,*fout;
int i,number,seed, count = 5;
char source[256];
char destination[5][256];
fin=fopen("cp40610.dat","r");
fout=fopen("cp40610.out","w");
if((fin = fopen("cp40610.dat", "r")) == NULL)
{
return uhOh("Cannot open file");
}
number=0;
fscanf(fin,"%u",&seed);
fgets(source,20,fin);
while(1)
{fprintf(fout," Hand %d\n", number);
for(i = 0; i < count; i++)
{
if(fgets(source, 100, fin) == NULL)
{
if(ferror(fin))
return uhOh("An error has occured, check it with ferror()");
if(feof(fin))
{
fprintf(fout,"done\n");
return 0;
}
}
else
{
strcpy(destination[i],source);
fprintf(fout," %s\n", destination[i]);
}
number=number+1;
}
}
fclose(fin);
fclose(fout);
return 0;
}I tried to change the position of fprintf(fout," Hand %d\n", number); but i did not succeed. Can someone help me please? Thank you B |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I didn't look at your code, and since I can't see the contents of your file, there's not much point in it. Let me just give you a scenario. Suppose your file doesn't have a newline at the end. Your last fgets gets the last line, so it doesn't return NULL, so you don't check for EOF yet (as you shouldn't), but use the line. The next attempt to read hits EOF, everything exits, all is well. Suppose your file ends with a newline. The last read is terminated by the newline, not the EOF. You use the line. You loop around and print the next hand message. You read. You hit EOF. Woopsie, no worky por chitty, you have a hand message and nothing following.
The way people write their code, some of them need the newline in order for it to work properly. Other people need to NOT have the newline. Good ol' boys construct it so it doesn't matter. Just think about the situation of getting EOF with good data and without good data (remember the NULL), and work it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Location: USA
Posts: 9
Rep Power: 0
![]() |
you could take the easy way out and put an if statement in the code. Unless I missed something, I think your code would print Hand 0 first not Hand 1, because you initialize number to 0 and print it before you increment it.
//this if you initialize number to 0
if(number < 10)
fprintf(fout," Hand %d\n", number);
//this if you initialize number to 1
if(number <= 10)
fprintf(fout," Hand %d\n", number); |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I suggest he take the good programmer's way out. Just a personal outlook, of course, shaped perhaps by having to hire and fire people to keep a healthy bottom line.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|