Implementing Stack on Array

Hello everyone so I have a quick question, my code is supposed to only take 6 character values so I used an array to limit it to 6 but whenever I run it, it gives me an error. Could anyone help shed some light on this? :(

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
  include <iostream> 
#include <stack> 
using namespace std;
void reverseName(stack<char> &name);

int main() 
{
	stack<char> name; 
	char singleChar[6];

	cout << "Enter your name one character at a time: ";
	while (cin.get(singleChar[6]) && singleChar[6] != ' ') 
	{
		name.push(singleChar[6]);
	} 
	cout << "Your secret code name is: " << endl;
	reverseName(name); cout << endl;
	return 0;
}
void reverseName(stack<char> &name)
{
	
	while (!name.empty())
	{
		cout << name.top(); 
		name.pop();
	}
}
Keep in mind that an array of size 6 begins at index 0 and ends at index 5.

Line 12 attempts to access the array singleChar at index 6, which is out of bounds. This can cause the program to crash with an out of bounds error and should be avoided.

Arrays are not a good way to enforce length limitations. Consider using a loop and counter instead. For loops are well suited for that purpose. http://www.cplusplus.com/doc/tutorial/control/#for
Thank you I found out the problem!
Topic archived. No new replies allowed.