Array for loops

Dec 1, 2015 at 4:03pm
Solved thanks.
Last edited on Dec 1, 2015 at 6:10pm
Dec 1, 2015 at 4:07pm
You just put it in the same for-loop, but it just does something differently.

Your array is of size 7. Your first loop runs 6 times. Which is a problem. Remember array indexes start from 0. So your array of size 7 goes from 0-6, so the for-loop should look like this -

1
2
3
4
for(int i = 0; i < 7; i++)
{
    //Do ya thang here
}


That will run from 0-6.

Then you will need an identical loop, to print out each content within the array, and also store each individual value in the array (scores[0], scores[1] etc) inside a variable double total;

And then outside the for loop at the end just print out the total.
Last edited on Dec 1, 2015 at 4:07pm
Dec 1, 2015 at 4:08pm
Arrays start at index 0 & go to (x-1) so in this case 6. Your for loop starts at 1 and goes to 6, so you're losing one number.

As for putting in another for loop, you just add in a for loop. It will be practically the same loop as your first one, but with a different body.
Last edited on Dec 1, 2015 at 4:10pm
Dec 1, 2015 at 4:09pm


My cout statement seems to be flawed..
Last edited on Dec 1, 2015 at 6:10pm
Dec 1, 2015 at 4:12pm
I dont know what you mean by flawed, since it works perfectly fine for me. What seems to be the problem?

Also try implementing the second for-loop :)
Dec 1, 2015 at 4:16pm

Gives me the correct sum and everything.
Last edited on Dec 1, 2015 at 6:10pm
Dec 1, 2015 at 4:23pm
IS this suppose to happen?
Dec 1, 2015 at 4:25pm
If you enter seven numbers separated by a space character instead of a end of line character (enter) your output is to be expected. Remember the insertion operator>> stops processing input when it encounters a whitespace character, leaving any unprocessed characters in the input buffer for the next input operation.

Dec 1, 2015 at 4:27pm
You're supposed to press enter after each number, so it maybe should be -

1
2
cout << "Enter number " << i + 1 << " of 7: ";
cin >> scores[i];

Dec 1, 2015 at 4:30pm
@jlb

I don't think my program is storing in these numbers into an array/printing them out..

:(
Dec 1, 2015 at 4:43pm


Is this program doing what is being asked?
Last edited on Dec 1, 2015 at 6:09pm
Dec 1, 2015 at 4:51pm
Not exactly.

and then prints out each of them, followed by the total.


You never print out each of them. You can do that in the second for-loop.
Dec 1, 2015 at 5:00pm
@TarikNeaj

How exactly can I show the inputted numbers?
Dec 1, 2015 at 5:18pm
Dec 1, 2015 at 5:35pm

[/code]

How can I add spaces to the inputted numbers (after it printed the array?)
Last edited on Dec 1, 2015 at 6:09pm
Dec 1, 2015 at 6:09pm
Solved.
Dec 1, 2015 at 6:27pm
Do you have to remove your initial post?, other people may find this useful in the future.
Topic archived. No new replies allowed.