我正在尝试编写一个程序来计算用户输入日期的星期几。我一直收到错误“警告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
答案 0 :(得分:1)
d=(day+y+y/4-y/100+y/400+(31*m/12))&7
mod 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 ...
享受漫长的周末 - 我们都去过那里。