【发布时间】:2014-06-06 04:30:45
【问题描述】:
您好,我有一些令我困惑的错误。其中一个很大,另一个只是困扰我。
所以在你建议我制作其他功能之前,请先做几件事:我制作的原型是我的老师指定的。除了他指示我使用的功能外,我不允许使用任何功能。所以我不能编造任何函数来解决我的问题。
在我的 COPY 函数中,我使用读取输入文件的行缓冲区从输入文本中获取每一行并将其打印到我的输出文件中。我的问题是,如果我不在输入中添加空换行符,则函数会少读 1 行。
例如,如果我的输入文件中有 5 行文本,并且在我的最后一行之后不按 Enter,它只会打印 4 行文本。
其次,我遇到了这些错误:
Error 2 error LNK2019: unresolved external symbol "int __cdecl calcFactorial(void)" (?calcFactorial@@YAHXZ) referenced in function _main C:\Users\UserName\Documents\CS161\Atilla\main\main\main.obj main
Error 3 error LNK1120: 1 unresolved externals C:\Users\UserName\Documents\CS161\Atilla\main\Debug\main.exe 1 1 main
还有这个警告:
Warning 1 warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\users\UserName\documents\cs161\atilla\main\main\main.cpp 127 1 main
然后时不时地,我所有的“cout”都会出错,说“cout is ambiguous”
对修复我的错误有什么建议吗?对不起,如果有很多。我在网上搜索了解决方案,但我不太明白如何解决它。有人可以为我这样的新手解释一下吗?谢谢
哦,这是我的代码:
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
int getUserOption();
void copyFiles();
string getFileName();
int calcFactorial();
void countWords();
void countBytes();
int main()
{
//List of main variables
int option;
int userFactorialInt;
do {
option = getUserOption();
switch (option) {
case 0:
break;
case 1:
cout << "\n\nYou Chose COPY\n" << endl;
copyFiles();
break;
case 2:
cout << "\n\nYou Chose FACTORIAL\n" <<
"\nPlease type an integer number between 1 and 12: ";
cin >> userFactorialInt;
cin.ignore(8192, '\n');
if (userFactorialInt >= 1, userFactorialInt <= 12)
{
cout << userFactorialInt << "'s factorial is: " << calcFactorial() << endl << endl;
}
else
{
cout << "Error, the number must be between 1 and 12" << endl;
}
break;
case 3:
cout << "\n\nYou Chose COUNT WORDS\n";
case 4:
cout << "\n\nYou Chose COUNT BYTES\n";
case -1:
cout << "\n\n*Invalid option entered.* Did you remember to type the word"<<
"\nin full caps? A valid option is demonstrated below:"<<
"\n(example) \nEnter Option: COUNT WORDS \n\n" << endl;
getUserOption();
break;
}
} while (option != 0);
cout << "Program ending" << endl;
return 0;
}
int getUserOption() {
string userInput;
cout << "-------------------------------------------------------------------------------\n"<<
"Enter one of the following options in full capital letters as it appears:\n\n" <<
"QUIT - Exit the program\n" <<
"COPY - Copy the contents of a .txt file\n" <<
"FACTORIAL - Calculate the factorial of an integer between 1 and 12\n" <<
"COUNT WORDS - Count the number of words in a .txt file\n" <<
"COUNT BYTES - Count the number of bytes in a .txt file\n" <<
"Enter option: ";
getline(cin, userInput);
if (userInput == "COPY") {
return 1;
}
else if (userInput == "FACTORIAL") {
return 2;
}
else if (userInput == "COUNT WORDS") {
return 3;
}
else if (userInput == "COUNT BYTES") {
return 4;
}
else if (userInput == "QUIT") {
return 0;
}
else {
return -1;
}
return 0;
}
string getFileName()
{
string inFileName;
cout << "Enter input file address: ";
getline(cin, inFileName);
return inFileName;
}
void copyFiles()
{
ifstream inFile;
ofstream outFile;
string inputFileName, outputFileName, lineBuffer;
cout << "Enter output file name: ";
getline(cin, outputFileName);
do
{
inputFileName = getFileName();
inFile.open(inputFileName.c_str()); //Try the open inputFileName
if (inFile.good() != true) { // If "it works" is not true,
cout << "Error: Invalid input file address. \n" //tell the user it doesn't work.
<< strerror(errno) << endl; //If open did not work tell why
cout << "Please type a valid file address\n" <<
"eg: C:\\Users\\ThisUnit\\Documents\\CS161\n";
}
} while (inFile.good() != true); //Loop as long as the open fails
outFile.open(outputFileName.c_str()); //Open the output
do {
getline(inFile, lineBuffer); //Read a line of input
if (inFile.eof() != true) {
outFile << lineBuffer << endl; //If read worked, write to output
}
} while (inFile.eof() != true); //Repeat for all lines in the file
inFile.clear();
inFile.close();
}
**感谢编辑
【问题讨论】:
-
你从未定义过
calcFactorial... -
@chris 我将如何再次定义它?同样,在情况 2 中,我想创建一个循环,如果它无法检测到 1 到 12 之间的有效整数,它将继续循环。我不确定如何构造该循环,并且我保留自己不插入 calcFactorial。有什么建议吗?
-
它需要一个身体。没有主体的函数不能调用。