0

I am trying to run the Caesar program from understanding C++ & it won't stay open once debugged. What to do?

I have used system("pause") to no avail. I have also tried getchar(), which has worked in other applications but does not work for this executable.

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

using namespace std;

/** 
Encrypts a stream using the Caesar cipher
@param in- the stream to read from
@param out- the stream to write to 
@param k- the encryption key
*/

void encrypt_file(ifstream& in, ofstream& out, int k)
{
    char ch;
    while (in.get(ch))
    {
        out.put(ch + k);
    }
}

int main(int argc, char* argv[])
{
    int key = 3;
    int file_count = 0; // The number of files specified
    ifstream in_file;
    ofstream out_file;

    for (int i = 1; i < argc; i++) //Process all command-line arguments
    {
        string arg = argv[i]; // The currently processed argument

        if (arg == "-d") // The decryption option
        {
            key = -3;
        }
        else // It is a file name
        {
            file_count++;

            if (file_count == 1) // The first file name
            {
                in_file.open(arg.c_str());
                if (in_file.fail()) // Exit the program if opening failed
                {
                    cout << "Error opening input file " << arg << endl;
                    return 1;
                }
            }
        }
    }

    if (file_count != 2) // Exit if the user didn't specify two files
    {
        cout << "Usage: " << argv[0] << " [-d] infile outfile" << endl;
        return 1;
    }

    encrypt_file(in_file, out_file, key);

    getchar();
    //system("pause");

    return 0;
}

Expected result is the application staying open while deciphering the code.

DerStarkeBaer
  • 669
  • 8
  • 28
  • The program will run until main is finished. The fact that it is returning means execution is finished(baring any faults). It also doesn't look like you're defining out_file anywhere? – BTables Sep 17 '19 at 01:07
  • `out_file` is *declared* at the top of `main()`, below `in_file`, but `out_file` is never actually *opened* before `encrypt_file()` is called. – Remy Lebeau Sep 17 '19 at 01:08
  • @RemyLebeau It's declared, not defined...Where is it supposed to be writing to? – BTables Sep 17 '19 at 01:11
  • This may be an XY problem. It sounds like maybe what you really want is for the *console* to stay open after the application terminates. That would be something to configure in your OS / windowing system / development environment, not something that would be part of your program. – Nate Eldredge Sep 17 '19 at 01:27
  • 2
    Possible duplicate of [Preventing console window from closing on Visual Studio C/C++ Console application](https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio) – Nate Eldredge Sep 17 '19 at 01:29
  • Possible duplicate of [Alternative to system("PAUSE")?](https://stackoverflow.com/questions/36797905/alternative-to-systempause) –  Sep 17 '19 at 02:41
  • Did you set a breakpoint? – JaMiT Sep 17 '19 at 03:51
  • I suggest you could try to explicitly set the Subsystem to Console under Configuration Properties / Linker / System. – Jeaninez - MSFT Sep 17 '19 at 06:10
  • Try: `Debug / Start Without Debugging` or just debug and set a breakpoint at the closing brace of `main`. – harper Sep 17 '19 at 10:44

2 Answers2

0

You're not actually pointing the ofstream to anywhere. As far as I can tell an ofstream will buffer input until you give it somewhere to point to(disclaimer, I could be wrong).

Regardless, since you're not opening anything for out_file, it will not write to whatever file you're wanting it to. Thus, it will look like your program isn't doing what you want and simply returning.

BTables
  • 4,413
  • 2
  • 11
  • 30
  • "*As far as I can tell an ofstream will buffer input until you give it somewhere to point to(disclaimer, I could be wrong)*" - if the `ofstream` is not opened, it simply won't write anywhere, and its `badbit` state will be set. – Remy Lebeau Sep 17 '19 at 01:24
0

You can try using system("pause/nul"); which i use when making prograns in VS because they wont stay open but i put them only before the return 0; so they close after i press a button but i think it works everywhere, give it a try.