2016-02-08 44 views
0

我編碼在COBOL,加入後的if語句下面列出的,我一直得到 「 在第‘000-主’: 錯誤:語法錯誤,意想不到的IF」語法錯誤在COBOL程序,意外,如果

Identification Division. 
    Program-ID. Lab2. 
    *This program will generate a statement for a single investment 
    *that compounds interest monthly. 
    * It will prompt use for 3 inputs, calculate total interest 
    * earned, final balance, & entire account balance schedule. 
    * It will then display all output to the console. 
    Environment Division. 
    Data Division. 
    Working-Storage Section. 
    01 invest-amt  pic S9(9)V99. 

    01 invest-error-msg  pic x(40) value "Investment " 
    &  "Amount must be positive". 

    01 int-rate  pic S9(2)V99. 
    01 int-rate-error-msg pic x(40) value "Annual Interest " 
    &  "Rate must be positive". 

    01 int-rate-final pic 9V99999. 


    01 num-months pic S9(3). 
    01 num-months-error-msg pic x(40) value "Number of Months " 
    &  "must be positive". 

    01 multiply-rate pic 9V99999. 

    01 blank-line  pic x value " ". 


    01 month-counter pic 99 value 1. 
    01 balance-month pic 9(9)V99. 
    01 interest-month pic 9(9)V99. 

    01 total-interest pic 9(9)V99. 
    01 final-balance pic 9(9)V99. 

    01 additional-value pic S9(9)V99. 
    01 add-value-error-msg pic x(40) value "Additional Value " 
    &  "must be positive". 
    01 Q   pic 9(3). 
    01 R   pic 9(3). 
    01 months-in-year pic 9(2) Value 12. 


    Procedure Division. 
    000-main. 
     perform 100-initialize 

     perform until invest-amt >=0 
      display invest-error-msg 
      perform 200-input 
     end-perform 

     perform 210-input-rate 

     perform until int-rate >=0 
      display int-rate-error-msg 
      perform 210-input-rate 
     end-perform 

     perform 220-input-month 

     perform until num-months >=0 
      display num-months-error-msg 
      perform 220-input-month 
     end-perform 

     perform 230-input-additional-value 

     perform until additional-value >=0 
      display add-value-error-msg 
      perform 230-input-additional-value 
     end-perform 

     perform 300-print-words 

     perform 400-process-interest 
     perform 310-print-values 
     display blank-line 

     perform until month-counter = num-months 
      add 1 to month-counter 

      Divide month-counter By months-in-year Giving Q Remainder R 

一切正在編譯罰款,直到我添加下面的if語句。

  if ((num-months> months-in-year) & (R=0)) 
      Add additional-value to balance-month 

      multiply month-counter by months-in-year 

      add interest-month to balance-month rounded 
      perform 400-process-interest 
      perform 310-print-values 
      display blank-line 
     end-perform 
+0

對於支持它的編譯器,'&'是一個串聯運算符。你不能只是編寫語法。掌握編譯器的手冊。只要提一下,通過不使用空格限制符號,就可以讓代碼看起來很sl sl。 –

+0

您的'Divide'超出了固定格式COBOL的有效列。這和IF中的'&'是問題,儘管似乎有一些編譯器支持IF中的'&'。從縮進中你期待一個'END-IF'',但由於它是可選的,它不是編譯錯誤(儘管幾乎肯定是一個邏輯錯誤)。 –

回答

3

我的編譯器抱怨缺少end-if的和不喜歡&。我以明顯的方式修復了問題。

插入end-ifAdd additional-value to balance-month似乎產生一個乾淨的編譯。當然,最後的end-perform需要一個句號

最終執行

我懷疑你的編譯器正在生成那條消息,因爲你的if語句格式錯誤。

0

我沒有編譯你的代碼。

您錯過了IF語句的END-IF。 您還應該放置「STOP RUN」。在你的程序中。 例如...

perform until month-counter = num-months 
     add 1 to month-counter 

     Divide month-counter By months-in-year Giving Q Remainder R 
     if ((num-months> months-in-year) & (R=0)) 
     Add additional-value to balance-month 
     end-if ******* <- YOU NEED THIS 
     multiply month-counter by months-in-year 

     add interest-month to balance-month rounded 
     perform 400-process-interest 
     perform 310-print-values 
     display blank-line 
    end-perform 
    STOP RUN. *** <- THIS WOULD BE NICE AS WELL. 
+1

你用什麼編譯器支持'&'for和?這是非標準的。你編譯成什麼,固定或自由格式?由於你看不到整個程序,是什麼讓你認爲'STOP RUN'缺失? 'END-IF'是可選的,所以雖然它可能需要一個,但是從縮進中不會導致編譯錯誤。 –