Creating an array populating loop

I'm trying to create a loop that allows the user to populate an array through the keyboard and typing a command which allows the user to exit the program after the array has been populated. Here is what I have so far...unfortunately I keep getting errors and I think I know what the problem is. I would need to use something other than "n" because its already been declared as an array in my "while" and "if" statements. How can I modify my code to have the program detect when to exit or not?

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
34
35
  // Project 4 2.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace std;


int main()
{
	int n[501];
	int i=0;
	

	for(i=0; i < 501; i++)
	{
			while (n != -1 && i < 501)
		{   
			cout << "Please enter all your numbers and type -1 when you are done" << endl;
			cin >> n[i];
            

			if( n == -1)
			{
				break;
			
			}
		}

	}

    system("PAUSE");
    return 0;

}
Last edited on
1
2
3
4
5
6
7
8
char choice = 'a'; //If this becomes 'y' at any time, we exit the loop. 
//Put this in the loop.
std::cout << "Do you wish to quit? y/n \n"; 
cin >> choice; //Get a choice from the user.
if(choice == 'y')//If the user selects 'y', then break out of the loop. 
{
     break;
}

Topic archived. No new replies allowed.