我目前正在完成一项学校作业,其中包括首先接收用户输入其名称的x个文件的内容,然后读取其中有多少个字母,然后将其输出给用户。 这就是我到目前为止所做的:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
const int fileNameSize = 20;
int fileCount = 0;
ifstream inFile;
ofstream outFile;
int numRows, numPRow, num, rowSum;
char inputFileName [fileNameSize+1];
cout << "How many files are being processed?\n";
cin >> fileCount;
for (int i = 0; i <= fileCount; i++)
{
cin >> inputFileName;
inFile.open(inputFileName);
if (!inFile)
{
cout << "Could not open input file.\n";
return 0;
}
else
{
int acount, tcount, ecount, ocount, icount, ncount, scount, hcount, rcount;
char c;
while (!inFile.eof())
{
//inFile >> num;
inFile.get(tolower(c));
switch (c)
{
case 'a': acount++;
case 't': tcount++;
case 'e': ecount++;
case 'o': ocount++;
case 'i': icount++;
case 'n': ncount++;
case 's': scount++;
case 'h': hcount++;
case 'r': rcount++;
}
}
int chartotal = acount + tcount + ecount + ocount + icount + ncount + scount + hcount + rcount;
cout << "File name: " << inputFileName << endl;
cout << "Number of E's: " << ecount << endl;
cout << "Number of T's: " << tcount << endl;
cout << "Number of A's: " << acount << endl;
cout << "Number of O's: " << ocount << endl;
cout << "Number of I's: " << icount << endl;
cout << "Number of N's: " << ncount << endl;
cout << "Number of S's: " << scount << endl;
cout << "Number of H's: " << hcount << endl;
cout << "Number of R's: " << rcount << endl;
cout << "Total Number of Characters: " << chartotal << endl;
}
}
//cout << "exit colm flag";
//cout << "exit row flag\n";
}
编译后,我收到第33行的错误:
33:26: note: candidates are:
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :283:7: note: std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_ CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>, st d::basic_istream<_CharT, _Traits>::int_type = int]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :283:7: note: candidate expects 0 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :297:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _T raits>::get(std::basic_istream<_CharT, _Traits>::char_type&) [with _CharT = char , _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::char_ty pe = char]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :297:7: note: no known conversion for argument 1 from 'int' to 'std::basic_ist ream<char>::char_type& {aka char&}'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :324:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _T raits>::get(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, st d::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char, _Traits = std ::char_traits<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std: :streamsize = int]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :324:7: note: candidate expects 3 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :335:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_is tream<_CharT, _Traits>::get(std::basic_istream<_CharT, _Traits>::char_type*, std ::streamsize) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_ istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :335:7: note: candidate expects 2 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :358:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _T raits>::get(std::basic_istream<_CharT, _Traits>::__streambuf_type&, std::basic_i stream<_CharT, _Traits>::char_type) [with _CharT = char, _Traits = std::char_tra its<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_st reambuf<char>, std::basic_istream<_CharT, _Traits>::char_type = char]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :358:7: note: candidate expects 2 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :368:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_is tream<_CharT, _Traits>::get(std::basic_istream<_CharT, _Traits>::__streambuf_typ e&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_C harT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_C harT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream :368:7: note: no known conversion for argument 1 from 'int' to 'std::basic_ist ream<char>::__streambuf_type& {aka std::basic_streambuf<char>&}'
我很清楚这些ifstream,ofstream等函数是如何工作的,只是简单地从示例文件中转录它们并将它们操作到所需的程序。是否有一个特定的函数用于从.txt文件中读取每个字符,我可以使用它吗?
答案 0 :(得分:2)
你所指的问题是:
inFile.get(tolower(c));
如果编译器告诉您特定函数调用没有适用的重载,那么您应该采取的第一个操作是阅读documentation并检查调用该函数所需的内容。
问题是,std::tolower()
的结果类型为int
为左值,而std::istream::get()
则为char&
类型。
您可以修复该代码,例如
inFile.get(c);
c = tolower(c);
请参阅固定代码here。
请注意,您的代码存在更多问题,如评论
中所述