0

Picture of the out put C++ I am working on a console game that when it starts up it opens a text file. After the players reads the rules, it is suppose to close the text file when the player presses any key to play. Then the player can input how many players on the new screen. I can't get the text file to close, so the players can enter the number of players on the new screen. It just outputs on the text file at the bottom. What am I doing wrong? Code

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
   ifstream inputFile;
   ifstream f("Text.txt");//associates the text file
   cout << "Liar's Dice Copy" << endl;
   if (f.is_open())//if true then allow open stream object.
       cout << f.rdbuf();//Get/set stream buffer

   cout << "Press any key to play" << endl;
   inputFile.close(); // should close text file
   getchar(); // gets key input
int numplayers;// declares numplayers
   cout << "Enter Number of Players:" << endl; //print on screen
   cin >> numplayers;// outputs number of players
}
  • ***inputFile.close(); // should close text file*** Will have no effect on what is displayed on the console. – drescherjm Jun 22 '17 at 19:45
  • ***Then the player can input how many players on the new screen*** Standard `c++` does not have a way to do this however winapi console functions should let you do that. https://stackoverflow.com/questions/6486289/how-can-i-clear-console – drescherjm Jun 22 '17 at 19:49
  • ***What am I doing wrong?*** I say you are mainly misunderstanding how the console works using only standard `c++`. – drescherjm Jun 22 '17 at 19:51
  • Thank you guys. system("cls"); worked great. – user7999687 Jun 22 '17 at 20:19

1 Answers1

0

From what I have understood, the file is output to the console and does get closed. However, your getchar() doesn't actually do anything for you. You'd need to create a conditional that clears the console based on the input of any key and then proceeds with asking the user the number of players for your game.

Ragxion
  • 113
  • 10