一个C++程序,没有错误。运行结束后怎么什么也没有显示?

程序要求读取一个文本,大写字母变小写,小写变大写。然后存入另一个文本。我用C++遍好后,没有错误,但是不会运用。求高手帮忙

#include <fstream>
#include <iostream>

using namespace std;

char ChangULCase(char ch)
{
if (ch>='A' && ch<='Z')
{
ch += 32;
}
else if (ch>='a' && ch<='z')
{
ch -= 32;
}
}

int main(void)
{
ifstream infile;
ofstream outfile;
char ch;

infile.open("./in.txt", ios::in);
outfile.open("./out.txt", ios::out);

while (!infile.eof())
{
ch = infile.get();

ch = ChangeULCase(ch);

outfile.put(ch);
}

infile.close();
outfile.close();

return 0;
}
为什么ChangeULCase是错的,要怎么改阿。还有,读取的那个文本有什么要求?如地址。

第1个回答  2010-07-09
因为你声明的函数是ChangULCase,注意少了一个e,而且并没有返回值,所以有错
还有程序运行有结果,结果在out.txt里面,其中in.txt和out.txt都和程序在同一个文件夹里
第2个回答  2010-07-09
#include <fstream>
#include <iostream>

using namespace std;

char ChangeULCase(char ch)
{
if (ch>='A' && ch<='Z')
{
ch += 32;
}
else if (ch>='a' && ch<='z')
{
ch -= 32;
}
return ch;
}

int main(void)
{
ifstream infile;
ofstream outfile;
char ch;

infile.open("./in.txt", ios::in);
outfile.open("./out.txt", ios::out);

while (!infile.eof())
{
ch = infile.get();

ch = ChangeULCase(ch);

outfile.put(ch);
}

infile.close();
outfile.close();

return 0;
}

函数总得有返回值吧???本回答被提问者采纳
第3个回答  2010-07-09
ChangeULCase函数最后要加return ch;
ch = ChangeULCase(ch);的赋值才有意义。
如果文件跟程序不是一个目录下,就应该用绝对地址
第4个回答  2010-07-09
程序改成如下:

#include <fstream>
#include <iostream>

using namespace std;

char ChangULCase(char ch)
{
char schar;
if (ch>='A' && ch<='Z')
{
ch += 32;
}
else if (ch>='a' && ch<='z')
{
ch -= 32;
}

schar = ch;
return schar; //增加返回值
}

int main(void)
{
ifstream infile;
ofstream outfile;
char ch;

infile.open("in.txt", ios::in); //放置目录
outfile.open("out.txt", ios::out); //放置目录

while (!infile.eof())
{
ch = infile.get();

ch = ChangULCase(ch);

outfile.put(ch);
}

infile.close();
outfile.close();

return 0;
}

注意:程序运行目录下必须要有in.txt文件,程序是不会自动创建文件的。程序找不到该文件,所以程序会一直循环。out.txt程序会自动建立。详情请查看MSDN中ifstream,ofstream的使用说明。
第5个回答  2010-07-09
可能是编译器的问题 去别的电脑上试试
相似回答