0

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
  • 2
    @user2419304: there's so many things wrong with your code I don't even know where to start. You should read a [beginner's C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to get you started. No offence meant. – syam May 25 '13 at 00:58
  • @syam ah, I was confused by the multi-line separation. sorry about that. – taocp May 25 '13 at 01:04
  • @Syam this is my very first programming assignment in my intro class. Clearly, I don't have any experience, that's why I was looking for help. I have 2 books but they haven't been much help; i have been at this for 5hrs –  May 25 '13 at 01:10
  • 1
    @user2419304: I was not saying this in a mean way, it's just that you got so many things wrong that we can't decently explain them here. It's ok to be a beginner (we all were at some point) but you can't expect us to teach you such basic things, that's what books are for. The first thing you should be aware of is that instructions are evaluated in the order they appear, so `d` gets its value even before the user inputs anything. Also, I'm not sure what you think the comma operator `,` does but it's definitely not what you think. – syam May 25 '13 at 01:15
  • @user2419304 - Try reading those books and start with a simpler problem. The classic is hello world. Then read about arrays. etc. – Ed Heal Nov 04 '13 at 21:27
  • You don't have to initialize `day`, `month` or `year` when you create them. You just need to make sure that a value gets read in for them (the `cin` statements). – Max Lybbert Feb 02 '14 at 15:14
  • I'm not clear why day and month are assigned multiple values. Neither is an array, so the result is that they are both assigned the last item on the list (31 and 12). It's OK to leave variables uninitialized in some cases. (3rd time; I wish I could edit old comments instead of deleting/recreating them). – Max Lybbert Feb 03 '14 at 18:21

1 Answers1

1

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256