While loop using an array

I have most of the code after this written but I just need this one part to work in order to move on.

1
2
3
4
5
6
7
while (storeArray[i] != -999)
	{
		cin >> storeArray[i];
		i++;
		validEntries++;
		totalEntries++;
	}

The while loop doesnt stop when I input "-999".
Last edited on
Your loop never stops because you increment i after you get your input. You are always comparing the next array element after the one receiving the input.

That potentially can lead to undefined behavior after going out of bounds on your array.

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.
Hello BillyBJones,

That is because you have increased "i" by 1. The while condition is checking the next element in the array, which probably a garbage value, and although that garbage value is likely a negative number it is not (-999).

The (-999) you are looking for is actually at (i - 1), but changing the while condition to (i - 1) will not work either.

Andy
So then how do I fix it so that I keep using the array but it stops when "-999" is inputted?
Hello BillyBJones,

I came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (storeArray[i] != -999)
{
    // <--- Needs a prompt. Unless you like staring at a blank screen.
    std::cin >> storeArray[i];
        
    if (storeArray[i] != -999)
    {
        i++;

        validEntries++;  // <--- How do you know it is valid and what do you consider valid?

        totalEntries++;
    }
}

//storeArray[i] = 0;  // <--- If you do not want (-999) in the last element, but then "i" is 1 more than it should be.

I do not claim it is the best way or only way, but it does work.

Just had another thought I want to try.

Andy
Hello BillyBJones,

You could also do this:
1
2
3
4
5
6
7
8
9
10
const std::string PROMPT{ " Enter a number (-999 to quit): " };  // <--- Change as to you like.

while (std::cout << PROMPT && std::cin >> storeArray[i] && storeArray[i] != -999)
{
    i++;

    validEntries++;  // <--- How do you know it is valid and what do you consider valid?

    totalEntries++;
}


Andy
A simple full code example with an alternate way to detect -999 as a loop termination condition:
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
#include <iostream>

int main()
{
	int storeArray[1000] { };
	int i { };
	int validEntries { };
	int totalEntries { };

	std::cout << "Blah, blah, blah, enter -999 to quit:\n";

	while (true)
	{
		std::cin >> storeArray[i];

		if (storeArray[i] == -999) { break; }

		i++;

		validEntries++;
		totalEntries++;
	}

	std::cout << i << ' ' << validEntries << ' ' << totalEntries << '\n';
}

Blah, blah, blah, enter -999 to quit:
5
12
88
9
145
-3
-999
6 6 6

Of course i, validEntries and totalEntries will hold the same value since the exact same logic is used to compute them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
	const size_t MaxArr {20};
	const char* const PROMPT {"Enter a number (-999 to quit): "};
	int storeArray[MaxArr] {};

	size_t validEntries {}, totalEntries {};

	for (int val {}; totalEntries < MaxArr && (std::cout << PROMPT) && (std::cin >> val) && (val != -999); storeArray[totalEntries++] = val)
		if (true)	// Change condition for what is considered valid
			++validEntries;

	std::cout << validEntries << ' ' << totalEntries << '\n';
}


Note you need to specify the condition for a valid value - otherwise all numbers are considered valid.
Topic archived. No new replies allowed.