作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我似乎不明白为什么我的 IF 语句被跳过了。在 C++ 中使用 SQL。该程序跳过我的前两个 IF 语句并跳转到 else 分支。完全不知道为什么要这样做。这是我的编码。
void add_technician() {
EXEC SQL BEGIN DECLARE SECTION;
char sn[10];
int s = 0;
char answer;
int umn;
char tname[30];
char tadd[30];
char tpho[10];
char tmod[15];
EXEC SQL END DECLARE SECTION;
cout << "Enter social security number:";
cin >> sn;
EXEC SQL SELECT count(*) into :s from Employees where SSN= :sn;
if (s == 1)
{
cout << "Employee already exists in the database.";
cout <<"Would you like to update the union-membership-number?";
cin >> answer;
if (answer == 'y'|| 'Y')
{cout <<"Enter new union member number:";
cin >> umn;
EXEC SQL
INSERT INTO Employee (ssn, union_member_no)
VALUES (:sn, :umn);
}
}
else {
cout << "Enter in union membership number of the new employee: ";
cin >> umn;
EXEC SQL INSERT INTO Employees (ssn, union_member_no)
VALUES (:sn, :umn);
EXEC SQL COMMIT WORK;
cout << "Enter the address of the technician.";
cin >> tadd;
cout << "Enter the name technician name.";
cin >> tname;
EXEC SQL INSERT INTO Technicians (address, name , phone)
VALUES (:tadd, :tname, :tpho);
EXEC SQL COMMIT WORK;
cout << "Enter airplane model number that you are an expert on." ;
cin >> tmod;
EXEC SQL INSERT INTO Experts (model_no, ssn)
VALUES (:tmod);
EXEC SQL COMMIT WORK; }
}
最佳答案
代码允许单个 SSN 分配多个 UMN,但第一个 if
语句没有考虑到这一点。它正在检查仅分配有 1 个 UMN 的 SSN。如果给定的 SSN 分配了多个 UMN,SELECT
将返回 count > 1
并且流程将跳转到您的 else
block 。
此外,您的第二个 if
语句格式不正确。 if (answer == 'y'|| 'Y')
将始终评估为 true
。您需要在每组条件中指定 answer
变量,如下所示:if ((answer == 'y') || (answer == 'Y'))
.
试试这个:
void add_technician()
{
EXEC SQL BEGIN DECLARE SECTION;
char sn[10];
int s = 0;
char answer;
int umn;
char tname[30];
char tadd[30];
char tpho[10];
char tmod[15];
EXEC SQL END DECLARE SECTION;
cout << "Enter social security number:";
cin >> sn;
EXEC SQL SELECT count(*) into :s from Employees where SSN= :sn;
if (s > 0)
{
cout << "Employee already exists in the database.";
cout << "Would you like to add a new union membership number?";
cin >> answer;
if ((answer == 'y') || (answer == 'Y'))
{
cout << "Enter new union member number:";
cin >> umn;
EXEC SQL INSERT INTO Employee (ssn, union_member_no) VALUES (:sn, :umn);
}
}
else
{
cout << "Enter union membership number of the new employee: ";
cin >> umn;
EXEC SQL INSERT INTO Employees (ssn, union_member_no) VALUES (:sn, :umn);
EXEC SQL COMMIT WORK;
cout << "Enter the address of the technician.";
cin >> tadd;
cout << "Enter the name of the technician.";
cin >> tname;
EXEC SQL INSERT INTO Technicians (address, name , phone) VALUES (:tadd, :tname, :tpho);
EXEC SQL COMMIT WORK;
cout << "Enter airplane model number that the technician is an expert on." ;
cin >> tmod;
EXEC SQL INSERT INTO Experts (model_no, ssn) VALUES (:tmod);
EXEC SQL COMMIT WORK;
}
}
关于c++ - 无法弄清楚为什么我的 IF 语句在使用 C++ 的 SQL 中被跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10218054/
我是一名优秀的程序员,十分优秀!