@dsustudent59
don't thank me yet ;) let's move towards improving your understanding while making this work, shall we? :)
i believe you've understood the concept of array elements as stated above, and the concept of for loops.
in order to traverse through an array, that is, to go from element 0 to n
th element, we usually use a for loop. here's a basic syntax of a for loop
1 2
|
for (/*start*/; /*condition*/; /*update*/
/*action!*/
|
alrighty, with this, lets compare with the code you have below
1 2
|
for(int counter=0; counter < EMPID; counter++)
for(int ctr=0; counter < EMPID; ctr++)
|
int counter is zero as start, right. we must start from element 0.
counter less than EMPID? remember what i said about EMPID? EMPID (even here) is a variable. you should place the max number of elements in here.
word of caution. lets say i have int x[3], okay? here's a common mistake by most C students (in my class that is). a for loop runs until the condition is false. observe the condition below
1 2 3 4
|
for (int count=0; count<=3; count++)
{
//action!
}
|
note the
less than or equals to sign. lets start! count starts from zero.
so, count is 0. is 0
less than or equals to 3? true. perform action, update.
so, count is now 1. is 1
less than or equals to 3? true. perform action, update.
so, count is now 2. is 2
less than or equals to 3? true. perform action, update.
so, count is now 3. is 3
less than or equals to 3? true. perform action, update.
so, count is now 4. is 4
less than or equals to 3? false. stop loop.
how many times did the action take place?
FOUR times!
whats the size of your array?
THREE!
what are the elements in the array? count[0], count[1] and count[2]!
you did the loop 4 times, accessing 0, 1, 2 and 3.
there is no count[3], is it? no. there isn't! but you've done the loop 4 times! you should get a runtime error here.
the fix is to use
less than only. so it only goes, 0, 1, and finally 2, accessing all the elements of my count array! voila :D
i believe this could help a lot in your code, but one more problem...
1 2
|
for(int counter=0; counter < EMPID; counter++)
for(int ctr=0; counter < EMPID; ctr++)
|
why two for loops? you only need one :)
dsustudent59 wrote: |
---|
What I'm trying to do is have a loop inside of another loop. The inside loop should prompt the user for data and performs calculations. It go back to the outer loop and repeats for all employees. After the last loop, I would like it to display on the console the employee id number and the grosspay. |
no, you still do not need a for loop, in a for loop. nested for loops are complex levels of iteration, and you certainly do not need them here.
here's a logic example. i hope this helps
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
# include <iostream>
using namespace std;
int main()
{
int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
int hours[7];
double payRate[7];
double wages[7];
//we were right up to here :D
for (/*count=0*/; /*count less than...?*/; /*add count*/)
{
//get the employee working hours
//get the employee payrate
//calculate the wages. basic formula is to have 'wages=hour*payrate'.
//try implementing the array logic we've learned.
//remember, EMPID[0], has worked for hours[0] hours
//and his payrate is payrate[0]. his wages should be in wages[0]
}
//we're done with the calculation. let's show them out.
//note, always separate calculation and display processes.
//they may get more complex!
for (/*count=0*/; /*count less than...?*/; /*add count*/)
{
//show the wages!
}
system("pause");
return 0;
}
|