Beginners Task #1

Online C++ programming contests.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo

Postby shiitake » Mon May 17, 2004 10:40 am

oh man! this is great. it was just what i was looking for.

i was literally coming to the board today to ask for an assignment like this. what a pleasant suprise!

ok.n. i've got to see if i can figure it out.
shiitake
 
Posts: 10
Joined: Wed May 12, 2004 7:18 am
Location: Dallas, Texas USA

Postby shiitake » Mon May 17, 2004 5:52 pm

alright... i think i've got it (at least it works for me :) )

Code: Select all
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
int DofY = 1;

   cout << "Please enter a number between 1-366: "<< endl;
   cin >> DofY;

   if (DofY < 31){
   cout << DofY << " January 2004" << endl;}
   else {
      if (DofY < 60){
      cout << (DofY - 31) << " February 2004" << endl;}
      else{
         if (DofY < 91){
            cout << (DofY - 60) << " March 2004" << endl;}
         else{
            if (DofY < 121){
               cout << (DofY - 91) << " April 2004" << endl;}
            else{
               if (DofY < 152){
                  cout << (DofY - 121) << " May 2004" << endl;}
               else {
                  if (DofY < 182){
                     cout << (DofY - 152) << " June 2004" << endl;}
                  else{
                     if (DofY < 213){
                        cout << (DofY - 182) << " July 2004" << endl;}
                     else{
                        if (DofY < 244){
                           cout << (DofY - 213) << " August 2004" << endl;}
                        else{
                           if (DofY < 274){
                              cout << (DofY - 244) << " September 2004" << endl;}
                           else{
                              if (DofY < 305){
                                 cout << (DofY - 274) << " October 2004" << endl;}
                              else{
                                 if (DofY < 335){
                                    cout << (DofY - 305) << " November 2004" << endl;}
                                 else{cout << (DofY - 335) << " December 2004" << endl;}
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
         
      }   system("PAUSE");   
  return 0;
       }
shiitake
 
Posts: 10
Joined: Wed May 12, 2004 7:18 am
Location: Dallas, Texas USA

Postby shiitake » Mon May 17, 2004 5:55 pm

ok. i just realized that mine doesn't include st, nd, rd, th after the numbers.

oh well.

this is literally my 1 program ever ;)
shiitake
 
Posts: 10
Joined: Wed May 12, 2004 7:18 am
Location: Dallas, Texas USA

Postby Esran » Mon May 17, 2004 9:02 pm

It's really good for your first program! Mine was the standard "Hello World" program. One suggestion though, use #include <cstdlib> instead of #include <stdlib.h>. Other than that, it's wonderful for someone's first program!
User avatar
Esran
 
Posts: 297
Joined: Fri Apr 09, 2004 4:42 pm

Postby Guest » Tue May 18, 2004 8:54 am

I made a windows version if any wants to see. I could just post it but it off topic so I just did not to just post it.
Guest
 

Postby tomcant » Tue May 18, 2004 8:55 am

If its not too long, which it probably isnt... post it. I think I'm going to try that too :)
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Guest » Tue May 18, 2004 10:14 am

Well here it is not sure how much you will need to compile it. This is what I have.

MainFrame.cpp
Code: Select all
//MainFrame.cpp
#include "MainFrame.h"
#include "MyDialog.h"
#include <Afx.h>

CMyWinApp  MyApplication;

BOOL CMyWinApp::InitInstance()
{
  CMainFrame* pFrame = new CMainFrame;
  m_pMainWnd = pFrame;     

  pFrame->ShowWindow(SW_SHOW);
  pFrame->UpdateWindow();

  return TRUE;
}


BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  ON_COMMAND (ID_SHOWDIALOG, ShowDialog)
END_MESSAGE_MAP()

CMainFrame::CMainFrame()
{
  Create ( NULL,"Calendar",
            WS_OVERLAPPEDWINDOW &~WS_THICKFRAME &~WS_MAXIMIZEBOX,
             CRect(0,0,300,200),
              NULL,
               MAKEINTRESOURCE (IDR_MENU1)
          );
  mDay  = 6;
}

void CMainFrame::ShowDialog()
{
  CClientDC dc (this);

  CMyDialog Dlg(this);
Invalidate();
  Dlg.day  = mDay;
                       
  if (Dlg.DoModal() == IDOK)
  {
     mDay  = Dlg.day;
     CString dayname[] = {"Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday"};
     CString month[] = {"January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"};
     const int daystosubtract[]= { 0, 31, 60, 91, 121, 152,
        182, 213, 244, 274, 305, 335 };//what number of days to subtract

     int i = 0;
     const int div = 32;

     i = mDay / div; // which number from arry to use for subtraction
     daystosubtract[i];

     int daysofmonth = 0;
     daysofmonth = mDay - daystosubtract[i]; // which day of the month

     const int daysdiveder = 7;
     int prefix = 0;
     prefix = mDay / daysdiveder;
     int g = 0;
     g = prefix * daysdiveder;
     int days = 0;
     days = mDay - g;

     int whichprefix = 0;
     if ( (daysofmonth >= 4 && daysofmonth <= 20) || (daysofmonth >= 24 && daysofmonth <= 30))
     {whichprefix = 5;}
     else
        whichprefix = daysofmonth % 10; //which prefix

     switch (whichprefix)
     {   
     case 1:
        {
           CString s;
           s.Format ("Day: %s, %d  st  %s   2004", dayname[days], daysofmonth, month[i]);
           dc.TextOut (10,10,s);
           break;
        }
     case 2:
        {
           CString s;
           s.Format ("Day: %s, %d  nd  %s   2004", dayname[days], daysofmonth, month[i]);
           dc.TextOut (10,10,s);
           break;
        }
     case 3:

        {
           CString s;
           s.Format ("Day: %s, %d  rd  %s   2004", dayname[days], daysofmonth, month[i]);
           dc.TextOut (10,10,s);
           break;
        }
     default :
        {
           CString s;
           s.Format ("Day: %s, %d  th  %s   2004", dayname[days], daysofmonth, month[i]);
           dc.TextOut (10,10,s);
           break;
        }
     }   
  }
  else
    dc.TextOut (10,10,"You press CANCEL");

}


MainFrame.h
Code: Select all
//MainFrame.h
#include <afxwin.h>

class CMyWinApp : public CWinApp
{
public:
  virtual BOOL InitInstance();
};

class CMainFrame : public CFrameWnd
{
private:
  int     mDay;
public:
  CMainFrame();

  afx_msg void ShowDialog();
  DECLARE_MESSAGE_MAP()
};


MyDialog.cpp
Code: Select all
//MyDialog.cpp
#include "MyDialog.h"


CMyDialog::CMyDialog(CWnd* pParentWnd)
: CDialog (IDD_DIALOG1, pParentWnd)
{  }                                 
                                   
CMyDialog::~CMyDialog() {  }

BOOL CMyDialog::OnInitDialog()   
{                                         
   
  SetDlgItemInt (IDC_DAY, day);       

  return TRUE;                   
}

void CMyDialog::OnOK()           
{                                 

  day = GetDlgItemInt (IDC_DAY); 
                                 
  CDialog::OnOK(); 
}


MyDialog.h
Code: Select all
//MyDialog.h
#include <afxwin.h>
#include "resource.h"

class CMyDialog : public CDialog 
{
public: 
  int      day;

   CMyDialog(CWnd* pParentWnd);
   virtual ~CMyDialog();

  virtual BOOL OnInitDialog();
  virtual void OnOK();
};


I assume you will need resource files to.

MyResScript.rc
Code: Select all
//MyResScript.rc
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDR_MENU1 MENU
BEGIN
    MENUITEM "Enter Number",                ID_SHOWDIALOG
END


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DIALOG1 DIALOGEX 0, 0, 119, 48
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Enter information"
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,77,7,35,14
    PUSHBUTTON      "Cancel",IDCANCEL,73,27,39,14
    RTEXT           "Day",IDC_STATIC,7,28,21,13,SS_CENTERIMAGE
    EDITTEXT        IDC_DAY,35,29,23,12,ES_AUTOHSCROLL
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
    IDD_DIALOG1, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 112
        TOPMARGIN, 7
        BOTTOMMARGIN, 41
    END
END
#endif    // APSTUDIO_INVOKED

#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED


resource.h
Code: Select all
//resource.h
//{{NO_DEPENDENCIES}}

// Microsoft Visual C++ generated include file.
// Used by MyResScript.rc
//
#define IDR_MENU1                       101
#define IDD_DIALOG1                     102
#define IDC_DAY                         1001
#define IDC_IMAGELISTCTRL1              1002
#define ID_SHOWDIALOG                   40001

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        103
#define _APS_NEXT_COMMAND_VALUE         40002
#define _APS_NEXT_CONTROL_VALUE         1003
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif


Hope that is enough to compile it and not to much. This was created in Microsoft Visual Studio .NET 2003 and is a MFC app if that makes any difference.
Guest
 

Postby tomcant » Tue May 18, 2004 10:32 am

Nice one :D I'll post my version when I get round to writing it :)
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Guest » Tue May 18, 2004 10:47 am

I guess it compiled fine for you.
Guest
 

Well........

Postby Sin » Tue May 18, 2004 11:19 am

It compiles fine for me........ 8)
Sin
 
Posts: 1570
Joined: Tue Oct 07, 2003 10:16 am

Postby tomcant » Tue May 18, 2004 12:05 pm

Yep, compiles fine :)
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Guest » Tue May 18, 2004 12:40 pm

That is good I was just wondering because I have only been working with MFC for about month or so and was unsure which files to post for someone else to compile it.
Thanks Christopher and tomcant.
Guest
 

Help

Postby darkmaster » Tue May 18, 2004 4:15 pm

Hello

I need some help, my code produces one incorrect output and a correct one, from days 61 to 365 the other days it produces a correct output.

Code: Select all
#include <iostream>
using namespace std;

void suffix(int nday)
{
   switch(nday)
   {
      case ((1) | (21) | (31)) : cout << "st";
                 break;
      case ((2) | (22)) : cout << "nd";
                 break;
      case ((3) | (23)) : cout << "rd";
                 break;
      default  : cout << "th";               
   }
}

void date(int dayn)   
{
    int jan = 31;
   int feb = 60;
   int mar = 91;
   int apr = 121;
   int may = 152;
   int jun = 182;         
   int jul = 213;         
   int aug = 244;
   int sep = 274;
   int oct = 305;
   int nov = 335;
   int dec = 366;

   int febd = dayn - jan;
   int mard = dayn - feb;
   int aprd = dayn - mar;
   int mayd = dayn - apr;
   int jund = dayn - may;   
   int juld = dayn - jun; 
   int augd = dayn - jul;
   int sepd = dayn - aug;
   int octd = dayn - sep;
   int novd = dayn - oct;
   int decd = dayn - nov;

if (dayn <= jan) //January
   {
   cout << dayn;
   suffix(dayn);
   cout << " January 2004 ";
}

if ((dayn > jan) && (dayn < mar)) //February
   {
   cout << (febd);
   suffix(febd);
   cout << " February 2004 ";
   }

if ((dayn > feb) && (dayn < apr)) //March
   {
   cout << (mard);
   suffix(mard);
   cout << " March 2004 ";
   }

if ((dayn > mar) && (dayn < may)) //April
   {
   cout << (aprd);
   suffix(aprd);
   cout << " April 2004 ";
   }

if ((dayn > apr) && (dayn < jun)) //May
   {
   cout << (mayd);
   suffix(mayd);
   cout << " May 2004 ";
   }

if ((dayn > may) && (dayn < jul)) //June
   {
   cout << (jund);
   suffix(jund);
   cout << " June 2004 ";
   }

if ((dayn > jun) && (dayn < aug)) //July
   {
   cout << (juld);
   suffix(juld);
   cout << " July 2004 ";
   }

if ((dayn > jul) && (dayn < sep)) //August
   {
   cout << (augd);
   suffix(augd);
   cout << " August 2004 ";
   }

if ((dayn > aug) && (dayn < nov)) //September
   {
   cout << (sepd);
   suffix(sepd);
   cout << " September 2004 ";
   }

if ((dayn > sep) && (dayn < nov)) //October
   {
   cout << (octd);
   suffix(octd);
   cout << " October 2004 ";
   }

if ((dayn > oct) && (dayn < dec)) //November
   {
   cout << (novd);
   suffix(novd);
   cout << " November 2004 ";
   }

if ((dayn > nov) && (dayn <= 366)) //December
   {
   cout << (decd);
   suffix(decd);
   cout << " December 2004 ";
   }

}

int main()
{
   int day;
   cout << "Enter a number between 1 and 366: ";

cin >> day;
   if ((day >= 1) && (day <= 366))
   {
   
   date(day);
   
   cin.ignore();
   cin.get();
   }

   else {
   
      cout << "Please enter a valid value";
      cin.ignore();
      cin.get();
   }
   return(0);
}
User avatar
darkmaster
 
Posts: 27
Joined: Mon Mar 29, 2004 6:11 pm

Postby Guest » Tue May 18, 2004 4:22 pm

go home wrote:That is obviously Dave Sinkula so go back to http://cboard.cprogramming.com/index.php? and stay there.
Last edited by Guest on Thu Jan 06, 2005 1:21 am, edited 1 time in total.
Guest
 

Postby Guest » Tue May 18, 2004 4:28 pm

go home wrote:That is obviously Dave Sinkula so go back to http://cboard.cprogramming.com/index.php? and stay there.
Last edited by Guest on Thu Jan 06, 2005 1:21 am, edited 2 times in total.
Guest
 

PreviousNext

Return to Contests

Who is online

Users browsing this forum: No registered users and 0 guests