i have to write a function (dayNumber) that returns the day number in a year for a date that is provided as input data. i cant use const, struct, or loops, it has to be very basic stuff. i have something written but i can figure out how to get it to run or what im missing, any clues will help, thanks!!

#include <iostream>
using namespace std;
bool leapYear(int);
int dayNumber(int, int, int);
int monthtable;


  int monthtable d[12] = 
{ 

	{ 0 }, { 31 }, { 59 }, { 90 }, { 120 },
	{ 151 }, { 181 }, { 212 }, { 243 }, { 273 },
	{ 304 }, { 334 }
};


int getDayNumber(int month)
{
     return d[month - 1].monthtable;
}

int main ()                                                         //begin main
{
int month;
int day;
int year;
int leap;
int number;

	                                                                    //input
    cout << "What month? \n";
    cin  >> month;
    cout << "What day? \n";
    cin  >> day;
    cout << "What four digit year? \n";
    cin  >> year;

	                                                                 //calculate
	leap = leapYear(year);                                                
	number = dayNumber(month, day, leap);

	                                                                   //output
	cout << "The day number for the date you entered: " << month 
         << "/" << day << "/" << year << " is: " << number << endl; 
	
	                                                     //display true or false
	if (leap)
		cout << "Year entered is a leap year: true" << endl;
	else
		cout << "Year entered is a leap year: false" << endl;
		                                                                  
	system ("pause");
         return 0;
}

                                                          //bool leapYear begin
bool leapYear(int year)
{
	return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
}                                                                     //end bool

                                                          //calculate dayNumber
int dayNumber(int month, int day, int leap)
{
	return day + getDayNumber(month) + (month > 2 ? leap : 0);
    
}

Recommended Answers

All 3 Replies

What are you using to compile/debug this program eg eclipse, visual Studio ?

I think that like this it works, you have done it entirely by yourself

Though the easiest and I think more correct way to do it would be using struct tm

cheers

#include <iostream>
using namespace std;
bool leapYear(int);
int dayNumber(int, int, int);

typedef int monthtable;


monthtable d[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

int getDayNumber(int month)
{
  return d[month - 1];
}

int main ()                                                         //begin main
{
int month;
int day;
int year;
int leap;
int number;

	                                                                    //input
    cout << "What month? \n";
    cin  >> month;
    cout << "What day? \n";
    cin  >> day;
    cout << "What four digit year? \n";
    cin  >> year;

	                                                                 //calculate
	leap = leapYear(year);                                                
	number = dayNumber(month, day, leap);

	                                                                   //output
	cout << "The day number for the date you entered: " << month 
         << "/" << day << "/" << year << " is: " << number << endl; 
	
	                                                     //display true or false
	if (leap)
		cout << "Year entered is a leap year: true" << endl;
	else
		cout << "Year entered is a leap year: false" << endl;
		                                                                  
         return 0;

	 system("pause");
}

                                                          //bool leapYear begin
bool leapYear(int year)
{
	return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
}                                                                     //end bool

                                                          //calculate dayNumber
int dayNumber(int month, int day, int leap)
{
	return day + getDayNumber(month) + (month > 2 ? leap : 0);
    
}

im using dev c++ bloodshed, were only in like 4th chapter in the book so were using very basics

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.