I have text file that contains username and password

userandPassword.txt

chris ABC
jane DEF
john DEF

I am tryng to Decrypt the password using caesar cipher, with a key of 3 from the text file. which means that
if I key in username: chris and password:XYZ, it will login successfully.

however this is my password output after the decryption at this line of my codes

for(int x = 0; x < passwordInFile.length(); x++) {
          output += caesarDecrypt(passwordInFile[x]);
}
cout << output << endl;

XYZ
XYZABC
XYZABCABC

expected output of password output after the decryption

XYZ
ABC
ABC

this is what I so far

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
char caesarDecrypt( char c );

int main()
{
        string line;
        string userName;
        string password;
        string output = "";
        string userNameInFile;
        string passwordInFile;

        ifstream readFile("userandPassword.txt");
        cout << "Enter UserName: ";
        cin >> userName;

        cout << "Enter Password: ";
        cin >> password;


        while (getline(readFile,line)) {
            stringstream iss(line);
            iss >> userNameInFile >> passwordInFile;
            for(int x = 0; x < passwordInFile.length(); x++) {
                output += caesarDecrypt(passwordInFile[x]);
            }
            cout << output << endl;
            if (userName == userNameInFile && password == output) {
                cout << "Login Successfully!"<< endl;
            }
        }

}

char caesarDecrypt( char c )
{
    if( isalpha(c) )
    {
        c = toupper(c); //use upper to keep from having to use two seperate for A..Z a..z
        c = (((c+65)-3) % 26) + 65;
    }
    //if c isn't alpha, just send it back.
    return c;
}

Please help me.Thanks

Edited 2 Years Ago by roidbeginner

You are using one variable, output, to store the decrypted password. However, you are just adding each decrypted password onto the end of the last one in your variable output. This is bad.

Each time you're finished checking a password, reset output to blank.

Edited 2 Years Ago by Moschops

This question has already been answered. Start a new discussion instead.