c++ - 为什么 Visual Studio 既可以工作又会为看起来稳定的 c++ 数据库程序提供错误?
问题描述
我决定在提交数据库相关任务之前测试一个程序。我应该创建一个管理员工表的系统,接受各种输入并将它们应用于数据库。
在开发过程中,我确保每个输入都工作多次。初始测试,每次运行程序时它都有效。我能够添加一个 id 为 1010 的用户(尚未在系统中。如果该 ID 已经存在,则系统会丢弃该信息并通知用户),然后创建一个无意义的用户,并且它起作用了。
我返回程序添加了一些最终编辑,例如正确显示输出、更好的格式等以及一般的清理和注释,当我注意到在运行应用程序并尝试相同的功能时,程序显示了一些奇怪的行为。
- 在第一次运行时,该程序检测到我输入的所有员工 ID(甚至是 99999 之类的无意义的 ID)都已经存在。当我退出并返回程序时,这个ID突然被接受了,好像什么也没发生一样
- 当我成功地将用户输入数据库时,它会立即自动退出程序(功能的选择是通过 switch/case 语句完成的)。
- 2.发生后,我尝试重新运行程序,只是它反复输入“名称”输入,然后跳过所有其他输入并立即退出程序。没有错误信息,什么都没有
- 当我试图恢复我的所有更改时,它继续发生,即使是在我几天前制作的最稳定的版本上,然后突然再次重复这种行为。
我不完全确定我做错了什么导致它这样做。这在VS中很常见吗?
供参考,我的代码如下
这是案例 3 的代码,即处理入口的代码
case 3:
cout << "Please Enter the employee number of the employee to add" << endl;
cin >> empNum;
failsafe = findEmployee(conn, empNum, emp);
if (failsafe == 1) {
if (emp.empNum == 0) {
cout << "Employee does not exist or is not in our records. Entry can proceed" << endl;
cout << "proceeding with entry" << endl;
cout << "-------------" << endl;
cout << "Enter the employee's Number: ";
cin >> emp.empNum;
cin.ignore();
cout << "Enter the employee's first name: ";
cin >> emp.firstName;
cout << "Last name: ";
cin >> emp.lastName;
cout << "Enter Employee Email: ";
cin >> emp.email;
cout << "Enter Employee's Extension: ";
cin >> emp.extension;
cout << "Enter the employee number of the person this employee reports to: ";
cin >> emp.reportsTo;
cout << "What is this employees' current position? ";
cin >> emp.jobTitle;
cout << "What city is this employee working within? ";
cin >> emp.city;
cout << "Compiling employee data" << endl;
insertEmployee(conn, emp);
}
else {
cout << empNum;
cout << "An employee with the same number exists." << endl;
}
}
else if (failsafe == 0) {
cout << "Judging from the way this program reads data, you shouldn't be able to see this"<<endl; //In case the function for some reason *doesnt* return a 1 like it's been doing every time
}
break;
这是处理将员工添加到数据库的代码
void insertEmployee(MYSQL* conn, struct Employee& emp) {
int queResult;
MYSQL_RES* res;
cout << "This and the below line is just here for testing to see what exactly it's skipping over" << endl;
pause();
if (conn) {
string query = ("INSERT INTO schoolschema.employees(employeeNumber, lastName, firstName, extension, email, officeCode, reportsTo, jobTitle)"
" VALUES (" + std::string(" \' " ) + to_string(emp.empNum) + std::string(" \' ") + " , " + " \' " + emp.lastName + " \' " + " , " + " \' " + emp.firstName + " \' " + " , " + std::string(" \' ") + emp.extension + " \' " + " , " + " \' " + emp.email + " \' " + " , " + to_string(1) + " , " + std::string(" \' ") + emp.reportsTo + " \' "+ " , " + " \' " + emp.jobTitle + " \' " + ");");
const char* q = query.c_str();
queResult = mysql_query(conn, q);
if (!queResult) {
cout << "Employee successfully added, check to verify by checking the number of the employee from the main menu, or using your DBMS application" << endl;
return;
}
else {
cout << "Error occurred: " << mysql_errno(conn) << " " << mysql_error(conn) << endl;
}
}
}
是的,我知道整个
+" \' "+
事情有点难看,但这是我遇到的另一个问题的解决方法。
如果有人认为错误可能在其他地方,我可以发布更多代码片段。
作为补充说明,我认为问题不在于“查找员工”,因为该函数似乎正确地返回了信息,只是编译器似乎在挑选这些数据是否正确。(您可以在下面看到我可以删除用户 1010 但我无法将其添加回来)
发生的错误
********************* HR Menu *********************
1) Find Employee
2) Employees Report
3) Add Employee
4) Update Employee
5) Remove Employee
0) Exit
5
Enter employee number to delete: 1010
Employee found, deleting
Employee successfully deleted
********************* HR Menu *********************
1) Find Employee
2) Employees Report
3) Add Employee
4) Update Employee
5) Remove Employee
0) Exit
3
Please enter number to add
1010
employee does not exist, proceeding with entry
Enter Employee number: 1010
Last name: Enter Employee Email: Enter Employee's Extension: Enter the employee number of the person this employee reports to: What is this employees' current position? What city is this employee working within? Compiling employee data
Press enter to continue...
编辑:添加注释:出现上述错误后(说按回车继续),它会显示一个sql错误,然后立即假设用户输入“0”并退出
解决方案
推荐阅读
- windows - 我可以将带有 Arduino Uno 3 的 HC-08 UART 连接到计算机吗?
- rust - Rust:返回拥有的值和对返回值的某些部分的借用引用
- matplotlib - Anaconda 2020.07 Matplotlib 未更新
- javascript - 第一个单选按钮的值总是被发送
- git - 当本地更改原始分支时,我丢失了我的提交
- c++ - 如何获取 ApplicationFrameHost.exe 的实际进程名称
- java - AlarmManager setTimeZone 在 API 26 中不起作用?
- java - 如何使用基于 gradle 的项目生成带有失败截图和 cucumber.json 文件的黄瓜 html 报告
- flutter - Flutter:未处理的异常:NoSuchMethodError:方法'findAncestorStateOfType'在null上被调用
- c - CS50(2020)恢复程序中的分段错误