How to append output to prev. output?

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
#include<iostream>
#include<fstream>
#include<limits>
using namespace std;

int main()
{
 string s, t, u, b;
 bool a = true;
 cout << "Welcome to the Address Book! "; 
 ofstream streamone("Address Book.txt");
 do
 {
 cout << "Enter a person's first and last name. ";
 cin >> s >> t;
 cout << "Input their full address. ";
 getline (cin, u);
 getline (cin, u);
 cout << "Enter their phone number: ";
 cin >> b;
 streamone << s << " " << t << endl;
 streamone << u << endl;
 streamone << b << endl << endl;
 streamone.close();
}
 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' ); 
}

This is how the code stands, question answered.
Last edited on
You accidentally wrote [\code] instead of [/code].

To append to a file, open it with the ios_base::app flag. Line 27 should read

ofstream streamone( "Address Book.txt", ios::app );

BTW, I recommend that you don't use >> to input strings. Use getline() instead.

Hope this helps.
Tried getline before, but it bugged. :(
I'll try ios::app, though.
[edit]I had to put getline twice in a row. LOL[/edit]
Last edited on
1. Question: why do you keep opening and closing the file? Open it once, before the loop, and close it after the loop.

2. getline() fails if you mix it inappropriately with >>. You must be careful:
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 <limits>
#include <string>
using namespace std;

int throw_off_bridge()
  {
  cout << "Auuuuuuuugh!\n";
  return 1;
  }

int main()
  {
  string name, color;
  int age;

  cout << "Halt!\nWhat is your name? ";
  getline( cin, name );

  cout << "What is your age? ";  // yes, but 'quest' takes a string...
  cin >> age;
  if (cin.get() != '\n')        // get rid of that pesky newline
    return throw_off_bridge();  // (unless the user didn't input a proper number)

  cout << "What is your favorite color? ";
  getline( cin, color );
  if (color != "yellow")
    return throw_off_bridge();

  cout << "Right. Off you go.\n";
  return 0;
  }


Hope this helps.

PS. Please edit that [\code] tag in your first post.
Another Q.
Is there a way to make a search option in there? (This is partly for my parents' benefit, and dear ol' Dad wants a search option.) I read about find_first_of, but I'm not sure where to put it/ how to do it. Any ideas(again)?
Nice. Just one thing: the extra getline on line 17 just reads the newline left over from line 15. Check out what I did on lines 22 and 23.

BTW, your variable names stink. Please choose something that describes things better.

The way you output the file, it is formatted thus:
Harry Potter
4 Privot Lane
5551234
Ronald Weasly
The Burrow
5552345
Hermione Granger

5553456

This makes it very easy to search, since all you need to do is read it three lines at a time and compare.

For example, to search for a telephone number:
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
struct person_t
  {
  string given_name, surname;
  string address;
  unsigned phone_number;  // notice that I changed this to a number

  person_t(): given_name(""), surname(""), address(""), phone_number(0) { }
  };

istream& operator >> ( istream& ins, person_t& person )
  {
  ins >> person.given_name
      >> person.surname;
  ins.get();

  getline( ins, address );

  string phone_number;
  getline( ins, phone_number );
  person.phone_number = 0;
  stringstream( phone_number ) >> person.phone_number;

  return ins;
  };

person_t find_phone_number( fstream& file, unsigned phone_number )
  {
  // make sure the file is reset
  file.clear();
  file.seekg( 0 );
  person_t person;

  // find the desired person
  while (file >> person)
    if (person.phone_number == phone_number)
      return person;

  // if not found, return nobody
  return person_t();
  };


You can do the same sort of thing for other members of the struct.

Hope this helps.
Last edited on
Most codes I write don't really need fancy names, but then...

Should I write that to a separate program that searches, or let the user decide between new/load and search, or...what?

Here is my current code:
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
#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!\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\nend(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";
 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) "; 
 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);
}
 return 0;
}
note: I like the way I did the getline, so nyeh. This isn't commercial(yet).
Last edited on
If by "fancy names" you mean "descriptive, meaningful names" then it is your own fire for the roast. X-|

As for the design, it is up to you. It depends on who your target consumer is.

Good luck!
lol, I just started this crud.

This is as advanced as I've gone, and I'm afraid I'll screw up so badly, that I can't think of a way out of that. D:

Where is a good place to put this on the Internet?
Also, I want to concatenate the search engine to the Address Book Maker using an if-else statement. Where should it go?
Last edited on
Heh heh, messing up is part of the learning process. Just keep regular backups and you can always undo a disaster. ;-)

Your ISP might provide some webspace, either for free or for a premium. You can buy space. You can use free file hosting services like MediaFire.

The main() function of a program is to direct the operation of the rest of the program. Anything that actually does stuff belongs in a subroutine.

For illustrative purposes:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef vector <string> name_list;

void show_menu()
  {
  cout << "\nWhat would you like to do?\n\n"

       << "add names\n"
       << "delete a name\n"
       << "list names\n"
       << "quit\n\n"

       << "> ";
  }

string read_name()
  {
  string name;
  if (getline( cin, name ) and !name.empty())
    {
    string s = name;

    // make sure the name is in "given-name(s) surname(s)" order.
    int i = name.find( ',' );
    if (i != string::npos())
      {
      s = name.substr( i +1 ) +' ' +name.substr( 0, i );
      }
    }

  // get rid of extra spaces
  stringstream ss( s );
  string piece;
  name.clear();
  while (ss >> piece)
    {
    if (!name.empty()) name += ' ';
    name += piece;
    }

  return name;
  }

void add_names( name_list& result )
  {
  string name;

  cout << "\nPlease enter your friends names, one per line.\n"
       << "Press ENTER twice to finish.\n";

  while (true)
    {
    // get a name
    name = read_name();
    if (name.empty()) break;

    // add it to the end of our list
    result.push_back( name );
    }

  // Keep it sorted
  sort( result.begin(), result.end() );
  }

void delete_name( name_list& names )
  {
  cout << "\nEnter the name of the friend you wish delete\n"
       << "or just press ENTER to abort.\n\n"

       << "> ";

  string name = read_name();

  name_list::iterator i = find( names.begin(), names.end(), name );
  if (i != names.end())
    {
    names.erase( i );
    }
  }

void list_names( name_list& names )
  {
  cout << "\nYour friends are:\n";

  for (unsigned n = 0; n < names.size(); n++)
    cout << setw( 2 ) << right << n << ": " << names[ n ] << endl;
  }

int main()
  {
  string    user_input;
  name_list names;
  bool      done = false;

  cout << "Welcome to the friend counter.\n"

  show_menu();
  while (!done and getline( cin, user_input ))
    {
    switch (toupper( user_input[ 0 ] ))
      {
      case 'A': add_names( names ); break;
      case 'D': delete_name( names ); break;
      case 'L': list_names( names ); break;
      case 'Q': done = true; break;
      default:
        cout << "What? Try again> ";
      }
    }

  cout << "Good bye.\n";

  return 0;
  }


Well, that's all I want to type. Hope this helps.
Topic archived. No new replies allowed.