Domanda

    required.close();
    destined.close();
}

This program should ask the user to enter a filename and an output filename. The input filename should have a message that would be encrypted or decrypted, depends on the user's choice. The program should have 3 functions, one to check the file's existence, one to encrypt/decrypt each character and one to encrypt/decrypt the whole file.

È stato utile?

Soluzione

There are some errors and some things to change:

  • in the encChar function this is wrong: if (flag = true). You should use a comparison operator (==):

    `if (flag == true)`
    

    Someone prefers this form (comparison terms swapped):

    `if (true == flag)`
    

    because wrong assignments are detected.

  • in the encFile function it should be:

    ofstream destined(outFile.c_str());
    

    It's a good idea to enable warnings (e.g. with GCC/CLANG use -Wansi -Wall -Wextra) so that unused variables/parameters are detected (in your case the string outFile input parameter).

Pay attention at Simple's advice (Try using an absolute path. It's probably related to your program having a different working directory to what you expect) and your program should work.

You could improve your code with some other changes:

  • place variables in the narrowest scope possible and initialize variables in the declaration, e.g.

    ifstream required;
    required.open(inFile.c_str());
    

    should be

    ifstream required(inFile.c_str());
    

    (and check for error before using the stream).

  • using namespace std is considered a bad practice.

  • coding style and formatting are pretty arbitrary, but code is much easier to follow if you always use the same style.

  • don't use global variables

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top