• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ Need help on a minor problem

BustaBust

Golden Member
// Calendar.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>


using namespace std;



//function prototypes
void WeekDay(float, float, int, int, int, int, int, int&);




int main()
{

float day;
float month;
int year;
int DayofWeek;
int intRes1 = 0;
int intRes2 = 0;
int intRes3 = 0;
int JDN = 0;
bool dataareOK = false;
do
{
cout << "What is the month? (mm) ";
cin >> month;
cout << "What is the day? (dd) ";
cin >> day;
cout << "What is the year? (yyyy) ";
cin >> year;

if (month < 13)
dataareOK = true;
else
cout << "You have entered an invalid month (1-12 only)"
<< endl;
if ( day<32);
else
cout << "You have entered an invalid day (1-31 only)"
<< endl;

if((year > 1582) || // years greater than 1582
(month > 10) && (year == 1582) || // months greater than 10 in 1582
(month == 10) && (day > 15) && (year == 1582)) // Month is greater than 10, day is greater than 15 in the year of 1582

{

}

} while (dataareOK == false);

WeekDay (day, month, JDN, year, intRes1, intRes2, intRes3, DayofWeek);
cin.get();
return 0;
}

void WeekDay(float day, float month, int year, int JDN,int intRes1,
int intRes2, int intRes3, int& DayofWeek )
{
//Conditions


intRes1 = (2- year /100 + year / 400);
intRes2 = int(365.25 * year);
intRes3 = int(30.6001 * (month + 1));
JDN = ((intRes1 + intRes2 + intRes3 + day) + 1720944.5);
DayofWeek = ((JDN + 1) % 7);

cout << "\nThe day of the week " << "for "
<< month <<"/"<< day <<"/"<< year << " is" << DayofWeek<<endl;
if (DayofWeek == 0)
cout << "Sunday";
else if (DayofWeek == 1) //Nested If
cout << "Monday";
else if (DayofWeek == 2) //Nested If
cout << "Tuesday";
else if (DayofWeek == 3) //Nested If
cout << "Wednesday";
else if (DayofWeek == 4) //Nested If
cout << "Thursday";
else if (DayofWeek == 5) //Nested If
cout << "Friday";
else if (DayofWeek == 6) //Nested If
cout << "Saturday";
cout << endl;

return;

}

Problem is output: MM/DD/0 <-- Year does not print out right... something to do with the
order of 'JDN' ?

Also, the 'blah blah is' shows the number after it....some kind of problem in the nested IF

Thanks for the help.
 
Function prototype:
void WeekDay(float day, float month, int year, int JDN,int intRes1,
int intRes2, int intRes3, int& DayofWeek )

Function Invocation:
WeekDay (day, month, JDN, year, intRes1, intRes2, intRes3, DayofWeek);

Swap JDN and year when you call WeekDay() -- you're passing parameters in the wrong order.
 
JDN is set to 0
int JDN = 0;

You are passing that in the parameter slot intended for year
WeekDay (day, month, JDN, year, intRes1, intRes2, intRes3, DayofWeek);
void WeekDay(float day, float month, int year, int JDN,int intRes1, int intRes2, int intRes3, int& DayofWeek )
 
Another thing is that when it prints out, it says "The day of the week for XX/XX/XXXX is2 Tuesday"

0=sun
1=mon
2=tues
3=wed
4=thurs
5=fri
6=sat

The number is printing out with the day and it is doing so right next to the word "is"
 
That's because your cout statement is outputting it.

cout << "\nThe day of the week " << "for "
<< month <<"/"<< day <<"/"<< year << " is" << DayofWeek<<endl;

if (DayofWeek == 0)
cout << "Sunday";
else if (DayofWeek == 1) //Nested If
cout << "Monday";
else if (DayofWeek == 2) //Nested If
cout << "Tuesday";
else if (DayofWeek == 3) //Nested If
cout << "Wednesday";
else if (DayofWeek == 4) //Nested If
cout << "Thursday";
else if (DayofWeek == 5) //Nested If
cout << "Friday";
else if (DayofWeek == 6) //Nested If
cout << "Saturday";
cout << endl;

Also, try using a switch statement instead of nested if's. It's a LOT cleaner to code, and easier to read.
 
/agree Crusty regarding the switch.
You might also wrap your if's in { }, even for single statements, as it improves maintainability and readability.
 
Back
Top