【问题标题】:C++ close text file and open new console windowC++ 关闭文本文件并打开新的控制台窗口
【发布时间】:2017-06-22 19:27:44
【问题描述】:

Picture of the out put C++ 我正在开发一个控制台游戏,它在启动时会打开一个文本文件。玩家阅读规则后,假设玩家按任意键播放时关闭文本文件。然后玩家可以在新屏幕上输入多少玩家。我无法关闭文本文件,因此玩家可以在新屏幕上输入玩家数量。它只是在底部的文本文件上输出。我究竟做错了什么? 代码

#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(); // 应该关闭文本文件 不会影响控制台上显示的内容。
  • 然后玩家可以在新屏幕上输入多少玩家标准c++没有办法做到这一点但是winapi控制台功能应该让你这样做。 stackoverflow.com/questions/6486289/how-can-i-clear-console
  • 我做错了什么?我说你主要是误解了控制台是如何使用标准c++工作的。
  • 谢谢你们。系统(“cls”);效果很好。

标签: c++ visual-c++ console-application


【解决方案1】:

据我了解,文件输出到控制台并关闭。但是,您的 getchar() 实际上并没有为您做任何事情。您需要创建一个条件,根据输入的任何键清除控制台,然后继续询问用户您的游戏的玩家数量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2018-05-16
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多