[SOLVED] How to let user name text files

Jun 15, 2008 at 9:14pm
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
#include<iostream>
#include<fstream>
#include<limits>
using namespace std;

int main()
{
 string c, s, t, u, b;
 bool a = true;
 cout << "Welcome to the Address Book! "; 
 cout << "What will you call your text file? ";
 cin >> c;
 ofstream streamone('c', ios::app);
 do
 {
 cout << "Enter a person's first and last name. ";
 cin >> s >> t;
 cout << "Input their full address (ex. 123 Road Rd. Boston, MA). ";
 getline (cin, u);
 getline (cin, u);
 cout << "Enter their phone number: ";
 cin >> b;
 streamone << s << " " << t << endl;
 streamone << u << endl;
 streamone << b << endl << endl;
}
 while(a);
 streamone.close();
 cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); 
 cout << "Press Enter to exit this program...\n"; 
 cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); 
} 

I'm wondering how to let the user input the text file's name (lines 11-13), but I got an error involving changing c "FROM CHAR TO CONST CHAR". Any ideas?
Last edited on Jun 16, 2008 at 12:31pm
Jun 15, 2008 at 10:08pm
http://www.cplusplus.com/forum/beginner/2215/

check this link out, I asked the same question but the syntax is for a directory, instead your syntax would be....

1
2
3
cout << "What will you call your text file? ";
 cin >> c;
 ofstream streamone(c.c_str(), ios::app);


That will work. :)
Jun 15, 2008 at 10:52pm
New prob.
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
#include<iostream>
#include<fstream>
#include<limits>
using namespace std;

int main()
{
 string c, s, t, u, b;
 bool a = true;
 char d;
 system("TITLE Address Book Maker");
 for(int i = 0; i < 1; i++)
 {
 cout << "Welcome to the Address Book Maker! "; 
 cout << "What will you call your Address Book file? \n(note: you MUST type \".txt\" at the end and no spaces (you may edit this directly later)!) ";
 cin >> c;
  cout << "Excellent! ";
 do
 {
 cout << "Now, enter a person's first and last name. ";
 cin >> s >> t;
 cout << "Input their full address (ex. 123 Road Rd. Boston, MA (insert zip code here)). ";
 getline (cin, u);
 getline (cin, u);
 cout << "Enter their phone number: ";
 cin >> b;
 ofstream streamone(c.c_str(), ios::app);
 streamone << s << " " << t << "'s address is" << endl;
 streamone << u << " and his phone number is" << endl;
 streamone << b  << "." << endl << endl;
 cout << "The program will show this:\n" << s << " " << t << "'s address is" << endl << u << " and his phone number is" << endl << b  << "." << endl << endl;
 do{
 cout << "Restart? (y or n) "; 
   if(d == 'y' || d == 'Y')
     continue;
   else if(d == 'n' || d == 'N')
     a = false;     
 }while (d != 'y' && d != 'Y' && d != 'n' && d != 'N');
 streamone.close();
}while(a);
}
 return 0;
}

Right after 26, an infinite loop starts, for no visibly apparent reason. Any ideas?
Last edited on Jun 15, 2008 at 11:13pm
Jun 16, 2008 at 2:49am
It is pretty apparent to me.

in the do loop starting at 32, you aren't taking user input, so the variable 'd' is always going to stay the same causing the loop to just repeat.
Last edited on Jun 16, 2008 at 2:52am
Jun 16, 2008 at 12:29pm
WOW. I feel so dumb. Then again, I wrote that at 11 at night, so...
Topic archived. No new replies allowed.