-
Programming not running
I have gotten my program to compile but it is not running properly. What is wrong? Also how do I incorporate an exit routine.
<code>
#include <iostream>
using namespace std;
int get_date(int, int, int); // function declaration
int calc_year(int the_year);
int main()
{
int m, day, year, our_day; //month, day and year
cout<< "\nEnter any date as three sets of numbers.\n";
cout<< "(month, day, year). Example: 7 29 1971\n";
cout<<"Entering invalid dates will exit the program.\n";
cout<<"Enter date: ";
cin>>m>>day>>year; //input date
// The conditional inside the while statement could get you into a
// lot of trouble. You need to check your logic as to how you want it
// to really work.
while((m != 13)&(day !=32)&(year != 0000)) //Invalid dates exit the loop
{
our_day = get_date(m, day, year);
//Passes the three values to get_date function
//Turns a numerical month into a named month when printed
switch(m)
{
case 1:
cout <<"January "<< endl;
break;
case 2:
cout <<"February "<< endl;
break;
case 3:
cout <<"March "<<endl;
break;
case 4:
cout <<"April "<<endl;
break;
case 5:
cout <<"May "<<endl;
break;
case 6:
cout <<"June "<<endl;
break;
case 7:
cout <<"July "<<endl;
break;
case 8:
cout <<"August "<<endl;
break;
case 9:
cout <<"September "<<endl;
break;
case 10:
cout <<"October "<<endl;
break;
case 11:
cout <<"November "<<endl;
break;
case 12:
cout <<"December "<<endl;
break;
}
//Switch statement for the days of the week
switch (day)
{
case 1:
cout <<"Tuesday\n"<<endl;
break;
case 2:
cout <<"Wednesday\n"<<endl;
break;
case 3:
cout <<"Thursday\n"<<endl;
break;
case 4:
cout <<"Friday\n"<<endl;
break;
case 5:
cout <<"Saturday\n"<<endl;
break;
case 6:
cout <<"Sunday\n"<<endl;
break;
}
cout<<"\n\nEnter date: ";
cin>>m>>day>>year;
}
return 0;
}
get_date(int m, int day, int year); //function call
//get_date function
int get_date(int the_m, int the_day, int the_year)
{
//values for months of the year
int jan = 1, febr = 4, mar = 4, apr = 0,
may = 2, june = 5, july = 0, aug = 3,
sept = 6, octo = 1, nov = 4, dec = 6;
int the_month, the_date; //Variables used in calculations
if(the_m <3)
{
if(the_m == 2)
{
the_date = febr + the_day;
}
else{
if(the_m == 1)
{
the_date = jan + the_day;
}
}
}
else
{
switch(the_m)
{
case 3: the_month = mar; break;
case 4: the_month = apr; break;
case 5: the_month = may; break;
case 6: the_month = june; break;
case 7: the_month = july; break;
case 8: the_month = aug; break;
case 9: the_month = sept; break;
case 10: the_month = octo; break;
case 11: the_month = nov; break;
case 12: the_month = dec; break;
}
if((the_year%4 == 0) &! (the_year%100 == 0))
{
the_date = the_day + (the_month + 1);
}
else{
if(the_year%400 == 0 )
{
the_date = the_day + (the_month + 1);
}
else
{
the_date = the_day + the_month;
}
}
}
int first_day; //declares int
first_day = calc_year(the_year); //passing the_year to the new function, and assigning the return value to first_day
int this_day; //declaration for calculation.
this_day = the_date + first_day; //add the return value(count_day) to the_date and assign the sum to this_day.
//While () loop counts backwards by decrementing until it reaches the day
while(this_day %= 7)
{
this_day %= 7;
}
this_day = this_day - 1; //Make the day before Monday (0 - 1) be Sunday
if(this_day == -1)
{
this_day = 6;
}
return this_day;
return 0;
}
//calc_year function
int calc_year(int the_year)
{
int count_day = 0; //start on Monday
//1900 started on a Monday, so don't enter the loop
for(int i = 1901; i <= the_year; i++)
{
count_day++; //day of the week.
if(count_day >= 7)
{
count_day = count_day - 7;
}
//reset at the end of each week
if((0 == ((i-1)%4)) &! (0 == ((i-1)%100)))
{
count_day = count_day + 1; //adjust for leapyear
}
else
{
if(0 == ((i-1)%400))
{
count_day = count_day + 1;
}
}
}
if(count_day == 7) //Make the day after Sunday (6 + 1) become Monday
{
count_day = 0;
}
return count_day;
}
</code>
-
See the column to the left for the proper code tag format.
That aside:
Code:
while((m != 13)&(day !=32)&(year != 0000))
You are using the bitwise and. You want want the logical and. That is to say:
Code:
while( foo && bar )
You've got another problem:
Code:
while((m != 13)&(day !=32)&(year != 0000)) //Invalid dates exit the loop
{
our_day = get_date(m, day, year);
switch(m)
{
...
}
switch (day)
{
...
}
cout<<"\n\nEnter date: ";
cin>>m>>day>>year;
}
return 0;
}
get_date(int m, int day, int year); //function call
Why do you have a function call outside of your main loop? This is actuallly a function prototype, not an actual call to the function. You usually prototype functions before the main loop.
Quzah.