0

我正在尝试编写一个程序,该程序将计算用户输入的日期所在的星期几。我不断收到错误“警告 C4700:使用了未初始化的局部变量‘年份’。” 我无法弄清楚如何初始化该变量。另外,当我运行它时,我输入的日期错误。谁能帮我?

#include <iostream>
#include <string>
using namespace std; 
int main(void)
{                                                       
string Days[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday"}; 

int a,month,year,y,day,m,d;
month=(1,2,3,4,5,6,7,8,9,10,11,12); 
day=( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
year=   
a=(14-month)/12;
y=year-a;
m=month+12*a-2;
d=(day+y+y/4-y/100+y/400+(31*m/12))&7;


cout << "Welcome to 'Day Of The Week Calculator!'" <<endl;  //display message 
cout << "Enter Month 1 - 12:" <<endl;                          //prompt user for month data
cin >> month;                                               //read integer from user into month
cout << "Enter day 1 - 31:" <<endl;                         //prompt user for day data
cin >> day;                                                 //read integer from user into day 
cout << "Enter year >1582:" <<endl;                         //prompt user for year data
cin >> year;                                                //read in integer from user into year 
cout <<endl <<"The Date: "<<month<<"/"<<day<<"/"<<year      //answer to day of week calculation
<<" Falls on a: "<< Days[d]<<endl;
return 0;                                                   //indicate that program ended successfully
4

1 回答 1

1

一些修复而不是 &7

d=(day+y+y/4-y/100+y/400+(31*m/12))&7

模式 7

d=(day+y+y/4-y/100+y/400+(31*m/12))%7

将您的输入放在方程式之前,然后将结果输出。放下

month=(1,2,3,4,5,6,7,8,9,10,11,12); 
day=( 1,2,3,4,5,6,7,8,9,10,11,1 ...

享受你漫长的周末——我们都去过那里。

于 2013-05-25T02:07:17.270 回答