很简单的问题,看你够不够细心
你能看出来这段程序的问题在哪吗?
提示:该程序用于清除多余的空格.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main()
{
ifstream fin;
ofstream out;
char ch= ' ';
fin.open ( "in.dat ");
if(fin.fail ())
{cout < < "opening in failed! " < <endl;exit(1);}
out.open ( "out.dat ",ios::app);
if(out.fail ())
{cout < < "opeing out failed! " < <endl;exit(1);}
while(fin.eof ())
{
while(ch== ' '||ch== '\t ')
fin.get(ch);
while(isalpha(ch))
{out < <ch;fin.get (ch);}
out < < " ";
}
fin.close ();
out.close ();
return 0;
}
[解决办法]
去掉:fin.get (ch);
最后成下如下代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main()
{
ifstream fin;
ofstream out;
char ch= ' ';
fin.open ( "in.dat ");
if(fin.fail ())
{cout < < "opening in failed! " < <endl;exit(1);}
out.open ( "out.dat ",ios::app);
if(out.fail ())
{cout < < "opeing out failed! " < <endl;exit(1);}
while(fin.eof ())
{
while(ch== ' '||ch== '\t ')
fin.get(ch);
while(isalpha(ch))
{out < <ch;}
out < < " ";
}
fin.close ();
out.close ();
return 0;
}