I am tyring to create a programme where it writes certain information to a certain file. How do you make the file that it writes equal to a string that is typed in by the user. What i Put is:
Code:ofstream a_file (name".txt");
I am tyring to create a programme where it writes certain information to a certain file. How do you make the file that it writes equal to a string that is typed in by the user. What i Put is:
Code:ofstream a_file (name".txt");
Here is the block of code that the errorsare in:
That is obviously not finished though.Code:else if (type == "create"){ cout<<endl; cout<<"Please enter the name of the member: "; getline(cin, name); cout<<endl; ofstream a_file ( name".txt" ); a_file<<name; cout<<"Please enter the member's first name: "; }
The istream and ofstream contructors require an argument that is a char*. When you read in the filename, what type of variable is it stored in? If it's a <string> you can use filename.c_str() to convert the string type to a char*.
In addition, you don't join strings together doing this:
You do this:Code:string str1 = "hello "; string str2 = str1"world";
string str2 = str1 + "world";
Last edited by 7stud; 11-13-2005 at 07:46 AM.
Thanks, would it be...filename.c_str( name )
"filename" is the name of the string variable. If your variable is named "name", you wouldn't do this:
name.c_str(name)
How exactly do I fit it together then? Can you show me the code edited the way it should be?
Would that be possible? I am very stupid an dwould not understand otherwise.
Try this:
When you cout a string variable and then its character array equivalent(a C-style string), they will look exactly the same.Code:string filename = "something"; cout<<filename.c_str()<<endl;
Last edited by 7stud; 11-13-2005 at 03:02 PM.
I don't want my string to be defined, i want it to be user defined and then that name is applied to the name of the output file that is created. here is the whole code it might be easier (it is not finished.)
Code:#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; int main() { string name = ""; string realfirst = ""; string lastname = ""; string datejoined = ""; string email = ""; string search = ""; string type = ""; string finish = ""; do { cout<<"Welcome To The Tyranno Database"<<endl; cout<<endl; cout<<"What would you like to do? (press i for instructions): "; getline(cin, type); cout<<endl; system ("CLS"); if (type == "i"){ cout<<endl; cout<<"This is Tyranno Database Software v0.1"<<endl; cout<<endl; cout<<"This software can save information about Tyranno members"<<endl; cout<<endl; cout<<"To do this type create into the opening screen"<<endl; cout<<endl; cout<<"To search for a member type search into the opening screen"<<endl; cout<<endl; cout<<"Press Enter"; cin.get(); system ("CLS"); finish = "no"; } else if (type == "create"){ cout<<endl; cout<<"Please enter the name of the member: "; getline(cin, name); cout<<endl; ofstream a_file (name".txt"); a_file<<name; cout<<"Please enter the member's first name: "; } } while (finish == "no"); }
Code:cout << "Please enter the name of the member: "; getline(cin, name); name += ".txt"; ofstream a_file ( name.c_str() );
Well I dont get the errors anymore but I need to write the rest of the code to see if it works.
Thanks
I am having a new problem with showing the file created.
Here is the code:
Code:else if (type == "search") { cout<<"Please enter the username of the member: "; getline(cin, search); cout<<endl; file = search + ".txt"; ifstream b_file ( file.c_str() ); if (!b_file.is_open()) { cout<<"That file could not be opened"<<endl; cout<<endl; cout<<"Would you like to finish? (yes / no): "; getline(cin, finish); system ("CLS"); } else { cout<<"Username: "<<search<<endl; cout<<endl; b_file>>realfirst; b_file>>lastname; cout<<"Full name: "<<realfirst + " " + lastname<<endl; cout<<endl; b_file>>datejoined; cout<<"Joined Tyranno on: "<<datejoined<<endl; cout<<endl; b_file>>email; cout<<"E-mail address: "<<email<<endl; cout<<endl; cout<<"Press Enter"; cin.get(); b_file.close(); cout<<endl; system ("CLS"); cout<<"Would you like to finish? (yes / no): "; getline(cin, finish); system ("CLS"); } }
Oh, the problem is that it does not show the information about the person where it should, it just tellas you all the stuff in onelong line. Like: bumfluff.txtMichaelBamforth08/08/[email protected]
and that is not what I want
A little help wouldnt go amiss.
how about posting the contents of the file (exactly), and posting exactly what the output of the program is.