我的if语句中的一个,但不是两个都出现以下错误。
165 IGYPS2000-S Expected a data-name, but found "TO". The "ADD" statement was discarded.
169 IGYPS2096-S An incomplete condition was found in a conditional expression. The operand(s) was(were) discarded.
169 IGYPS2079-S Expected a verb or "NEXT SENTENCE", but found ""F"". The statement was discarded.
IF MARITAL-STATUS-IN = "S"
ADD 1 TO SINGLE-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO SINGLE-TOTAL
ADD SALARY-IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO SINGLE-MALE-COUNT
ADD SALARY-IN TO SINGLE-MALE-TOTAL
IF GENDER-IN = "F"
ADD 1 TO SINGLE-FEMALE-COUNT
ADD SALARY-IN TO SINGLE-FEMALE-TOTAL
END-IF.
IF MARITAL-STATUS-IN = "M"
ADD 1 TO MARRIED-COUNT
ADD 1 TO OVERALL-COUNT line 165
ADD SALARY-IN TO MARRIED-TOTAL
ADD SALARY IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO MARRIED-MALE-COUNT
ADD SALARY-IN TO MARRIED-MALE-TOTAL
IF GENDER-IN "F"
ADD 1 TO MARRIED-FEMALE-COUNT
ADD SALARY-IN TO MARRIED-FEMALE-TOTAL
END-IF.
如果您需要查看我的代码,请告诉我。这是我第一次编写嵌套的if。
答案 0 :(得分:3)
piet.t和Bruce Martin正确地发现了那些给你编译器诊断的错误。你没有帮助将错误的行列为数字165。
要避免此问题,最简单的方法是让编译器嵌入诊断程序,并在编译结束时列出它们。事实上,这不是您安装时的默认设置。
要解决它不是默认设置,你可以这样做:
CBL FLAG(I,I)
从第12列开始,并在测试时将其作为程序源中的第一行。
第一个我说“在编译列表末尾列出诊断消息,从严重性I向上”。第二个我说“将诊断消息嵌入源的编译列表中,从严重性I向上”。
所有编译器选项和有关使用方法的信息都包含在Enterprise COBOL编程指南中,可从IBM免费获得。
列表的相关部分看起来像这样(我只是粘贴而没有参考实际的列表):
IF MARITAL-STATUS-IN = "S"
ADD 1 TO SINGLE-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO SINGLE-TOTAL
ADD SALARY-IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO SINGLE-MALE-COUNT
ADD SALARY-IN TO SINGLE-MALE-TOTAL
IF GENDER-IN = "F"
ADD 1 TO SINGLE-FEMALE-COUNT
ADD SALARY-IN TO SINGLE-FEMALE-TOTAL
END-IF.
IF MARITAL-STATUS-IN = "M"
ADD 1 TO MARRIED-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO MARRIED-TOTAL
ADD SALARY IN TO OVERALL-TOTAL
165 IGYPS2000-S Expected a data-name, but found "TO". The "ADD" statement was discarded.
IF GENDER-IN = "M"
ADD 1 TO MARRIED-MALE-COUNT
ADD SALARY-IN TO MARRIED-MALE-TOTAL
IF GENDER-IN "F"
169 IGYPS2096-S An incomplete condition was found in a conditional expression. The operand(s) was(were) discarded.
169 IGYPS2079-S Expected a verb or "NEXT SENTENCE", but found ""F"". The statement was discarded.
ADD 1 TO MARRIED-FEMALE-COUNT
ADD SALARY-IN TO MARRIED-FEMALE-TOTAL
END-IF.
正如已经指出的那样,构建IF的方式也存在一些问题:
IF MARITAL-STATUS-IN = "S"
ADD 1 TO SINGLE-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO SINGLE-TOTAL
ADD SALARY-IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO SINGLE-MALE-COUNT
ADD SALARY-IN TO SINGLE-MALE-TOTAL
IF GENDER-IN = "F"
ADD 1 TO SINGLE-FEMALE-COUNT
ADD SALARY-IN TO SINGLE-FEMALE-TOTAL
END-IF.
IF MARITAL-STATUS-IN = "M"
ADD 1 TO MARRIED-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO MARRIED-TOTAL
ADD SALARY IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO MARRIED-MALE-COUNT
ADD SALARY-IN TO MARRIED-MALE-TOTAL
IF GENDER-IN "F"
ADD 1 TO MARRIED-FEMALE-COUNT
ADD SALARY-IN TO MARRIED-FEMALE-TOTAL
END-IF.
您使用两种不同的方法来终止您的IF。 END-IF范围分隔符和句号/句点。使用第二个意味着你的缩进与“逻辑”缩进不匹配。
IF MARITAL-STATUS-IN = "S"
ADD 1 TO SINGLE-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO SINGLE-TOTAL
ADD SALARY-IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO SINGLE-MALE-COUNT
ADD SALARY-IN TO SINGLE-MALE-TOTAL
IF GENDER-IN = "F"
ADD 1 TO SINGLE-FEMALE-COUNT
ADD SALARY-IN TO SINGLE-FEMALE-TOTAL
END-IF.
IF MARITAL-STATUS-IN = "M"
ADD 1 TO MARRIED-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO MARRIED-TOTAL
ADD SALARY IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO MARRIED-MALE-COUNT
ADD SALARY-IN TO MARRIED-MALE-TOTAL
IF GENDER-IN "F"
ADD 1 TO MARRIED-FEMALE-COUNT
ADD SALARY-IN TO MARRIED-FEMALE-TOTAL
END-IF.
注意,我没有更正您的GENDER-IN测试嵌套。
使用范围分隔符来结束任何条件要好得多。使用绝对最小的句号/句号。
PROCEDURE DIVISION.
any-paragraph-name-or-SECTION-name.
some code
.
即:PROCEDURE DIVISION标题结束(如果你有其中一个,则使用USING之后的项目);在段落/部分之前;段落/ SECTION标签的结尾;段落/部分结束;该计划结束。
那里有一些重叠,因为程序不要求其中有段落/ SECTIONS。
保持您的IF内容简单。避免重复代码。我举了一个你的代码的例子,重新安排,跟随。有两种样式,单一的嵌套-IF(因为这项工作的目的可能是嵌套-IF)和结婚的不同方式。
IF MARITAL-STATUS-IN = "S"
PERFORM COUNT-ANY-PERSON
PERFORM COUNT-SINGLE-PEOPLE
IF GENDER-IN = "M"
ADD 1 TO SINGLE-MALE-COUNT
ADD SALARY-IN TO SINGLE-MALE-TOTAL
END-IF
IF GENDER-IN = "F"
ADD 1 TO SINGLE-FEMALE-COUNT
ADD SALARY-IN TO SINGLE-FEMALE-TOTAL
END-IF
END-IF
IF MARITAL-STATUS-IN = "M"
PERFORM COUNT-ANY-PERSON
PERFORM COUNT-MARRIED-PEOPLE
END-IF
.
COUNT-ANY-PERSON.
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO OVERALL-TOTAL
.
COUNT-SINGLE-PEOPLE.
ADD 1 TO SINGLE-COUNT
ADD SALARY-IN TO SINGLE-TOTAL
.
COUNT-MARRIED-PEOPLE.
ADD 1 TO MARRIED-COUNT
ADD SALARY-IN TO MARRIED-TOTAL
IF GENDER-IN = "M"
ADD 1 TO MARRIED-MALE-COUNT
ADD SALARY-IN TO MARRIED-MALE-TOTAL
END-IF
IF GENDER-IN = "F"
ADD 1 TO MARRIED-FEMALE-COUNT
ADD SALARY-IN TO MARRIED-FEMALE-TOTAL
END-IF
.
已婚/单身可能互相排斥。如果是这样,请使用ELSE而不是第二次测试。男/女可能是互斥的。如果是这样,请使用ELSE而不是第二次测试。如果它们不是相互排斥的,那么找出你需要计算的其他内容,而不是让数据通过该程序。
您应该使用88级条件名称,而不是文字测试。使您的程序更易于理解,更易于维护。
答案 1 :(得分:2)
此行
IF GENDER-IN "F"
似乎是第169行(从错误消息判断),第165行应该是这一行:
ADD SALARY IN TO OVERALL-TOTAL
您似乎错过了-
和SALARY
之间的IN
。
在第169行,你有
IF GENDER-IN "F"
并且缺少=
。
你的拼写错误太多了,但由于缺少END-IF
,你的逻辑结构也出错了。
将缩进调整为编译器看到的内容:
IF MARITAL-STATUS-IN = "S"
ADD 1 TO SINGLE-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO SINGLE-TOTAL
ADD SALARY-IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO SINGLE-MALE-COUNT
ADD SALARY-IN TO SINGLE-MALE-TOTAL
IF GENDER-IN = "F"
ADD 1 TO SINGLE-FEMALE-COUNT
ADD SALARY-IN TO SINGLE-FEMALE-TOTAL
END-IF.
IF MARITAL-STATUS-IN = "M"
ADD 1 TO MARRIED-COUNT
ADD 1 TO OVERALL-COUNT
ADD SALARY-IN TO MARRIED-TOTAL
ADD SALARY IN TO OVERALL-TOTAL
IF GENDER-IN = "M"
ADD 1 TO MARRIED-MALE-COUNT
ADD SALARY-IN TO MARRIED-MALE-TOTAL
IF GENDER-IN "F"
ADD 1 TO MARRIED-FEMALE-COUNT
ADD SALARY-IN TO MARRIED-FEMALE-TOTAL
END-IF.