im trying to use stacks to output files with names here is what i have so far ive tried every combination of file.open i could think of any suggestions?

#include <iostream>
#include <string>
#include <fstream>
#include <stack>
using namespace std;

int main(){
    stack<string> filename;
    string fname;
    int count;
    ofstream file;
    while(count!=3){
        cin>>fname;
        count++;
        filename.push(fname);
    }
    while(!filename.empty()){
        cout<<filename.top()<<endl;
        filename.pop();
    }
    return 0;
}

Stacks work in reverse order, first in last out. If you want to pop the filename from the full stack, then empty the stack into a file with that name, the filename has to be the last element pushed on to the stack so that it's the first information popped out.

Edited 6 Years Ago by Fbody: n/a

i know but when i use file.open(filename.top()) it gives a couple of errors what im trying to do is during #

while(!filename.empty()){
#
cout<<filename.top()<<endl;
#
filename.pop();
#
}

i want to put a file out using the stack to pop the file out

>>i know but when i use file.open(filename.top()) it gives a couple of errors

because filename is a collection of string and open expects a const char * , try using the c_str() function on the string.

could you provide an example ?
this is all i came up with after looking at c_str()

#include <iostream>
#include <string>
#include <cstring>
#include <stack>
using namespace std;

int main(){

    stack<string> filename;
    string fname;
    char *fn, *n;
    fn= new char [filename.size()+1];
    strcpy(fn,filename.c_str());
    n=strtok(fn," ");
    int count;
    while(count!=3){
        cin>>fname;
        count++;
        filename.push(fname);
    }
    while(!filename.empty()){
        cout<<filename.top()<<endl;
        while(n!=NULL){
            cout<<n<<endl;
            n=strtok(NULL," ");
        filename.pop();
    }
    return 0;
}

and that returns quite a few errors

Edited 6 Years Ago by kourt32: n/a

Where is the original problem of doing file.open? and why suddenly all these strcpy, strtok functions? all you could have done is

file.open((filename.top()).c_str());

ok srry what im trying to do is use a stack to output multiple files
btw this isnt homework (this is just a hobby of mine) (only 17 no college here :))
i think i mistook how to use c_str();

outline:

user enters set amount of filenames
filenames are printed in reverse order then they are created

ok srry what im trying to do is use a stack to output multiple files
btw this isnt homework (this is just a hobby of mine) (only 17 no college here :))
i think i mistook how to use c_str();

outline:

user enters set amount of filenames
filenames are printed in reverse order then they are created

ok i just tried this

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <stack>
using namespace std;

int main(){
    stack<string> filename;
    string fname;
    int count;
    ofstream file;
    while(count!=3){
        cin>>fname;
        count++;
        filename.push(fname);
    }
    while(!filename.empty()){
        cout<<filename.top()<<endl;
        file.open(filename.top().c_str());
        file<<"hello";
        filename.pop();
    }
    return 0;
}

and it broke my while loop check for only 3 fnames

ok i just tried this

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <stack>
using namespace std;

int main(){
    stack<string> filename;
    string fname;
    int count;
    ofstream file;
    while(count!=3){
        cin>>fname;
        count++;
        filename.push(fname);
    }
    while(!filename.empty()){
        cout<<filename.top()<<endl;
        file.open(filename.top().c_str());
        file<<"hello";
        filename.pop();
    }
    return 0;
}

and it broke my while loop check for only 3 fnames

EDIT: for the post above

i did this and it outputs only 1 file with the file name in it but i would like to output 3 so what is breaking the check ?

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <stack>
using namespace std;

int main(){
    stack<string> filename;
    string fname;
    int count;
    ofstream file;
    if(count!=3){
        cin>>fname;
        count++;
        filename.push(fname);
    }
    while(!filename.empty()){
        cout<<filename.top()<<endl;
        file.open((filename.top()).c_str());
        file<<filename.top().c_str();
        filename.pop();
    }
    return 0;
}

admins i messed up could you delete my accidental double post

i did this and it outputs only 1 file with the file name in it but i would like to output 3 so what is breaking the check ?

You must initialize count to a value of your choice. It is NOT initialized to zero by your compiler.

can a admin remove my double post ?
also thanks mitrmkar i should have known that count doesnt have any value until its assigned :?

Edited 6 Years Ago by kourt32: n/a

so ive gotten to do what it should however it only outputs one file( but it prints three filenames)

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <stack>
using namespace std;

int main(){
    stack<string> filename;
    string fname;
    ofstream file;
    for(int count=0; count!=3; count++){
        cin>>fname;
        filename.push(fname);
    }
    while(!filename.empty()){
        cout<<filename.top()<<endl;
        file.open((filename.top()).c_str());
        file<<filename.top().c_str();
        filename.pop();
    }
    return 0;
}

i should have known that count doesnt have any value until its assigned :?

Hmm, you made it sound as if the variable would have vanished into thin air :|
Certainly it has a value - depending on the compiler/settings, the value may be random garbage or it may have a predefined value, intended to aim in spotting uninitialized variables. But at any rate, remember to initialize your variables - it's undefined behaviour to end up using uninitialized variables.

it only outputs one file( but it prints three filenames)

You want to do file.close() after you've written to a file.

Edited 6 Years Ago by mitrmkar: n/a

This article has been dead for over six months. Start a new discussion instead.