3
votes

Okay, so I'm trying to write encrypted text to a file.

For some reason, I am getting a not declared in this scope compile error.

Here's my encryption.cpp file:

#include <iostream>
#include <fstream>
#include <string>

class encrypt{
    public:
        static void cryptText(std::string newpass)
        {
            int x = newpass.size();

            int output[x -1];

            for(int i = 0; i <= x -1; i++)
            {
                output[i] = (int)newpass[i];
                std::cout << char(output[i] + x);
                //Here I am trying to write output onto the file. For some reason, though,
                //I get the error.
                myfile.open ("passwords.thaddeus");
                myfile << output << std::endl;
                myfile.close();

            }

            std::cout << std::endl;

        }

};

I have looked at the cplusplus.com documentation for Input/Output with files. I copied their example program into Code::Blocks, and it worked perfectly. But when I try it, I get the error. It's strange because I included .

3
Could you reduce your code and information to the necessary minimum that still exhibits this specific error? Most of the information you provide isn’t relevant and just detracts from the problem. That said, the error is pretty self-explanatory. - Konrad Rudolph
Besides you have not declared myfile you got out of bounds array access at output[i]+x with i == 0 - Sergey
@Shafik Yaghmour I added that line, and instead get the same error but telling me ofstream was not declared in this scope. - Crju
@Syd Yes, that is b/c the sample also has using namespace std; which is bad so without that you need to do std::ofstream myfile;. - Shafik Yaghmour

3 Answers

8
votes

You neither declared nor defined your myfile variable.

So, it doesn't exist in the current context and that's why you are getting this error.

You've missed this part of code ofstream myfile;

5
votes

You miss the very basics of any language ... where is your myfile definition?

4
votes

First, you need to use ofstream, so do using namespace std;

Then declare your myfile object: ofstream myfile;

General rule of thumb: Standard libraries never include a variable prefixed with 'my' for you to just use without declaring.