Interview Questions on C C++

Home  |  About Us  |  Contact
View Questions Contribute Training Misc..

C++ program that takes an integer representing a day of the year and translates it to a string

Assuming that a year consists of 365 days, write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consisting of the month followed by the day of the month. The arrays for “days at end of month” and “month name” should be in the specification (header) file.
Add a loop in main so that multiple numbers can be input and “Q” to quit.

The program should do following:
 Check for valid input
 Use a separate *.h file for the DayOfYear class
 Declare and initialize the arrays in the *.h file of the class
 Design a Loop main to allow the user to use the program multiple time


Solution: (this solution has two files main.cpp & DaysOfYearClass.h)


//main.cpp 

#include <iostream>
#include <stdlib.h>
#include "DaysOfYearClass.h"

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	//Create an instance of the DaysOfYearClass
	DaysOfYearClass dayOfYearObj;
	
	//To hold the day
	int day;
	
	//temporary array to hold the user input to stor input & exit character 'Q'
	char input[10];
	
	//Display the purpose of the program
	cout<<"\n This program convert a number \n"<<"into a string representing the \n"<<"month and day.\n\n";
	
	//Variable to exit
	char choice ='n';
	
	while(choice != 'Q')
	{
	
	//Get the day as input from user
	cout<<"\n\n Enter a whle number 1 and 365 or 'Q' to quit : ";
	cin>>input;
	
	choice = input[0];
	
	day = atoi(input);
	
	if(day>0 && day<=365)
	{
		//Set the day
		dayOfYearObj.setDay(day);
		
		//Dispaly the object
		dayOfYearObj.print();
	     	
	}
	else
	{
		if(choice == 'Q')
		cout<<"\n thanks ";
		else
		cout<<"\n [Error : ********* Invalid Input *********] \n\n";
	}
	
	
    }

return 0;
}
//end of main.cpp



//DaysOfYearClass.h

#include<string.h>
#include<iostream>
using namespace std;
class DaysOfYearClass
{

private: int totalDays;
private: 
        struct monthAndDays{
        	int days;
        	char  monthName[20];
        	
		};
		struct monthAndDays obj[12];	
public:

 DaysOfYearClass()
{

	strcpy(obj[0].monthName,"January"); obj[0].days = 31;
	strcpy(obj[1].monthName,"February"); obj[1].days = 28;
	strcpy(obj[2].monthName,"March"); obj[2].days = 31;
	strcpy(obj[3].monthName,"April"); obj[3].days = 30;
	strcpy(obj[4].monthName,"May"); obj[4].days = 31;
	strcpy(obj[5].monthName,"June"); obj[5].days = 30;
	strcpy(obj[6].monthName,"July"); obj[6].days = 31;
	strcpy(obj[7].monthName,"August"); obj[7].days = 31;
	strcpy(obj[8].monthName,"September"); obj[8].days = 30;
	strcpy(obj[9].monthName,"October"); obj[9].days = 31;
	strcpy(obj[10].monthName,"November"); obj[10].days = 30;
	strcpy(obj[11].monthName,"December"); obj[11].days = 31;	
}
	
~DaysOfYearClass()
{
	
}

	void setDay(int dayValue)
	{
		this->totalDays = dayValue;
	}
		
	void print()
	{ 
	  char tempMonth[20]="January";
	  for(int i=0; i<12; i++){
	  	
	  	if(totalDays > obj[i].days )
	  	{
	  	    totalDays -= obj[i].days;
			strcpy(tempMonth, obj[i+1].monthName);	
		}
		else
		{
			break;
		}
	  }
	  cout<<"\n "<< tempMonth <<" "<<totalDays ;
	  
		
	}	
	
	
	
};
//end of DaysOfYearClass.h