Sunday 15 March 2015

how to name a file with a string in c++? -



how to name a file with a string in c++? -

i want name file after user input.

for example: application asks name. user types in test , file called "test.txt".

my code:

using namespace std; string filename; cout << "name? "; cin >> filename; ofstream outfile(filename + ".txt"); outfile.close();

i programmed java before, maybe problem :)

edit: maybe whole code helps more:

#include <iostream> #include <iomanip> #include <cmath> #include <fstream> using namespace std; long double fac(long double num){ long double result= 1.0; for(long double i=2.0; i<num; i++){ result *=i; } homecoming result; } int main(){ long double z; string q; string filename; long double pi=0.0; for(long double k=0.0;k<10.0;k++){ pi += (pow(-1.0,k) * fac(6.0 * k) * (13591409.0 + (545140134.0 * k))) / (fac(3.0 * k) * pow(fac(k), 3.0) * pow(640320.0, 3.0 * k + 3.0/2.0)); } pi *= 12.0; cout << "wie viele nachkomma-stellen? "; cin >> z; cout << setprecision(z+1) << 1.0/pi << endl; cout <<"als datei schreiben? [(y)es/(n)o]"; cin >> q; if(q=="y" or q=="yes" or q=="y" or q=="yes"){ cout << "dateiname? "; cin >> filename; ofstream outfile(filename + ".txt"); outfile << setprecision(z+1) << 1.0/pi << endl; outfile.close(); cout << "als" + filename + ".txt gespeichert"; } homecoming 0; }

i want compute pi. first project on c++. nil special, want larn basics, problem now.

compiler tell me:

g++ -wall -c "test.cpp" (in directory: /home/christian/documents/programmierung/programme/test) test.cpp: in function ‘int main()’: test.cpp:32:37: error: no matching function phone call ‘std::basic_ofstream<char>::basic_ofstream(std::basic_string<char>)’ ofstream outfile(filename + ".txt"); ^ test.cpp:32:37: note: candidates are: in file included test.cpp:4:0: /usr/include/c++/4.8/fstream:640:7: note: std::basic_ofstream<_chart, _traits>::basic_ofstream(const char*, std::ios_base::openmode) [with _chart = char; _traits = std::char_traits<char>; std::ios_base::openmode = std::_ios_openmode] basic_ofstream(const char* __s, ^ /usr/include/c++/4.8/fstream:640:7: note: no known conversion argument 1 ‘std::basic_string<char>’ ‘const char*’ /usr/include/c++/4.8/fstream:625:7: note: std::basic_ofstream<_chart, _traits>::basic_ofstream() [with _chart = char; _traits = std::char_traits<char>] basic_ofstream(): __ostream_type(), _m_filebuf() ^ /usr/include/c++/4.8/fstream:625:7: note: candidate expects 0 arguments, 1 provided /usr/include/c++/4.8/fstream:599:11: note: std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&) class basic_ofstream : public basic_ostream<_chart,_traits> ^ /usr/include/c++/4.8/fstream:599:11: note: no known conversion argument 1 ‘std::basic_string<char>’ ‘const std::basic_ofstream<char>&’ compilation failed.

thanks helpful answers

you using c++ dialect older c++11, , can't pass std::string std::ofstream constructor; it's not supported until c++11. however, can pass char const *, , std::string::c_str() method homecoming pointer appropriate utilize here:

// creates temporary std::string , calls c_str() on it, passing result // std::ofstream constructor. ofstream outfile((filename + ".txt").c_str());

c++ file-io

No comments:

Post a Comment