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.
#include <iostream>
usingnamespace 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])
returntrue;
elsereturnfalse;
}
}
bool upperCaseCheck(char alphabet)
{
int code=(int) alphabet;
if (code >= 65 && code <= 90)
returntrue;
elsereturnfalse;
}
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;
}
elseif(!upperCaseCheck(letter))
{
cout<<"Invalid Character"<<endl;
}
elseif (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
Perhaps you should add abreak; 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.