Here is the code that I\'ve written. It compiles with no errors, only warnings.
ID: 3626527 • Letter: H
Question
Here is the code that I've written. It compiles with no errors, only warnings. I'm using Dev C++ as my compiler. Why does my program run and immediately close? I am missing something fundamental but can't figure out what it is. How do I prompt the user if they want to continue or not? Here is sample output:Please enter the month: 10
Please enter the day: 3
Please enter the year: 1999
10/3/1999 falls on a Sunday
Would you like to continue? (y/n)n
//program to compute day of week
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//varibles section
int month;
int day;
int year;
int ValidCount;
int ValidDate;
double p;
int answer;
int k;
int l;
int o;
int z1;
int z2;
int z3;
int z4;
int z;
int y;
ValidCount = 0;
while(answer == y)
{
ValidDate = 1;
cout << "Please enter the month: ";
cin >> month;
cout << "Please enter the day: ";
cin >> day;
cout << "Please enter the year: ";
cin >> year;
if(year <= 1753)
{
ValidDate = 0;
}
if(day < 1 || day > 31)
{
day = 0;
}
if(month < 1 || month > 12)
{
month = 0;
}
if (ValidDate == 1)
{
k = 0.6 + 1.0 / month;
l = year - k;
o = month + 12 * k;
p = l / 100.0;
z1 = p / 4;
z2 = p;
z3 = (5 * l) / 4.0;
z4 = (13 * (o + 1)) / 5.0;
z = z4 + z3 - z2 + z1 + day - 1;
z = z - (7 * (z / 7)) + 1;
}
if(z == 1)
{
cout << "Sunday";
}
if(z == 2)
{
cout << "Monday";
}
if(z == 3)
{
cout << "Tuesday";
}
if(z == 4)
{
cout << "Wednesday";
}
if(z == 5)
{
cout << "Thursday";
}
if(z == 6)
{
cout << "Friday";
}
if(z == 7)
{
cout << "Saturday";
}//end of if
}//end of while
return 0;
}//end of main function
Explanation / Answer
please rate - thanks
//program to compute day of week
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//varibles section
int month;
int day;
int year;
int ValidCount;
int ValidDate;
double p;
char answer='y';
int k;
int l;
int o;
int z1;
int z2;
int z3;
int z4;
int z;
int y;
ValidCount = 0;
while(answer == 'y')
{
ValidDate = 1;
cout << "Please enter the month: ";
cin >> month;
cout << "Please enter the day: ";
cin >> day;
cout << "Please enter the year: ";
cin >> year;
if(year <= 1753)
{
ValidDate = 0;
}
if(day < 1 || day > 31)
{
day = 0;
}
if(month < 1 || month > 12)
{
month = 0;
}
if (ValidDate == 1)
{
k = 0.6 + 1.0 / month;
l = year - k;
o = month + 12 * k;
p = l / 100.0;
z1 = p / 4;
z2 = p;
z3 = (5 * l) / 4.0;
z4 = (13 * (o + 1)) / 5.0;
z = z4 + z3 - z2 + z1 + day - 1;
z = z - (7 * (z / 7)) + 1;
}
if(z == 1)
{
cout << "Sunday";
}
if(z == 2)
{
cout << "Monday";
}
if(z == 3)
{
cout << "Tuesday";
}
if(z == 4)
{
cout << "Wednesday";
}
if(z == 5)
{
cout << "Thursday";
}
if(z == 6)
{
cout << "Friday";
}
if(z == 7)
{
cout << "Saturday";
}//end of if
cout<<" do it again? (y/n): ";
cin>>answer;
}//end of while
return 0;
}//end of main function