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;
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.