How to give user specified names to a file???

Hey guys ,so yesterday I was to my tuition class and there the Sir taught us something of data handling.So I thought why not create a phonebook application(simple one mind it!!!) and store data in binary mode for starters(as its efficient,Sir told!!).

So heres my program...the main part...

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
  #include<conio.h>
#include"PhoneBook.h"

using namespace std;

int main()
{

cout<<"Hello this is a Phonebook system..made by Avra Neel!!!"<<endl;
cout<<"Do you want to create a new Phonebook??"<<endl;
char c;
cin>>c;
if(c=='y'||c=='Y')
{
    phonebook *p=new phonebook;
    cout<<"What do you want to do??"<<endl;
    cout<<"1:Enter records"<<endl
        <<"2:Show records"<<endl
        <<"3:Delete this phonebook"<<endl;
    int i;
    cin>>i;
    switch(i)
    {
        case 1: p->create();
                p->accept();
                break;
        case 2: p->show();
                break;
        case 3: delete p;
                break;
        default : cout<<"Wrong Choice"<<endl;
    }
    delete p;
}
getch();
return 0;
}


And heres the PhoneBook.h file...
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
class phonebook
{
private:
    string file_name;
    string name,number;
    char ch='y';
    int t=0;
    fstream fio;
public:
    phonebook()
    {
     file_name="Names.txt";
    name="";number="";
    }
    ~phonebook()
    {
        fio.close();
    }
    void create()
    {
        cout<<"Enter the file name"<<endl;
        cin>>file_name;
        fio.open(file_name,ios::in|ios::out|ios::binary|ios::app);
    }
    void accept()
    {
        while(ch=='y'||ch=='Y')
        {
            cout<<"Enter the name and number pls"<<endl;
            cin>>name>>number;
            fio>>name>>number;
            t++;
            cout<<"Do you wanna add more"<<endl;
            cin>>ch;
        }
        fio.close();
    }
    void show()
    {
        fio.open("file_name",ios::out|ios::app|ios::binary);
        fio.seekg(0);
        cout<<endl;
        for(int i=0;i<t;i++)
        {
            fio>>name;
            fio>>number;
            cout<<"Name: "<<name<<endl
                <<"Number: "<<number<<endl;
        }
    }
};

What I want to do is give the user a chance to name the file in which he stores the records.Idk how to name like that,and the create method does throw an error.Can anyone point me to the link/page which describes in detail??I have seen the tutorial,but its maybe a specific question.
Thnx guys for viewing this !!!
Your friendly newbie,
LadyInRed7.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//
#include <iostream>
#include <fstream>

using namespace std;

int main () {	
	string s;
	getline(cin, s);
	ofstream fout;
	fout.open(s);
	
return 0;
}
You can remove line 40. The file should already be open when show() is called.

At line 31 you are reading from fio when I think you want to write. So it should be fio << name << number;. Actually you probably want some formatting in there so that show will know where the name stops and the number begins, so maybe fio << name << ' ' << number << '\n';
Thnx for your replies dhayden and anup30,I will change the specified things and make my program better.Actually this is my first file handling program you know ,so maybe I messed up.
But what I asked was how to give the user a chance to specify the name of the file to which he saves the records...I really dont know how to do that.

Anyways thnx for your replies,I will mend the things.
Your friendly newbie,
LadyInRed7.
Ok,so after a few ifs and elses,I made some minor tweaks to my program.

Here is the main part...
1
2
3
4
5
case 2: char x[50];
cout<<"Enter the name of the file..."<<endl;
cin>>x;
p->show(x);
break;

And heres the PhoneBook.h,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void show(char x[50])
{
for(int j=0;x[j]!='\0';j++)
file_name[j]=x[j];
fio.open(file_name,ios::binary|ios::out);
fio.seekg(0);
cout<<endl;
for(int i=0;i<t;i++)
{
fio<<name<<'\n'<<number<<'\n';
cout<<"Name: "<<name<<endl
<<"Number: "<<number<<endl;
}
}

If you do compile this program,and choose the 2nd option in the executable,
after the file name is entered,in the executable 2 lines are skipped and walla,nothing happens!!!
Actually what I want is to have the program also read from a file....which does not happen in this case.[:( ]
Pls help guys,I mean point were Im wrong.I would be indebted by your guidance.
Thnx for reading.
Topic archived. No new replies allowed.