Choice between ios::app & ::trunc error

Jul 2, 2008 at 6:56pm
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
52
53
54
55
#include<iostream>
#include<fstream>
#include<limits>
using namespace std;

int main()
{
 string c, s, t, u, b;
 bool a = true;
 char d, e;
 char g;
 system("TITLE Address Book Maker");
 cout << "Welcome to the Address Book Maker!\nRules and Help Regarding Filenames\n 1. When naming a file you MUST put \".txt\" at the end.\n 2. To load a previous address book, type the EXACT name and put \".txt\" at the\n end(unless already there).\n"; 
 cout << "Ok! Now, hit ENTER to start.";
 cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
 system("cls");
 cout << "What will you call your Address Book file/What Address Book will you load?\n";
 getline(cin, c);
 cout << "Erase all contents? [y/n] (n) ";
 cin >> e;  
 if(e == 'y' || e == 'Y')
   ofstream streamone(c.c_str(), ios::trunc);
 else
   ofstream streamone(c.c_str(), ios::app);
 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;
 if( streamone.fail() ){
   cout << "Failed to open " << c << endl;
   cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
   cout << "Press ENTER to exit...\n";
   cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
   return 0;}
 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) "; 
 cin >> d;  
   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);
}


I am given an error saying streamone is undeclared, when it in fact is in lines 22 & 24. Any ideas?
---
DOWN WITH DVORAK
Jul 2, 2008 at 7:24pm
streamone is only declared within the scope of that if statement.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (true) {
 int i = 3; 
 // j will never exist here
} else {
 // i never exists here
 int j = 4;
}

// i or j no longer exist. because they have gone out of scope
if (true)
 int b = 3;

// b no longer exists, because the if statement is complete and it's out of scope


Jul 2, 2008 at 10:00pm
Any suggestions on how to do this, then?
---
DOWN WITH DVORAK
Jul 2, 2008 at 10:03pm
1
2
3
4
5
6
7
8
 ios_base::openmode mode = ios_base::out;

 if(e == 'y' || e == 'Y')
   mode = ios::trunc;
 else
   mode = ios::app;

ofstream streamone(c.c_str(), mode);
Topic archived. No new replies allowed.