assistance with File I/O assignment?

Can some one get me pointed in the right direction?
I know how to use variables, functions and loops, and have basic understanding of file I/O
how do i get started on this assignment?


"Write a program that gives and takes advice on program writing. The program starts by
writing a piece of advice to the screen and asking the user to type in a different piece
of advice. The program then ends. The next person to run the program receives the
advice given by the person who last ran the program. The advice is kept in a file, and
the contents of the file change after each run of the program. You can use your editor
to enter the initial piece of advice in the file so that the first person who runs the
program receives some advice. Allow the user to type in advice of any length so that it
can be any number of lines long. The user is told to end his or her advice by pressing
the Return key two times. Your program can then test to see that it has reached the
end of the input by checking to see when it reads two consecutive occurrences of the
character '\n'. Name the input file as “advice.txt”.
The tricky part here is that a piece of advice ends when there are TWO newlines, meaning, one of the inputs is the empty string. For example, here are three pieces of advice:

1
2
3
4
5
6
7
An apple a day keeps the doctor away.

Time flies like an arrow.
Fruit flies like a banana.

A bird in the hand is worth two in the bush.

<-- blank line


<-- blank line

<-- notice the final blank line

That blank line at the end of each piece of advice is important.

Use getline() until what you get is an empty line, that is, until s.empty() is true.


Next, you will want to be able to keep all those pieces of advice somewhere. Use a vector:

1
2
typedef std::vector <std::string> Advice;
typedef std::vector <Advice> List_of_Advice;


From there, write a function to read a single piece of advice into an Advice.
Then write a function to read a List_of_Advice, using the first function.

You should also write functions to write a single piece of advice (don’t forget the extra newline), and to write a list of advices.


From there, your program should open the “advice.txt” file, read all the pieces of advice. You should be able to reuse the functions, since a std::ifstream can be substituted for std::cin.

That is:
  • Read the entire file. Write the last piece of advice to std::cout.
  • Then use the first function to get a new piece of advice from std::cin.
    Add it to the end of your list of advice.
  • Finally, reopen the file (for rewrite, using std::ofstream f( "advice.txt" );) and write all the advices to the file.

Hope this helps.
Last edited on
THANK YOU!

ill try it!
can you explain to me what s.getline() represents ? and do i treeat the vector as a variable and just call it by using cin>>Advice in order to get the vector::string as a vector?
Use the reference section of this page to lookup getline(). Here's the page for string: http://www.cplusplus.com/reference/string/string/

You can't simply use cin >> Advice because there is no >> operator for vectors. You'll have to read each line into a string and then use Advice::push_back() to add it to the vector of advice strings.
“s” is a meta-variable name for “string”, meaning, it is a string variable you have somewhere.

1
2
  std::string s;
  ...

Also, be careful. At no point did I ever write “s.getline()”.
As dhayden wrote, you should have access to C++ library documentation. This will make your life so much easier. Those of us who are used to writing code keep our browsers bookmarked to at least one documentation site, and refer to it regularly.

“getline()” is a free-standing function to read a line of text from a stream:

1
2
3
4
5
6
  std::string users_name;
  std::cout << "What is your name? ";

  getline( std::cin, users_name );

  std::cout << "Hello, " << users_name << ".\n";

You indicated basic understanding of I/O. This is it.

Hope this helps.
Topic archived. No new replies allowed.