如何按任意键继续提示



我正在编写一个程序,其中代码从.txt文件中读取文本,在该文件中,任何超过24行的内容都必须用回车键继续,但不确定如何输入要求输入回车键的提示,这不会打乱格式,因为它必须立即显示前24行。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
....    
{
cout << "Please enter the name of the file: ";
string fileName;
getline(cin, fileName);
ifstream file(fileName.c_str(), ios::in);
string input;
ifstream fin(fileName);
int count = 0;
while (getline(fin, input))
{
cout << count << ". " << input << 'n' ;
count++;
if (count % 25 == 0)
cin.get();
}
cin.get();
system("read");
return 0;
}

代码中执行该功能的部分,如果我在这里插入提示

if (count % 25 == 0)
cout << "Press ENTER to continue...";
cin.get(); 

它只是在每行必须按enter键的地方。把提示放在任何地方都会在其他方面搞砸。

只需为相应的if放置大括号{}(如注释中所述(,您的程序就会工作。

还要注意,不需要像在程序中那样使用ifstream两次。

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

int main()
{
std::string fileName;
std::cout<<"Enter filename"<<std::endl;
std::getline(std::cin, fileName);

std::ifstream file(fileName);
std::string line;
int count = 1;
if(file)
{
while (std::getline(file, line))
{
std::cout << count << ". " << line << 'n' ;

if (count % 25 == 0)
{
std::cout<<"Press enter to continue"<<std::endl;
std::cin.get();
}
count++;
}
}
else 
{
std::cout<<"file cannot be opened"<<std::endl;
}

return 0;
}

最新更新