String 输入/输出文件和查找字符串的程序
我们被告知可以将名称的所有实例转换为大写或小写(我选择了大写),因此程序将输出名称的实例,而不管它是什么大小写 我在下面指出了我的一个问题,我可能在其他地方有更多我还看不到的问题。不知道你们这些好心人能不能帮我解决这个问题 下面是我到目前为止所看到的,我也注意到了返回的错误 非常感谢您的时间和帮助String 输入/输出文件和查找字符串的程序,string,String,我们被告知可以将名称的所有实例转换为大写或小写(我选择了大写),因此程序将输出名称的实例,而不管它是什么大小写 我在下面指出了我的一个问题,我可能在其他地方有更多我还看不到的问题。不知道你们这些好心人能不能帮我解决这个问题 下面是我到目前为止所看到的,我也注意到了返回的错误 非常感谢您的时间和帮助 #include <fstream> #include <iostream> #include <string> #include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
bool die(const string & msg);
bool input(string & s, const string & prompt);
bool open(ifstream & fin, const string & fileName);
bool open(ofstream & fout, const string & fileName);
//bool name(const string & line);
bool convert(string & str, string & converted);
int main() {
string inName, outName;
ifstream fin;
ofstream fout;
if (!input(inName, "Name of input file: "))
die("I can't read the name of the input file");
if (!open(fin, inName))
die("I can't open " + inName + " for output");
if (!input(outName, "Name of output file: "))
die("I can't read the name of the input file");
if (!open(fout, outName))
die("I can't open " + outName + " for output");
for (string converted; getline(fin, converted);) {
if (convert(converted)) //<---***HAVING AN ISSUE HERE***
fout << converted << endl;
}
if (fin.rdstate() != (ios::failbit | ios::eofbit))
die("Input file " + inName + " terminated input incorrectly");
fout.close();
if (!fout)
die("Output file " + outName + "had a problem with writing or closing");
fin.close();
cout << "read from " << inName << ", wrote to " << outName << ", ok" << endl;
}// main
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
bool input(string & s, const string & prompt) {
cout << prompt;
return getline(cin, s) ? true : false;
}
bool open(ifstream & fin, const string & fileName){
fin.open(fileName);
return fin ? true : false;
}
bool open(ofstream & fout, const string & fileName){
ifstream tin(fileName);
if (tin) return false;
fout.open(fileName);
return fout ? true : false;
}
//bool name(const string & line){
//return line.find("john") != UINT_MAX;
//}
bool convert(string & str, string & converted)
{
for (short i = 0; i < str.size(); ++i)
converted += toupper(str[i]);
return converted.find("john") != UINT_MAX;
}
#包括
#包括
#包括
#包括
使用名称空间std;
bool die(常量字符串和消息);
布尔输入(字符串和s、常量字符串和提示);
bool open(ifstream和fin、常量字符串和文件名);
bool open(流和fout、常量字符串和文件名);
//布尔名称(常量字符串和行);
布尔转换(字符串和str,字符串和转换);
int main(){
字符串inName,outName;
流鳍;
流式流量计;
如果(!input(inName,“输入文件的名称:”))
die(“我无法读取输入文件的名称”);
如果(!打开(fin,inName))
死(“我无法打开”+inName+“进行输出”);
如果(!input(outName,“输出文件名:”))
die(“我无法读取输入文件的名称”);
如果(!打开(fout,outName))
骰子(“我无法打开”+outName+“进行输出”);
for(字符串已转换;getline(fin,已转换);){
如果(convert(converted))//很好,那么错误消息几乎说明了一切。convert
函数声明为接受两个参数,但您只传递了一个参数
bool convert(string & str, string & converted)
您需要传递另一个获取转换字符串的字符串引用
题外话:另外,为了安全起见(也许你以后想切换到其他编程语言):请不要开始学习把一切而不是0
都看作真的坏习惯
像这样的事情迟早会引起麻烦的:
return fin ? true : false;
convert(string&str,string&converted)函数接受两个参数,但只传递1。这是如何在第40行convert(converted)中使用该函数的。尝试理解错误消息,这将使编程更容易。