I did not see this question already posted, or anything helpfully similar.
I'm in a 2nd semester C++ class and am only passing 5 of 10 test points. I can tell something is wrong with my try/catch setup. After scouring the internet I'm possibly more confused about the errors I'm getting:
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 | }
| ^
Problem and my code below:
6.9 LAB: Student info not found
Given a program that searches for a student’s ID or name in a text file, complete the FindID()
and FindName()
functions. Then, insert a try/catch statement in main()
to catch any exceptions thrown by FindID()
or FindName()
, and output the exception message. Each line in the text file contains a name and ID separated by a space.
Function FindID()
has two parameters: a student's name (string) and the text file's contents (ifstream). The function FindID()
returns the ID associated with the student's name if the name is in the file, otherwise, the function throws a runtime_error
with the message "Student ID not found for studentName", where studentName is the name of the student.
Function FindName()
has two parameters: a student's ID (string) and the text file's contents (ifstream). The function FindName() returns the name associated with the student's ID if the ID is in the file, otherwise, the function throws a runtime_error with the message "Student name not found for studentID", where studentID is the ID of the student.
The main program takes three inputs from a user: the name of a text file (string), the search option for finding the ID or name of a student (int), and the ID or name of a student (string). If the search option is 0, FindID() is invoked with the student's name as an argument. If the search option is 1, FindName() is invoked with the student's ID as an argument. The main program outputs the search result or the caught exception message.
Ex: If the input of the program is:
roster.txt 0 Reagan
and the contents of roster.txt are:
Reagan rebradshaw835
Ryley rbarber894
Peyton pstott885
Tyrese tmayo945
Caius ccharlton329
the output of the program is:
rebradshaw835
Ex: If the input of the program is:
roster.txt 0 Mcauley
the program outputs an exception message:
Student ID not found for Mcauley
Ex: If the input of the program is:
roster.txt 1 rebradshaw835
the output of the program is:
Reagan
Ex: If the input of the program is:
roster.txt 1 mpreston272
the program outputs an exception message:
Student name not found for mpreston272
my main.cpp code:
#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;
}
Compiler warnings when I submit:
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 | }
| ^
PASSED TESTS:
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
FAILED TESTS:
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)