C++ 如何在c++;
从我创建的名为“numbers.txt”的文件调用数据时遇到问题。该文件的编号为1-26,应该放在一个数组中。现在它不是编译。我有类似的项目没有问题。所以我不知道我做错了什么。下面是我正在使用的代码C++ 如何在c++;,c++,function,file-io,C++,Function,File Io,从我创建的名为“numbers.txt”的文件调用数据时遇到问题。该文件的编号为1-26,应该放在一个数组中。现在它不是编译。我有类似的项目没有问题。所以我不知道我做错了什么。下面是我正在使用的代码 #include <iostream> #include <string> #include <fstream> using namespace std; // function prototypes: int readEncodingCipher(string
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// function prototypes:
int readEncodingCipher(string filename, int encodeKey[], int size);
int main()
{
string fileName;
const int size = 26;
int encodeKey[size];
//Requests the name of a file to read from.
cout << "Please enter a file name with a key: ";
cin >> fileName;
readEncodingCipher(fileName, encodeKey, size);
system("pause");
return 0;
}
int readEncodingCipher(string fileName, int encodeKey[], int size)
{
string fileName;
ifstream inFile;
int num;
int counter = 0;
inFile.open(fileName);
if (inFile)
{
while (inFile >> num && counter <= size)
{
encodeKey[counter] = num;
counter++;
}
}
else
{
cout << "unable to locate file";
}
inFile.close();
}
#包括
#包括
#包括
使用名称空间std;
//功能原型:
int readEncodingCipher(字符串文件名,int encodeKey[],int size);
int main()
{
字符串文件名;
常数int size=26;
int编码键[大小];
//请求要从中读取的文件的名称。
cout>文件名;
readEncodingCipher(文件名、编码密钥、大小);
系统(“暂停”);
返回0;
}
int readEncodingCipher(字符串文件名,int encodeKey[],int size)
{
字符串文件名;
河流充填;
int-num;
int计数器=0;
inFile.open(文件名);
如果(填充)
{
而(infle>>num&&counter我假设您收到了错误消息
error: declaration of 'std::__cxx11::string fileName' shadows a parameter
在你的功能
int readEncodingCipher(string fileName, int encodeKey[], int size)
{
string fileName;
局部变量fileName
将参数fileName
隐藏起来。您必须更改变量名称。修复此错误后,它将为我编译
此外,您应该修复函数的返回类型。将其更改为void
或返回值
您应该修复while循环的计数器。最后一个元素是encodeKey[size-1]
,因此while循环应在此元素处停止。如果未编译,则应在问题中包含完整的错误消息。除了错误日志之外,还应上载数字文件。countercounter
当函数不返回任何内容时,为什么返回int
类型?避免重复变量不可靠的名字。它通常会导致疼痛。