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

Relevance
  • 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) {

    os<<token<<endl;

    }

    }

    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);

    cout<<*ip<<endl;

    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.

    #include<iostream>

    #include<fstream>

    #include<string>

    #include<cctype>

    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): ";

    cin>>inChoice;

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

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

    cin>>infileName;

    }

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

    cin>>outChoice;

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

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

    cin>>outfileName;

    }

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

    if(infileName!="") {

    infile.open(infileName.c_str());

    }

    if (outfileName!="") {

    outfile.open(outfileName.c_str());

    }

    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.