Run-Time Check Failure #2 - Stack around the variable was corrupted.

Hello, my first time posting here hoping someone can help me out. I'm trying to have a program that will take in 10 capital letters into an array and output them. It will output "invalid character" if the letter is not capital or it will output "duplicate character" if the capital letter has already been input. So at the end it should just display 10 non duplicate capital characters. This is my code but Whenever I have an invalid character it always just infinitely outputs "invalid character" and for the duplicate character it only recognizes the first duplicate character.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

void init (char CapitalCharacter[]) 
{
        for (int i = 0; i < 10; i++) 
		{
                CapitalCharacter[i] = '0';
        }
}


bool duplicateCheck(char a[], char alphabet, int i)
{
	for(int n = 0; n<(i+1); n++)
	{
		if (alphabet==a[n])
			return true;
		else
			return false;
	}
}

bool upperCaseCheck(char alphabet)
{
	int code=(int) alphabet;
	if (code >= 65 && code <= 90)
		return true;
	else
		return false;
}



int main ()
{
	char letter;
	char CapitalCharacter [10];

	cout<<"Enter 10 capital letters between A and Z"<<endl;

	for(int i=0; i<=10; i++)
	{
		cin >> letter;

		while(true)
		{
			if(upperCaseCheck(letter) && !duplicateCheck(CapitalCharacter,letter,i))
			{
				CapitalCharacter[i]=letter;
				break;
			}
			else if(!upperCaseCheck(letter))
			{
				cout<<"Invalid Character"<<endl;
			}
			else if (duplicateCheck(CapitalCharacter,letter,i))
			{
				cout<<"Duplicate Character."<<endl;
			}
		}
	}

	cout<<"The nonduplicate characters are "<<endl;
	for (int i = 0; i<10; i++)
	{
		cout<<CapitalCharacter[i]<<" ";
	}

	system("Pause");
	return 0;
}

Also if I take out the while loop just to check if the array is loading properly i get the following error message
Run-Time Check Failure #2 - Stack around the variable 'CapitalCharacter' was corrupted.
Thank you in advance
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(true)
		{
			if(upperCaseCheck(letter) && !duplicateCheck(CapitalCharacter,letter,i))
			{
				CapitalCharacter[i]=letter;
				break;
			}
			else if(!upperCaseCheck(letter))
			{
				cout<<"Invalid Character"<<endl;
			}
			else if (duplicateCheck(CapitalCharacter,letter,i))
			{
				cout<<"Duplicate Character."<<endl;
			}
		}


Perhaps you should add a break; to the else if(!upperCaseCheck(letter)) and else if (duplicateCheck(CapitalCharacter,letter,i)) sections. How else do you expect the while loop to ever stop?

If you mean the user to have to enter another letter in these cases, you should move the cin >> letter; to inside the while loop.
Last edited on
won't the variable i still increment :\ then wouldn't there be a empty spot in the array for that value of i?
Topic archived. No new replies allowed.