【发布时间】:2021-11-24 09:43:39
【问题描述】:
我没有看到这个问题已经发布,或者任何有用的类似内容。
我正在上第二学期的 C++ 课程,并且只通过了 10 个测试点中的 5 个。我可以看出我的 try/catch 设置有问题。在网上搜索之后,我可能对我遇到的错误更加困惑:
main.cpp: In function ‘std::string FindID(std::string, std::ifstream&)’:
main.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
24 | }
| ^
main.cpp: In function ‘std::string FindName(std::string, std::ifstream&)’:
main.cpp:49:1: warning: control reaches end of non-void function [-Wreturn-type]
49 | }
| ^
问题和我的代码如下:
6.9 LAB:找不到学生信息
给定一个在文本文件中搜索学生 ID 或姓名的程序,完成 FindID() 和 FindName() 函数。然后,在main()中插入一条try/catch语句,捕获FindID()或FindName()抛出的任何异常,并输出异常信息。文本文件中的每一行都包含由空格分隔的名称和 ID。
函数FindID() 有两个参数:学生姓名(字符串)和文本文件内容(ifstream)。如果学生姓名在文件中,函数FindID() 返回与学生姓名相关联的 ID,否则,该函数会抛出带有消息“Student ID not found for studentName”的runtime_error,其中 studentName 是学生的姓名.
函数FindName() 有两个参数:学生的ID(字符串)和文本文件的内容(ifstream)。如果 ID 在文件中,函数 FindName() 将返回与学生 ID 关联的名称,否则,该函数将引发 runtime_error 并显示消息“Student name not found for studentID”,其中 studentID 是学生的 ID。
主程序接受用户的三个输入:文本文件的名称(字符串)、用于查找学生 ID 或姓名的搜索选项(int)以及学生的 ID 或姓名(字符串) .如果搜索选项为 0,则使用学生姓名作为参数调用 FindID()。如果搜索选项为 1,则使用学生 ID 作为参数调用 FindName()。主程序输出搜索结果或捕获到的异常信息。
例如:如果程序的输入是:
roster.txt 0 Reagan
而 roster.txt 的内容是:
Reagan rebradshaw835
Ryley rbarber894
Peyton pstott885
Tyrese tmayo945
Caius ccharlton329
程序的输出是:
rebradshaw835
例如:如果程序的输入是:
roster.txt 0 Mcauley
程序输出异常信息:
Student ID not found for Mcauley
例如:如果程序的输入是:
roster.txt 1 rebradshaw835
程序的输出是:
Reagan
例如:如果程序的输入是:
roster.txt 1 mpreston272
程序输出异常信息:
Student name not found for mpreston272
我的 main.cpp 代码:
#include <string>
#include <iostream>
#include <stdexcept>
#include <fstream>
using namespace std;
string FindID(string name, ifstream &infoFS) {
bool foundID = false;
string input;
infoFS >> input;
while (infoFS)
{
if (input == name)
{
infoFS >> input;
return input;
}
else { infoFS >> input; }
}
if (foundID == false)
{
throw name;
}
}
string FindName(string ID, ifstream &infoFS) {
bool foundName = false;
string previous;
string input;
infoFS >> previous;
infoFS >> input;
while (infoFS)
{
if (input == ID)
{
foundName = true;
return previous;
}
else
{
previous = input;
infoFS >> input;
}
}
if (foundName == false)
{
throw ID;
}
}
int main() {
int userChoice;
string studentName;
string studentID;
string studentInfoFileName;
ifstream studentInfoFS;
// Read the text file name from user
cin >> studentInfoFileName;
// Open the text file
studentInfoFS.open(studentInfoFileName);
// Read search option from user. 0: FindID(), 1: FindName()
cin >> userChoice;
try
{
if (userChoice == 0) {
cin >> studentName;
studentID = FindID(studentName, studentInfoFS);
cout << studentID << endl;
}
else {
cin >> studentID;
studentName = FindName(studentID, studentInfoFS);
cout << studentName << endl;
}
}
catch (string name)
{
if (userChoice == 0) {
cout << "Student ID not found for " << name << endl;
}
}
catch (char ID)
{
cout << "Student name not found for " << ID << endl;
}
studentInfoFS.close();
return 0;
}
我提交时的编译器警告:
main.cpp: In function ‘std::string FindID(std::string, std::ifstream&)’:
main.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
24 | }
| ^
main.cpp: In function ‘std::string FindName(std::string, std::ifstream&)’:
main.cpp:49:1: warning: control reaches end of non-void function [-Wreturn-type]
49 | }
| ^
通过测试:
1: Compare output
Input
roster.txt 0 Reagan
Your output
rebradshaw835
2: Compare output
Input
roster.txt 0 Mcauley
Your output
Student ID not found for Mcauley
3: Compare output
Input
roster.txt 1 rebradshaw835
Your output
Reagan
7: Unit test
Test FindName("mpreston272", infoFS) with roster1.txt, should return Mcauley
Your output
FindName("mpreston272", infoFS) correctly returned Mcauley
8: Unit test
Test FindID("Mcauley", infoFS) with roster1.txt, should return mpreston272
Your output
FindID("Mcauley", infoFS) correctly returned mpreston272
测试失败:
4: Compare output
Input
roster.txt 1 mpreston272
Your output
Your program produced no output
Expected output
Student name not found for mpreston272
5: Unit test
Test FindID("John", infoFS) with roster1.txt, should throw an exception
Returned unexpected error code (-6)
6: Unit test
Test FindName("jsmith474", infoFS) with roster1.txt, should throw an exception
Returned unexpected error code (-6)
【问题讨论】:
-
函数期望你返回一个字符串,你什么都没有返回。
-
你用的是什么
C++标准版? -
当
foundID或foundName是true时,你应该返回什么? -
您收到警告是因为您的
if (foundID == false)。删除条件,因为无论如何它都没用。 -
另外,不要用
throw和std::string、throw和std::runtime_error代替。然后,将您的exception处理程序更改为:catch ( const std::exception& error )
标签: c++ exception try-catch ifstream