Board logo

標題: 變數PI的程式問題 [打印本頁]

作者: 花之慶次    時間: 2006-11-28 00:31     標題: 變數PI的程式問題

以下為變數PI的程式設計的問題
#include<iostream>
#include<stdlib.h>
using namespace std;
const float PI = 3.14158;     // PI為全域變數
int main()
{
     int i=5;      // i 為局部變數
     {
        int j=10;    //  J為局部變數
        cout << 10*PI + i*j << '\n';
     }
     cout << j << '\n';   // 錯誤, 因j已經消失
     system("pause");
     return 0;
}
         

請留意一下第十二行:
cout << j << 'n';     // 錯誤, 因j已經消失是什麼意思
因為在Dev-C++中, 有關訊息是這樣的:
第十二行: '
第十二行:[each
作者: d_chu    時間: 2006-11-28 17:38

Is it becuase you declare 'j' inside of the bracket?? I am not 100% sure... since I dont have a complier installed on my pc...

since you declare inside the bracket, this can be used within the barcket only....

int i=5;      // i 為局部變數
     {  <<-------------------------------------------------------- open bracket
        int j=10;    //  J為局部變數  <-------------------- variable you declare only works within brackets
        cout << 10*PI + i*j << '\n';
     }  <<-------------------------------------------------------- close bracket
     cout << j << '\n';   // 錯誤, 因j已經消失
     system("pause");
     return 0;
作者: hhl    時間: 2006-11-29 00:49

exactly, all the variables within the bracket will be destroyed ...




歡迎光臨 香港 Xocat Forum 討論區 (http://p.xocat.com/p/) Powered by Discuz! 6.0.0