Reputation: 3
The code I have loads a encrypted text file, with the first line of this text file being a cipher that is used to decode the rest of the text file. I know that this error usually happens if the function isn't defined before main, but I have it defined in the header, so i'm not sure what is causing this problem.
#include <string>
#include <iostream>
#include <fstream>
std::string decrypt(std::string cipher, std::string text);
int main()
{
//variable
std::string fileName;
std::string cipher;
std::string text;
//prompt the user to enter the file name
std::cout << "Enter file name: ";
std::cin >> fileName;
//declare input stream
std::ifstream inFile(fileName);
//Checks if the file has been connected or not
if (inFile.is_open())
{
std::cout << "File found." << std::endl;
std::cout << "Processing..." << std::endl;
getline(inFile,cipher); //get the first line as one string
std::cout << cipher << std::endl; //TEST!!!!
std::cout << "\n" << std::endl; //TEST!!!!!!
while (inFile.good()) //continue running until an error occurs to get the rest of the text as second string
{
getline(inFile,text); //get the first line as one string
std::cout << text << std::endl; //TEST!!!!
}
}
else //if the file isn't connected
{
std::cout << "File not found." << std::endl; **(ERROR C3861 HERE)**
}
inFile.close(); //close the file
std::string finalResult = decrypt(cipher,text);
std:: cout << finalResult << std::endl;
return 0;
}
//Decrypt function: Takes 2 strings by reference, changes the second string into the unencrypted text
std::string decrypt(std::string cipher, std::string text)
{
char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //create a character array of the alphabet
char code[27]; //empty array
char oldText[500]; //empty array
strcpy(code,cipher.c_str()); //copies the cipher into the array
strcpy(oldText,text.c_str()); //copies the encrypted text into the array
for (int i = 0; i < 500;i++) //Loop through the entire encrypted text
{
int k = i; //creates and resets a variable for while loop
while (alphabet[k] != oldText[i])
{
k = k+1;
}
oldText[i] = alphabet[k]; //after a match is detected, swap the letters
}
std::string newText(oldText); // converts the array back into text
return newText;
}
Upvotes: 0
Views: 165
Reputation: 31394
Your problem is this line in decrypt
:
std::string text(oldText); // converts the array back into text
It does not convert 'oldText' back into text
; instead it declare a new local variable named test
. That new local variable conflicts with the parameter named text
. This is the line that is causing the C2082
. What you should have written is:
text = oldText;
Which is replace the contents of test
with what is in oldText
Although it would be simpler to skip that entirely and just return oldText
.
I don't get a C3861
when compling your code, plus the fact that you are showing the C2082
on the wrong line makes me think your source file is out of sync with your errors.
Upvotes: 1