int main()
{
char FirstName[30], LastName[30];
int Age;
char FileName[255];
char filename[30];
cout << "\nEnter the name of the file you want to create: ";
cin >> FileName;
float a=10,b=12,c=13;
What I want to acheive is to name the file with the values of varliable a,b and c for example user enters test so the output file is saved with a name "test10.12.13". So far I've managed to save the file with the user specified name but I am struggling with putting the integers values of a,b and c as file name.
1. Don't #include <stdheader.h> but <stdheader>
eg:
1 2 3
#include <fstream>
#include <iostream>
#include <cstdio> // this is from C so it has the prefix 'c'
2. Don't #include <conio.h> it's old and non standard
3. Add std:: before standard symbols (eg std::cout) or add the line usingnamespace std; after the #includes
4. Don't use char arrays but strings from <string>
5. Use stringstreams from <sstream> to build complex strings ( http://www.cplusplus.com/reference/iostream/stringstream/ )
6. Use getline to get input ( http://www.cplusplus.com/forum/articles/6046/ )
7. Use [code][/code] tags when posting code
So here is a simplified version of you program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <fstream>
#include <sstream>
usingnamespace std;
int main()
{
string filename;
cout << "Enter Name: ";
getline(cin,filename); // get input
int a=10, b=12;
stringstream ss;
ss << filename << a << '.' << 'b'; // add your stuff to the stream
ofstream file ( ss.str().c_str() ); //get const char * with the contents of the stringstream and open file with that name for writing
file << "hello world";
}
Thanks for that but I am using an older version of c++ compiler i.e. Borland 3.1 and some of the things that are new dont work with borland such string etc etc.
However my program works perfectly all I am looking for is to save the data file with the integer values of a,b and c, for example test123.txt in case of a=1, b=2 and c=3.
Can you sugesst some way that I can print these values as file name?.
If your compiler doesn't compile standard C++ it isn't a good C++ compiler and you should get a new one
I recommend that people take Standard conformance very seriously when considering a compiler. If you can, avoid any compiler that doesn't closely approximate the ISO standard or fails to supply a solid implementation of the standard library. The recent releases from all the major C++ vendors do that.
If you are a novice and don't know how to see if a compiler is conformant, try this:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout << "Please enter your first name followed by a newline\n";
cin >> s;
cout << "Hello, " << s << '\n';
return 0; // this return statement isn't necessary
}
its a standard Borland compiler but an old version. It compiles everything fine as I said i need some method to print the integer values as part of the file name.
It doesn't compile ISO C++.
I insist because if you learn old C++ you won't learn current C++.
It doesn't compile fine.
If you want to program in C++ you should get a newer compiler. There are many available for free, why don't you get one?
Anyway if you still want to program in a bad old non standard C++ with a bad old compiler, you should #include<strstream.h> instead of <sstream>
So that lines from my example would be
1 2 3
strstream ss;
ss << filename << a << '.' << 'b'; // add your stuff to the stream
ofstream file ( ss.str() );
But that code is deprecated and if you use old C++ you can't say that you program in C++