user2419304
user2419304

Reputation:

Day of the Week calendar calculator

I'm trying to write a program which will calculate the dayof the week a date inputted by a user would fall on. I keep getting the error "warning C4700: uninitialized local variable 'year' used." I cannot figure out how to initialize that variable. Also, when I run it I get the wrong day for the date I input. Can anyone help me?

#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

Upvotes: 0

Views: 6034

Answers (1)

chux
chux

Reputation: 153447

some fixes instead of &7

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

Put your input before your equations and your result output after. Drop the

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 ...

Enjoy your long week-end - we've all been there.

Upvotes: 1

Related Questions