JBabyJ wrote: |
---|
Okay, stridexr, I think you're right...my problem may be with the loops. However, you said with the loop that the "for" executes n number of times. The following is the answer after I compiled it. Why didn't it execute n number of times like it said in the loop?
c
-17
Rick
0xffbffa70
53
-17
99
62 |
You're getting garbage output because the program is wrong. Based on your output, the only correct one that I can see is "Rick", so I will try and explain why the loop output "Rick". This is the part of the loop we are interested in:
1 2
|
for (Slot = 0; Slot <= 3; Slot++)
cout << First [Slot];
| |
Notice that "First" is defined as
char First [] = {'R', 'i', 'c', 'k'};
. This means that "First" is an array of characters or collection of the characters R, i, c and k, respectively. If we wanted to print the first letter 'R' we would use
cout<<First[0];
because this would call the first possible character in the array. Similarly, if we wanted to call the second letter we would use
cout<<First[1];
and so on, so forth.
Now back to the loop. We know that the loop is going to repeat itself
Slot number of times, where
Slot in this case starts at 0 (
for(Slot = 0 ...
) and the loop keeps on repeating itself until
Slot <= 3
. So the loop is going to repeat itself 4 times. (Since slot starts at 0 and ends up at 3, the loop will repeat for 0,1,2,3 - a total of 4 times.)
The key to remember here is that every time the loop executes,
Slot is increased by 1. Therefore, if we type
cout<<First[Slot];
in the body of the loop, it will do this when the program is run:
1 2 3 4
|
cout<<First[0];
cout<<First[1];
cout<<First[2];
cout<<First[3];
| |
And since we know that these hold the values 'R' 'i' 'c' and 'k' respectively, the output will of course read:
Hope that helped.