Post: [C++] Get day of the month of any date
11-02-2012, 02:39 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Well recently xd366 had a school project where he had to do this in assembly. Now I am lazy and decided to build the same application but in C++. I used CLI instead of win32 because you get windows class libraries, but you can use any. I just wanted to show you this project and release it. Feel free to post opinions or better fixes to it.


Image Example:
You must login or register to view this content.

    
#include "stdafx.h"
#include "Windows.h"
#include "stdio.h"

using namespace System;

String^ intToCommonDay( int x )
{
switch( x )
{
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesday";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
default: return "N/A";
}
}

int commonDayToInt( String^ x )
{
//have to use this b/c cannot use switch with char array in C++
//too lazy to use public enum so use if/then
x = x->ToLower();
if(x == "january")
return 1;
else if(x == "february")
return 2;
else if(x == "march")
return 3;
else if(x == "april")
return 4;
else if(x == "may")
return 5;
else if(x == "june")
return 6;
else if(x == "july")
return 7;
else if(x == "august")
return 8;
else if(x == "september")
return 9;
else if(x == "october")
return 10;
else if(x == "november")
return 11;
else if(x == "december")
return 12;
else return 0;
}

int main(array<System::String ^> ^args)
{
SetConsoleTitle(L"Date Project");

Console::WriteLine(L"Please enter a date. The program will find the day it is held on.\n\nValid Synax:\n\t*\t3/15/2012\n\t*\tMarch 15, 2012\n");

//get date + convert to numbers.
String^ date = Console::ReadLine();
array<String^>^ parsed;

if(date->Contains("/"))
parsed = date->Split('/'Winky Winky;
else if(date->Contains(","))
{
array<String^>^ da = date->Split(','Winky Winky;
array<String^>^ db = da[0]->Split(' 'Winky Winky;
parsed = gcnew array<String^>(3);
parsed[0] = Convert::ToString(commonDayToInt(db[0]));
parsed[1] = db[1];
parsed[2] = da[1]->Substring(1); //for the space ' '
}
else
{
Console::WriteLine(L"You have entered an invalid syntax.\nPress any key to quit.");
Console::ReadKey();
return 0;
}
int month = Convert::ToInt32(parsed[0]);
int day = Convert::ToInt32(parsed[1]);
int year = Convert::ToInt32(parsed[2]);

//do calculations
int a = (14-month)/12;
int y = year - a;
int m = month + (12*a) - 2;
int d = (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7;

//finish
Console::WriteLine(L"The day of the month is: " + intToCommonDay(d) );

//wait for user end
Console::ReadKey();
return 0;
}


Pastebin: You must login or register to view this content.

The following 2 users say thank you to Jakes625 for this useful post:

ICS Vortex, xePixTvx

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo