Loop/array question

Hi everybody!

A coding noob trudging through his first attempt at programming with the help of Stephen Prata's "C++ Primer Plus" here.

While working on an exercise, I've encountered the following situation: I've got a while loop that should end after I've filled up the array but it allows me to input one more value. This is an array of 5 ints and it outputs 5 ints, but while inputting, I can enter 6.
Why is that? Is there a mistake in the way I've formulated the while condition?

Thanks in advance.




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
  #include "stdafx.h"
#include <iostream>
const int MAX = 5;
using namespace std;

int main()
{
	int donations[MAX];
	int entry = 0;
	cin >> donations[entry];
		while (entry < MAX)
	{
		++entry;
		cin >> donations[entry];
	};

		cout << "Reading the array with a loop:\n";
		
		
		int read = 0;
		while (read < MAX)
	{
			cout << donations[read] << endl;
			++read;
		};
			
	
	system("pause");
    return 0;
}
Your second while loop is correct. The first one is not.
Compare your two while loops and look at the differences.

Line 10: Why are you doing a cin of donation[0] before the while loop?
Line 13: Why are you incrementing entry here? This is going to cause you to store donations[1] through donations[5]. donations[5] is out of bounds. The entries in your array are [0] - [4].


Hello everybody! This is my first time here :). I'm a beginner, 1st year in college/C++. I have this homework to get done.In the bottom of the page I attached what I tried to do.
I don't understand what the teacher is asking for. I'm confused... please give me a hint. thank you


Homework
"Write a C++ program that performs the following functions.
1) Part 1: Using the WHILE command. Start with a Message consisting of “***** Part 1: WHILE command *****”.
a. Displays the same message (Message #1) for ten (10) times.Set the number of times to be printed as a variable so I can change it when I test your program.
i. You define the message but define a message variable so that the message can be changed in ONE (1) place in the program.
b. Display a second message (Message #2) when the number of times that you have displayed message #1 is ONLY ODD.
i. Do not test for specific values (1, 3 etc).
ii. Do NOT display the message if the number of times that you display Message #1 is NOT odd.
c. Display a third message (Message #3) when Message #1 is ONE (1) less than its maximum number of times to be displayed.
i. You must not use a specific value or number for testing. That is, do not test for Value=9.
d. Display a fourth message (Message #4) when Message #1 is set to be processed (displayed) but has not yet processed.
i. That is, display Message #4 before the final Message #1.
e. Control the message output by putting the messages in GROUPs separated by a SINGLE line between groups.
i. A group is when your “Message counting variable” is the same value. A new group is when the “Message counting variable” changes value.




#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
// Program-specific constants
const string While_Message = "***** Part 1: WHILE command *****\n";
const string Message_1 = "This is a message specified in the program.\n";
const string Message_2 = "This should display only after odd numbers.\n";
const string Message_3 = "This should display only one before the last message.\n";
const string Message_4 = "This should display immediately before the final Message_1\n";
const string Message_Separator = "----------\n";



// Counting variable
int i;
int value; // Value to display

// While loop
cout << While_Message;
cout << " How many time you want the messege to be printed ?" << " Enter times: ";
cin >> value;
i = 1;
while (i <= value ) {
if (i == value) {
// Only display immediately before the final message
cout << Message_4;
}
cout << Message_1;
if (i % 2 == 1) {
// Only display after odd numbered messages
cout << Message_2;
}
if (i == value - 1) {
// Only display when the current number is one less than the maximum
cout << Message_3;
}
cout << Message_Separator;
i++;
}
................................................................................



@coraline01 - Please do not hijack someone else's thread.
Sorry, I tried the forum platform but it's not working.
I tried the forum platform

I don't know what that means.

At the top of the beginners page, press the "New Topic" button to start your own thread.


Thank you for your help, AbstractionAnon. I think I understand what mistakes I made.
Topic archived. No new replies allowed.