Computer Science Canada [C] - Simple problem I assume - displaying one output |
Author: | INFERNO2K [ Wed Nov 10, 2004 5:05 pm ] | ||
Post subject: | [C] - Simple problem I assume - displaying one output | ||
My program is a payroll system. It asks the user to enter 5 payrates to be stored in an array. Than five times, it asks the user for their name, last name, hours worked and which payrate option they are. I want the program to display all five people in the output at the end. For now it is only displaying the last person I entered. I need it to display all five. "Weekly Gross pay for ABC Company" Name Grosspay First Last xxx.xx First Last xxx.xx First Last xxx.xx First Last xxx.xx First Last xxx.xx I am using minGW compiler for Windows. |
Author: | wtd [ Wed Nov 10, 2004 5:27 pm ] | ||||
Post subject: | |||||
Consider:
You're scanning into the first element in the array each time. Instead, try:
|
Author: | INFERNO2K [ Wed Nov 10, 2004 5:30 pm ] |
Post subject: | |
That just stops the program after I enter the first name. |
Author: | wtd [ Wed Nov 10, 2004 5:37 pm ] |
Post subject: | |
I think the best advice I can give you (aside from writing the whole darn thing myself), is to write part of it, get that to work, then move on. Test often! That's pretty much what they're gonbna tell you at Ars too. |
Author: | INFERNO2K [ Wed Nov 10, 2004 5:44 pm ] |
Post subject: | |
As you can see I've written the entire program. I am confused on what I need to do fix it. |
Author: | wtd [ Wed Nov 10, 2004 5:58 pm ] |
Post subject: | |
INFERNO2K wrote: As you can see I've written the entire program.
Yeah. Kinda your problem. When you program you need to do it in steps. Lots of small steps. If you know one thing is working fine, you can move onto the next piece without worrying about the one you've tested. If you try to write the whole program, then test, you have a lot more to debug. To me, if I had to identify a trouble spot, I'd say it's your arrays. Learning more about dealing with arrays will likely help you. |
Author: | wtd [ Wed Nov 10, 2004 6:10 pm ] | ||||||
Post subject: | |||||||
One other quick suggestion. Your "another" function asks the user if they want to enter another name, then returns their input. Really, there are only two answers. Yes or no. This is the perfect scenario for a boolean value. C doesn't have a boolean type. Zero is false, and anything else is true. The "stdbool.h" header gives us some syntactic sugar for this, though.
Now, when you want to test, instead of writing out:
You could simpy write:
|
Author: | md [ Mon Nov 15, 2004 12:32 pm ] | ||
Post subject: | Re: [C] - Simple problem I assume - displaying one output | ||
Here's part of the main function; only better formated and commented
Problems: #1 you declare y as being an int... even though you always treat it as a char... #2 what is the point of the do {} while loop? it is garunteed to only execute once, and it's written to do so; the loop is pointless #3 asking if the user wishes to proceed, and then doing so anyway is pointless, perhaps if you wished to ask if they wished to enter more employees? (note you would have to store these employees as well...) #4 you say that the program only outputs the information for the last person, given that you always store an employees information at index 0, and then you only output the employee at index 0 (do you see a loop?)... To be blunt: your problem is a lack of thought. If you go through your code, and comment it it's fairly easy to see where errors like this are. |
Author: | Andy [ Mon Nov 15, 2004 3:50 pm ] |
Post subject: | |
couldnt have said it better myself |