Mushahid Hussain
Mushahid Hussain

Reputation: 4045

Not getting out of the loop

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;
}

Upvotes: 0

Views: 140

Answers (3)

Richard Chambers
Richard Chambers

Reputation: 17583

I would do something like the fallowing which uses standard C++ I/O.

#include <iostream>
#include <string>

using namespace std;

// you will need to fill out this table.
char arr[] = {'Z', 'Y', 'X'};
char key[] = {'A', 'B', 'C'};

int main(int argc, _TCHAR* argv[])
{
    string sInput;
    char   sOutput[128];
    int k;

    cout << "\n\nEnter the plain text you want to encrypt\n";
    cin >> sInput;

    for (k = 0; k < sInput.length(); k++) {
        char ch = sInput[k];

        for(int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
        {
            if(arr[i] == ch)
            {
                ch = key[i];
                break;
            }
        }
        sOutput[k] = ch;
    }
    sOutput[k] = 0;
    cout << sOutput;

    cout << "\n\nPause.  Enter junk and press Enter to complete.\n";
    cin >> sOutput[0];

    return 0;
}

Upvotes: 2

Wug
Wug

Reputation: 13196

Relying on old C libraries in C++ is yucky. Consider this alternative:

#include <iostream>
#include <string>

using namespace std; // haters gonna hate

char transform(char c) // replace with whatever you have
{
    if (c >= 'a' && c <= 'z') return ((c - 'a') + 13) % 26 + 'a';
    else if (c >= 'A' && c <= 'Z') return ((c - 'A') + 13) % 26 + 'A';
    else return c;
}

int main()
{
    // system("cls"); // ideone doesn't like cls because it isnt windows
    string outstring = "";
    char ch;
    cout << "Enter the plain text you want to encrypt: ";
    while(1)
    {
        cin >> noskipws >> ch;
        if(ch == '\n' || !cin) break;
        cout << (int) ch << " ";
        outstring.append(1, transform(ch));
    }
    cout << outstring << endl;
    cin >> ch;
    return 0;
}

Upvotes: 2

Jack
Jack

Reputation: 133577

Here the problem is probably the fact that getche() (unlike getchar()) just returns the first character when there are more then one inputed and you are on windows (othewise you wouldn't use cls) then the EOL is encoded with \r\n.

What happens is that getche() returns \r so your break is never actually executed. You should change it to getchar() even because getche is a non standard function.

You can even try to look for \r instead that \n in your situation but I guess the \n would remain in the buffer causing problems if you need to fetch any additional input later (not sure about it).

Upvotes: 3

Related Questions