naming the output file and writing to it

Hi all

I am having trouble giving the user the option to name the output file (using Dev-C++) and then writing to that particular output file. Here is my code thus far (note-some of the stuff I have in there is incomplete, I know):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
 #include<fstream>
 #include<iomanip>
 #include<cmath>
 using namespace std;
 
 const double e=2.71828183;
 const double c1=374200000, c2=14390;
 double E, frac;
 double wave_start, wave_final; 
 double wave_inc, temp;
 double test;
 
 
 void info_block();
 void file_print();
 void file_print_clean();

 int main ()
 {
     cout<<"Please enter a starting value for the wavelength range: ";
     cin>>wave_start;
     cout<<"Please enter an ending value for the wavelength range: ";
     cin>>wave_final;
     cout<<"Please enter a value for the wavelength increment: ";
     cin>>wave_inc;
     cout<<"Please enter a value for temperature in Kelvin: ";
     cin>>temp;
     
     string filename;
     ofstream outfile;
     cout<<"What is the name of the output file? (be sure to include extension)";
     cin>>filename;
     outfile.open(filename.c_str());

    info_block();
    
    for (wave_start; wave_final>=wave_start; wave_start=wave_start+wave_inc)
     {
       frac=(c2/(wave_start*temp));
       E=c1/((pow(wave_start,5))*((pow(e,frac))-1));
       cout<<"\t\t    "<<wave_start<<"\t\t      "<<E<<endl;
       filename.c_str()<<"\t\t    "<<wave_start<<"\t\t      "<<E<<endl;
     }
     
     
     
system("pause");
return(0);
}

void info_block()
{
     cout<<"\n\t\t  Energy report for T = "<<temp<<endl;
     cout<<"\n\t\t Wavelength";
     cout<<"\t\t  E "<<endl;
     cout<<"\t\t ---------- \t\t ---"<<endl;
}

void file_print()
{
 }


And I'm getting my error here:
 
 filename.c_str()<<"\t\t    "<<wave_start<<"\t\t      "<<E<<endl;


That's where I'm trying to write to the outfile. The error I'm getting is:

invalid operands of types `const char*' and `const char[7]' to binary `operator<<'

What am I doing wrong here? Any help would be appreciated! If I need to go about naming the outfile in a completely different method, just let me know. I am open to any and all help/changes. Thanks!
Also, is it possible to create more than one outfile for one program? Basically I need one version with the void info_block() in the outfile and one without it. I need the one without it so I can import it into MATLAB.
You need to send your output to the ofstream named outfile. Try:
outfile<<"\t\t "<<wave_start<<"\t\t "<<E<<endl;
Ah that worked nicely! I was hoping for a simple fix and I got one haha. Now to figure out how to make two outfiles.
same as the first one... or use outfile.close() and open it with another filename using outfile.open()...
Alright I'll give that a try. Like I said before, I'm trying to print one outfile with the info_block header and one without. As the program stands right now, it's making one outfile without the info_block header. Im fiddling around with it trying to get it to print the info_block header to the outfile, but I cant seem to get it to work. How would I go about this?
Topic archived. No new replies allowed.