Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with my code. My algorithm seems to be able to work when doing the enc

ID: 3727883 • Letter: N

Question

Need help with my code. My algorithm seems to be able to work when doing the encryption. Except, only for the first one. I think there is something wrong when i input from the file and my if statements. Please can you help me solve this problem.

CODE:

#include <iostream>
#include <fstream>

using namespace std;

void editDigits(int number, int storageArray[], ofstream &outData, int &i)
{
    int n = 1;

    while(number/n>=10)
    {
        n = n*10;
    }

   while(n!=0)
    {
            int digit = number/n;
            storageArray[i] = (digit+8)%10;
            number = number%n;
            i = i + 1;
            n=n/10;
    }
}

void swapDigits(int number,int storageArray[], ofstream &outData)
{
    int tempArray[4] = {0,0,0,0};

    tempArray[0] = storageArray[0];
    tempArray[1] = storageArray[2];
    tempArray[2] = storageArray[1];
    tempArray[3] = storageArray[3];

    storageArray[0] = tempArray[1];
    storageArray[2] = tempArray[0];
    storageArray[1] = tempArray[3];
    storageArray[3] = tempArray[2];

    outData << storageArray[0] << storageArray[1] << storageArray[2] << storageArray[3] << storageArray[4] << endl;

}

int main()
{
    ifstream inData;
    inData.open("input.txt");
    ofstream outData;
    outData.open("output.txt");

    int number = 0;
    int storageArray[100];
    char unknownoperation;
    int i = 0;

    while (!inData.eof())
    {

    inData >> unknownoperation >> number;

    if (unknownoperation == 'e')
    {
        editDigits(number,storageArray,outData,i);
        if (i>5)
        {
        outData << "-1" << endl;
        }
        else
        {
            swapDigits(number,storageArray,outData);
        }
    }

    else if (unknownoperation == 'E')
    {
        editDigits(number,storageArray,outData,i);
        if (i>5)
        {
        outData << "-1" << endl;
        }
        else
        {
            swapDigits(number,storageArray,outData);
        }

    }
    else
    {
        outData << "-1" << endl;
    }

    }

    return 0;
}

You are required to implement a simple encryption algorithm that operates on five-digit integer values. The encryption algorithm is: . Replace each digit by (the sum of that digit plus 8) modulus 10. . Then, swap the first digit with the third. » Lastly, swap the second digit with the fourtlh . The resulting five-digit number is the encrypted value You are required to write a computer programme in C++ that reads in a list of operations and numbers from a file and writes the results to an output file.

Explanation / Answer

Mistake was in line  while (!inData.eof()) as you are using i as reference variable and updating it.

So after encrytion value of i will 6 and It will be remain same through out complete file read.

So write i=0, in first line of while loop.

Please find the update code with that small modification only.

Please see below part of code carefully

while (!inData.eof())
{

//variable must be 0 before each encryption operation start
i=0;

***********************************************************************

#include <iostream>
#include <fstream>

using namespace std;

void editDigits(int number, int storageArray[], ofstream &outData, int &i)
{
int n = 1;

while(number/n>=10)
{
n = n*10;
}

while(n!=0)
{
int digit = number/n;
storageArray[i] = (digit+8)%10;
number = number%n;
i = i + 1;
n=n/10;
}
}

void swapDigits(int number,int storageArray[], ofstream &outData)
{
int tempArray[4] = {0,0,0,0};

tempArray[0] = storageArray[0];
tempArray[1] = storageArray[2];
tempArray[2] = storageArray[1];
tempArray[3] = storageArray[3];

storageArray[0] = tempArray[1];
storageArray[2] = tempArray[0];
storageArray[1] = tempArray[3];
storageArray[3] = tempArray[2];

outData << storageArray[0] << storageArray[1] << storageArray[2] << storageArray[3] << storageArray[4] << endl;

}

int main()
{
ifstream inData;
inData.open("input.txt");
ofstream outData;
outData.open("output.txt");

int number = 0;
int storageArray[100];
char unknownoperation;
int i = 0;

while (!inData.eof())
{

//variable must be 0 before each encryption operation start
i=0;

inData >> unknownoperation >> number;

if (unknownoperation == 'e')
{
editDigits(number,storageArray,outData,i);
if (i>5)
{
outData << "-1" << endl;
}
else
{
swapDigits(number,storageArray,outData);
}
}

else if (unknownoperation == 'E')
{
editDigits(number,storageArray,outData,i);
if (i>5)
{
outData << "-1" << endl;
}
else
{
swapDigits(number,storageArray,outData);
}

}
else
{
outData << "-1" << endl;
}

}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote