因此,我正在編寫這個簡單的程序,使用發現的高斯算法計算任何日期的日期here。'未在此範圍內聲明'錯誤
#include <iostream>
using namespace std;
//Using the Gaussian algorithm
int dayofweek(int date, int month, int year){
int d=date;
if (month==1||month==2)
{int y=((year-1)%100);int c=(year-1)/100;}
else
{int y=year%100;int c=year/100;}
int m=(month+9)%12+1;
int product=(d+(2.6*m-0.2)+y+y/4+c/4-2*c);
return product%7;
}
int main(){
cout<<dayofweek(19,1,2054);
return 0;
}
這是一個非常簡單的程序,更令人費解的是輸出。
:In function dayofweek(int, int, int)’:
:19: warning: unused variable ‘y’
:19: warning: unused variable ‘c’
:21: warning: unused variable ‘y’
:21: warning: unused variable ‘c’
:23: error: ‘y’ was not declared in this scope
:25: error: ‘c’ was not declared in this scope
它說,我的變量是未使用,但然後說它沒有聲明?任何人都可以告訴我什麼是錯的。
局部變量在聲明它們的'{}'塊之外是不可見的。 – DCoder 2012-04-07 16:18:04