C++ Deleting pointers after printing?

I have a program the accepts multiple forms of output including console. The problem is if they choose console I cannot delete the istream pointer until after the program has run. Is there a way to make the program send its output to the console then delete my istream pointer.

3 Answers

  • Ari
    Lv 6
    8 years ago
    Favorite Answer

    why do you need that for?

    just send the ostream and istream by reference to the program.

    like this:

    void print(istream& os, ostream& is) {

    string token;

    while(is>>token) {




    now there are no pointers (at least explicitly). moreover, you only "delete" a pointer after a call to "new". if I just do:

    int x = 10;

    int* ip = &x;

    i dont have to delete anything.

    the following needs a delete:

    int* ip = new int(10);


    delete ip;

    what this does is it frees the memory that you asked for after you called new.

    I'll write a program that uses your idea.





    using namespace std;

    int main() {

    char inChoice;

    char outChoice;

    string infileName="";

    string outfileName="";

    ifstream infile;

    ofstream outfile;

    cout<<"What do you want to use for input? (F for file or K for keyboard): ";


    if (tolower(inChoice)=='f') {

    cout<<"Which file do you want to open? ";



    cout<<"What do you want to use for output? (F for file or K for keyboard): ";


    if (tolower(outChoice)=='f') {

    cout<<"Which file do you want to open? ";



    //infile.open() takes a c-string so you need to call string::c_str() function to convert C++ string into C string

    if(infileName!="") {



    if (outfileName!="") {



    if (infileName=="" && outfileName=="") {

    print(cin, cout);

    } else if (infileName=="") {

    print(cin, outfile);

    }else if (outfileName=="") {

    print(infile, cout);

    }else {

    print(infile, outfile);



    hope this helps

  • 4 years ago

    How are you deleting the string? i'm extraordinarily beneficial it rather is doing this by using fact the tactic you're utilising is erasing the variable array yet no longer the reminiscence area. it rather is the two that or the string has been set to null. the subsequent question is, in case you merely want to cout or printf sparkling, why no longer merely set the array to all areas or something?

  • Anonymous
    8 years ago

    Just get a better IDE, I sometimes like to use notepad++ because I like the font :D but other than that just get a better one, or press CONTROL + COMMA and you should go to your preferences and configure them there.

Still have questions? Get your answers by asking now.