I am using this program to implement Mono alphabetic cipher. The problem i am getting is when i input plain text it doesn’t get out of the loop when condition is met which is pressing the enter key.Here is my code.
int main()
{
system("cls");
cout << "Enter the plain text you want to encrypt";
k = 0;
while(1)
{
ch = getche();
if(ch == '\n')
{
break; // here is the problem program not getting out of the loop
}
for(i = 0; i < 26; i++)
{
if(arr[i] == ch)
{
ch = key[i];
}
}
string[k] = ch;
k++;
}
for(i = 0;i < k; i++)
{
cout << string[i];
}
getch();
return 0;
}
Here the problem is probably the fact that
getche()(unlikegetchar()) just returns the first character when there are more then one inputed and you are on windows (othewise you wouldn’t usecls) then the EOL is encoded with\r\n.What happens is that
getche()returns\rso your break is never actually executed. You should change it togetchar()even because getche is a non standard function.You can even try to look for
\rinstead that\nin your situation but I guess the\nwould remain in the buffer causing problems if you need to fetch any additional input later (not sure about it).