![]() |
|
#1
|
|||
|
|||
homework problemHi everybody! I've been working on this program, that's due tomorrow and I have stumbled upon a problem that I can't figure out.
The program I'm doing suppose to input into the array names, id-numbers and years of service of the employees of non-existent company, the input is about 5 lines total each line looks kinda like this: Jason C. Greenway 642 8, and then I'm suppose to sort it all out by last name, id-number etc. I haven't done the sorting part yet, coz I'm having problems with input file that just wouldn't read into an array. Can anyone tell me what am I doing wrong? Here's the program: CPP / C++ / C Code:
Thanks in advance for your help. |
|
#2
|
||||
|
||||
|
You have defines your employee array to hold 10 employees, numbered 0 to 9. When you read the file:
CPP / C++ / C Code:
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#3
|
|||
|
|||
|
Quote:
I've tried that before, but it didn't work. |
|
#4
|
|||
|
|||
|
Quote:
I finally got it done, right before class, and it worked. But the thing I added didn't really make much sense. Turned out that in the for loop I had to put i < count - 1 which meant that it went through the loop 7 times although I only had 5 lines in my data file. Weird... |
|
#5
|
|||
|
|||
|
Quote:
Well, it's not weird at all, but it is kind of important, since your program shows something that is quite common, and is easy to understand (if you are interested) and easy to fix. I have abstracted your file reading process to the following: CPP / C++ / C Code:
Here is the test.txt file: CPP / C++ / C Code:
Here is the result of running the program: Quote:
The program printed the last line of the file twice. The reason is that the loop condition: while(inData) remains true until after the program has tried to read beyond the end of file. That means that the program runs through the loop after line 3 has been read. Trying to read more yields nothing from the file, but the variable line still has its old value: the last line that was read. If you want to use this procedure for file reading, you could do something like this: CPP / C++ / C Code:
Now, it only prints if inData is not in its "fail" state (hasn't tried to read past the end of file). The output is this: Quote:
There may be more elegant and robust ways of reading different things for files, but the principles are the same: test what you got before using it. Regards, Dave |
|
#6
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
One reason I like this is it removes the if from the processing loop so only one comparison is needed. The thing I don't like is you have to set up two reads. __________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#7
|
|||
|
|||
|
Quote:
Actually, I rarely use either type. More often it's something like this: CPP / C++ / C Code:
But I did want to illustrate the point about reading after the end of the file. Your example is another way. Neither is particularly elegant (as far as my personally-defined sense of elegance goes), but they do show ways of getting it done. Regards, Dave |
|
#8
|
|||
|
|||
|
Now, I'm stuck on a different problem.
In my original program I need to take the printing part and make it a void function, which I have no idea how to do, coz I never really had those before and my book doesn't explain much ;( The main problem is I'm getting confused on declaring parameters for the void function. Plus the structure makes it even more compilcated. Here's what I've done so far: CPP / C++ / C Code:
it actually compiles it and all I get is this warning warning C4700: local variable 'i' used without having been initialized, but when I execute it here's what my my output looks like: Employee List: Name: ID: Years: -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 Where did I screw up? |
|
#9
|
||||
|
||||
|
The problem I believe is you redefined employeeList[] in the function so you are printing out bogus values. You need to pass the structure as a parameter to printIt().
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#10
|
|||
|
|||
|
Quote:
That did it. Thanks! |
Recent GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Please help! Insert into database problem | robsmith | MySQL / PHP Forum | 1 | 24-Apr-2005 03:44 |
| Need advice on a disturbing problem | JUNK KED | Open Discussion Forum | 6 | 31-Mar-2005 13:51 |
| Need Help Starting the following problem | helpme | C Programming Language | 1 | 24-Nov-2004 14:44 |
| Another FX 5600 problem (but with details that might shed light on this) | BobDaDuck | Computer Hardware Forum | 2 | 16-Apr-2004 07:53 |
| problem with php5 cgi installation | fab13 | Apache Web Server Forum | 3 | 19-Nov-2003 09:11 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The