Ok, here's the problem

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 a piece of advice given by the person who last run 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 lenght 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 ocurrences of the character '\n'.

I really have no experience by using files, so my question is: how can I use the input in a file and make it be shown on the screen? The other is: how can I do the user to enter some text and save it in a file?

If you guys can help me, I would be so blessed...

Recommended Answers

All 4 Replies

Read:

#include <fstream>
#include <iostream>
#include <string>

int main()
{
  std::ifstream in ( "file.txt" );
  std::string line;

  if ( in ) {
    while ( std::getline ( in, line ) )
      std::cout<< line <<'\n';
  }
}

Write:

#include <fstream>

int main()
{
  std::ofstream out ( "file.txt" );

  if ( out )
    out<<"This is a test";
}

Consider yourself blessed, I didn't tell you to RTFM and STFW for a non-smart question.

Open the file
Read the file
Put what you have read from the file on an ouputdevice, printer, screen,...

Thanks

i want a program for finding number of lines in a given pragraph

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.