File Display

Jul 30, 2016 at 4:04am
Im trying to display a file onto my program the file will consist of a whole paragraph i know i have to use the fstream library but im not too sure what to do to display it and if i should use a string.. can someone help?
If a player throws a 7 or 11 (sum of two dice) on the first roll, 
the player wins the game.
 If a player throws a 2, 3 or 12 (sum of two dice) on the first roll, 
the player loses the game.
* If a player throws a 4, 5, 6, 8 or 10 (sum of two dice) on the first roll, 
s(he) neither wins nor loses but creates a "point." 
If this is the case, the player keeps rolling the dice until the point (4, 5, 6, 8 or 10) is thrown again, and the player wins the game. 
However, if the player throws a 7 (sum of two dice) before the "point" is thrown, the player loses the game
.
Trying to display this!!
Last edited on Jul 30, 2016 at 4:05am
Jul 30, 2016 at 4:37am
Simply read each line into a string (using getline), and then cout the string.
Jul 30, 2016 at 5:36pm
But then i would have to cout every line wouldnt i?
Jul 30, 2016 at 6:32pm
You can read the file in one go and display it.
1
2
3
4
5
6
7
8
9
10
11
12
string readFile (string fname)
{
  ifstream src (fname);
  if (!src)
  {
    perror ("File error: ");
    return "";
  }
  ostringstream oss;
  oss << src.rdbuf ();
  return oss.str ();
}
Topic archived. No new replies allowed.