d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Looking For Feedback(c++) > Turning In An Assignment
12Next
Add Reply New Topic New Poll
Member
Posts: 976
Joined: Apr 5 2010
Gold: 1,080.50
Oct 20 2013 08:41am
Main:
Code
#include <iostream>
#include <string>
#include "Date.h"
#include "Date.cpp"

using namespace std;

//function prototypes
void dayCheck(int&, const int&, const int&);
void monthCheck(int&);
void yearCheck(int&);

int main()
{
int day,
month,
year;

cout << "Enter the month(Numerical): ";
cin >> month;
monthCheck(month); //validates month input

cout << "Enter the year: ";
cin >> year;
yearCheck(year); //validates year input

cout << "Enter the day: ";
cin >> day;
dayCheck(day, month, year); //validates day input

Date myDate(day, month, year); //creates myDate object with a constructor arguments

myDate.showShortDate(); //will print a short date
myDate.showLongDate(); //will print a long date
myDate.showEuroDate(); //will print a euro date

return 0;
}

//dayCheck will vaildate the day to be within acceptable ranges according to month
void dayCheck(int& dayRef, const int& monthRef, const int& yearRef)
{
enum monthNameList { January = 1, February, March, April, May, June, July, August, September, October, November, December };

if(monthRef == April || monthRef == June || monthRef == September || monthRef == November)
{
while(dayRef > 30 || dayRef < 1)
{
cout << "The day can only be between 1 and 30: ";
cin >> dayRef;
}
}

else if(monthRef == February)
{
if(yearRef % 4 == 0)
{
while(dayRef > 29 || dayRef < 1)
{
cout << "The day can only be between 1 and 29: ";
cin >> dayRef;
}
}

else if(yearRef % 4 != 0)
{
while(dayRef > 28 || dayRef < 1)
{
cout << "The day can only be between 1 and 28: ";
cin >> dayRef;
}
}
}

else if(monthRef == January || monthRef == March || monthRef == May || monthRef == July || monthRef == August || monthRef == September || monthRef == October || monthRef == December)
{
while(dayRef > 31 || dayRef < 1)
{
cout << "The day can only be between 1 and 31: ";
cin >> dayRef;
}
}

else
cout << "There was an error at function dayCheck() in main.cpp";
}

//monthCheck will validate the month to be within 1 and 12
void monthCheck(int& monthRef)
{
while(monthRef > 12 || monthRef < 1)
{
cout << "The month can only be between 1 and 12: ";
cin >> monthRef;
}
}

//yearCheck will validate the year to be within 1950 and 2020
void yearCheck(int& yearRef)
{
while(yearRef < 1950 || yearRef > 2020)
{
cout << "The year can only be between 1950 and 2020: ";
cin >> yearRef;
}
}


Date.h:
Code
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
class Date
{
private:
int day,
month,
year,
shortYear;

public:
Date(int, int, int);

int getDay();
int getMonth();
int getYear();

int makeShortYear();

void makeMonthName(const int&);

void showShortDate();
void showLongDate();
void showEuroDate();


};
#endif // DATE_H_INCLUDED


Code
#include "Date.h"
#include <iostream>
#include <string>

//constructor sets the variables according the values entered in main
Date::Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}

//returns the day
int Date::getDay()
{
return day;
}

//returns the month
int Date::getMonth()
{
return month;
}

//returns the year
int Date::getYear()
{
return year;
}

//prints the short date
void Date::showShortDate()
{
std::cout << getMonth() << "/" << getDay() << "/" << makeShortYear() << std::endl;
}

//prints the long date
void Date::showLongDate()
{
makeMonthName(month); std::cout << " " << getDay() << ", " << getYear() << std::endl;
}

//prints the short euro date
void Date::showEuroDate()
{
std::cout << getDay() << " "; makeMonthName(month); std::cout << " " << getYear() << std::endl;
}

//prints the month according to the user enter month value
void Date::makeMonthName(const int& monthValue)
{
enum monthNameList { January = 1, February, March, April, May, June, July, August, September, October, November, December };

if(monthValue == January)
std::cout << "January";

else if(monthValue == February)
std::cout << "February";

else if(monthValue == March)
std::cout << "March";

else if(monthValue == April)
std::cout << "April";

else if(monthValue == May)
std::cout << "May";

else if(monthValue == June)
std::cout << "June";

else if(monthValue == July)
std::cout << "July";

else if(monthValue == August)
std::cout << "August";

else if(monthValue == September)
std::cout << "September";

else if(monthValue == October)
std::cout << "October";

else if(monthValue == November)
std::cout << "November";

else if(monthValue == December)
std::cout << "December";

else
std::cout << "There was an error at function makeMonthName() in Date.cpp";
}

//creates the two digit "short year" for the short date function
int Date::makeShortYear()
{
int shortYearTemp = getYear();

if(shortYearTemp > 1999)
return shortYearTemp - 2000;

else if (shortYearTemp < 2000)
return shortYearTemp - 1900;

else
std::cout << "There was an error at function makeShortYear() in Date.cpp";
}


My fourth week ever learning to program in my computer science class and I was just wondering if there was anything I could improve on from
coding conventions to improving how efficient/long my code is. Thank you :3
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Oct 20 2013 10:57am
Make a string array that holds your months and access those with the monthValue index instead of that long if else you're using
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Oct 20 2013 01:29pm
use unsigned short instead int since days / months will never be negative or have a value higher than 65,535

in your makemonthname function I'd recommend using a switch statement to handle your monthValue conditions since it's faster.

also im not sure why you're passing variables to your functions by reference (&). You only want to pass by reference if you're going to be change the value of the variable

This post was edited by SelfTaught on Oct 20 2013 01:30pm
Member
Posts: 976
Joined: Apr 5 2010
Gold: 1,080.50
Oct 20 2013 04:31pm
I have since changed the code:

Main:
Code
//Program Name: Date
//Program Description: Will print the date in three fashions
//Programmer Name: Joseph Cordero
//Date: October 20, 2013
#include <iostream>
#include <string>
#include "Date.h"

//function prototypes
bool dayCheck(uint16_t& day, uint16_t month, unsigned short year, bool&, bool&, bool&, bool&);
bool monthCheck(uint16_t month);
bool yearCheck(unsigned short year);

int main()
{
bool isLeapYear,
nonLeapYear,
shortMonth,
longMonth;

uint16_t day,
month;

unsigned short year;

std::cout << "Enter the month(Numerical): ";
std::cin >> month;
while(monthCheck(month)) //will return true if the month does not validate
{
std::cout << "The month may only be between 1 and 12: ";
std::cin >> month;
}

std::cout << "Enter the year: ";
std::cin >> year;
while(yearCheck(year)) //will return true if the year does not validate
{
std::cout << "The year may only be between 1950 and 2020: ";
std::cin >> year;
}

std::cout << "Enter the day: ";
std::cin >> day;
//checks to see if any of the validations were set off to correct user input
while(dayCheck(day, month, year, isLeapYear, nonLeapYear, shortMonth, longMonth))
{
if(nonLeapYear)
{
std::cout << "The day may only be between 1 and 28: ";
std::cin >> day;
}

else if(isLeapYear)
{
std::cout << "The day may only be between 1 and 29: ";
std::cin >> day;
}

else if(shortMonth)
{
std::cout << "The day may only be between 1 and 30: ";
std::cin >> day;
}

else if(longMonth)
{
std::cout << "The day may only be between 1 and 31: ";
std::cin >> day;
}
}

Date myDate(day, month, year); //creates myDate object with a constructor arguments

myDate.showShortDate(); //will print a short date
myDate.showLongDate(); //will print a long date
myDate.showEuroDate(); //will print a euro date

return 0;
}

//dayCheck will vaildate the day according to acceptable ranges
bool dayCheck(uint16_t& day, uint16_t month, unsigned short year,
bool& isLeapYearRef, bool& nonLeapYearRef, bool& shortYearRef, bool& longYearRef)
{
enum monthNameList { January = 1, February, March, April, May, June, July, August, September, October, November, December };

isLeapYearRef = nonLeapYearRef = shortYearRef = longYearRef = false;

if(month == April || month == June || month == September || month == November)
{
if(day > 30 || day < 1)
return shortYearRef = true;
else
return shortYearRef = false;
}

else if(month == February)
{
if(year % 4 == 0)
{
if(day > 29 || day < 1)
return isLeapYearRef = true;
else
return isLeapYearRef = false;
}

else
{
if(day > 28 || day < 1)
return nonLeapYearRef = true;
else
return nonLeapYearRef = false;
}
}

else if(month == January || month == March || month == May || month == July || month == August || month == September || month == October || month == December)
{
if(day > 31 || day < 1)
return longYearRef = true;
else
return longYearRef = false;
}
}

//monthCheck will validate the month to be within 1 and 12
bool monthCheck(uint16_t month)
{
bool monthBool = false;

if(month > 12 || month < 1)
monthBool = true;

return monthBool;
}

//yearCheck will validate the year to be within 1950 and 2020
bool yearCheck(unsigned short year)
{
bool yearBool = false;

if(year < 1950 || year > 2020)
yearBool = true;

return yearBool;
}


Date.h
Code
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
class Date
{
private:
uint16_t _day,
_month,
_shortYear;

unsigned short _year;

public:
Date(uint16_t day, uint16_t month, unsigned short year);

const uint16_t getDay();
const uint16_t getMonth();
const unsigned short getYear();

int makeShortYear();
void makeMonthName(uint16_t month);

void showShortDate();
void showLongDate();
void showEuroDate();
};
#endif // DATE_H_INCLUDED


Date.cpp
Code
#include <iostream>
#include "Date.h"

//constructor sets the variables according the values entered in main
Date::Date(uint16_t day, uint16_t month, unsigned short year)
{
_day = day;
_month = month;
_year = year;
}

//returns the day
const uint16_t Date::getDay()
{
return _day;
}

//returns the month
const uint16_t Date::getMonth()
{
return _month;
}

//returns the year
const unsigned short Date::getYear()
{
return _year;
}

//prints the short date
void Date::showShortDate()
{
std::cout << getMonth() << "/" << getDay() << "/" << makeShortYear() << std::endl;
}

//prints the long date
void Date::showLongDate()
{
makeMonthName(_month); std::cout << " " << getDay() << ", " << getYear() << std::endl;
}

//prints the short euro date
void Date::showEuroDate()
{
std::cout << getDay() << " "; makeMonthName(_month); std::cout << " " << getYear() << std::endl;
}

//prints the month according to the user enter month value
void Date::makeMonthName(uint16_t monthValue)
{
switch(monthValue)
{
case 1:
std::cout << "January";
break;

case 2:
std::cout << "February";
break;

case 3:
std::cout << "March";
break;

case 4:
std::cout << "April";
break;

case 5:
std::cout << "May";
break;

case 6:
std::cout << "June";
break;

case 7:
std::cout << "July";
break;
case 8:
std::cout << "August";
break;

case 9:
std::cout << "September";
break;

case 10:
std::cout << "October";
break;

case 11:
std::cout << "November";
break;

case 12:
std::cout << "December";
break;
}
}

//creates the two digit "short year" for the short date function
int Date::makeShortYear()
{
int shortYearTemp = getYear();

if(shortYearTemp > 1999)
return shortYearTemp - 2000;

else if (shortYearTemp < 2000)
return shortYearTemp - 1900;
}


This post was edited by cssa on Oct 20 2013 04:42pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Oct 20 2013 10:22pm
Getting better but it looks like didn't change the return type of Date::makeShortYear(). Its prototype type is declared as unsigned short but the function type is int.

Also i think you meant to specify month and not year here:

bool dayCheck(uint16_t& day, uint16_t month, unsigned short year, bool& isLeapYearRef, bool& nonLeapYearRef, bool& shortYearRef, bool& longYearRef)

This post was edited by SelfTaught on Oct 20 2013 10:51pm
Member
Posts: 976
Joined: Apr 5 2010
Gold: 1,080.50
Oct 21 2013 02:33pm
Quote (SelfTaught @ Oct 20 2013 09:22pm)
Getting better but it looks like didn't change the return type of Date::makeShortYear(). Its prototype type is declared as unsigned short but the function type is int.

Also i think you meant to specify month and not year here:

bool dayCheck(uint16_t& day, uint16_t month, unsigned short year, bool& isLeapYearRef, bool& nonLeapYearRef, bool& shortYearRef, bool& longYearRef)


Yeah I could change the bool names, but also Date::makeShortYear is prototyped as an int along with declared as an int. Maybe you are seeing the getYear() prototype?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Oct 21 2013 03:41pm
Quote (cssa @ Oct 21 2013 12:33pm)
Yeah I could change the bool names, but also Date::makeShortYear is prototyped as an int along with declared as an int. Maybe you are seeing the getYear() prototype?


Probably.

For consistency I'd change _year's data type. It's unsigned short which is pretty much the same as uint16_t, and you're declaring everything else that way.

Also, one thing I like to do is make my constructors inline if possible. Inline functions are faster and will increase the performance of your programs.

Explanation here: http://stackoverflow.com/questions/145838/benefits-of-inline-functions-in-c

i.e

Date.h

Code
class Date
{
uint16_t m_Day, m_Month, m_Year;

public:
Date(uint16_t day, uint16_t month, uint16_t year): m_Day( day ), m_Month( month ), m_Year( year ) { };
};


Even though it's declared as inline, because it's in a header file, that's how most compilers will interpret it.

This post was edited by SelfTaught on Oct 21 2013 04:04pm
Member
Posts: 976
Joined: Apr 5 2010
Gold: 1,080.50
Oct 21 2013 04:16pm
Lol.. I've been programming all of a couple weeks and someone told me to use the uint16_t and I never knew that it was a "16 bit" integer which is the same as short :3 Thanks for that, also, why the m_var convention instead of just _var for private variables?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Oct 21 2013 04:20pm
Quote (cssa @ Oct 21 2013 02:16pm)
Lol.. I've been programming all of a couple weeks and someone told me to use the uint16_t and I never knew that it was a "16 bit" integer which is the same as short :3 Thanks for that, also, why the m_var convention instead of just _var for private variables?


Np lol. It's good practice to prefix your variables. I prefix class member variables with m_ whether they're public or private.

This post was edited by SelfTaught on Oct 21 2013 04:24pm
Member
Posts: 976
Joined: Apr 5 2010
Gold: 1,080.50
Oct 23 2013 04:05am
Alright! Finished code for the most part. Lay it on me.


Main:
Code

#include <iostream>
#include "Date.h"

bool getMonthLength(int month, int year, bool&, bool&, bool&);
bool inputValidation(int minInputRange, int maxInputRange, int& userInput);
bool checkDays(int day, bool isLeapYear, bool shortMonth, bool longMonth);

/***************************************************
Gathers user input and will display the three
dates upon validation of input

****************************************************/
int main()
{
bool isLeapYear,
shortMonth,
longMonth;

int day,
month,
year;

const int MAX_YEAR = 2020,
MIN_YEAR = 1950,
MAX_MONTH = 12,
MIN_MONTH = 1,
MIN_MONTH_LENGTH = 1,
MAX_NON_LEAP_MONTH = 28,
MAX_LEAP_MONTH = 29,
MAX_SHORT_MONTH = 30,
MAX_LONG_MONTH = 31;

std::cout << "Enter the month(Numerical): ";
std::cin >> month;
while(inputValidation(MIN_MONTH, MAX_MONTH, month))
std::cin >> month;


std::cout << "Enter the year: ";
std::cin >> year;
while(inputValidation(MIN_YEAR, MAX_YEAR, year))
std::cin >> year;

//Determines month and leap year
getMonthLength(month, year, isLeapYear, shortMonth, longMonth);

std::cout << "Enter the day: ";
std::cin >> day;

//Determines if day input is valid based on the given range
if(!isLeapYear)
while(inputValidation(MIN_MONTH_LENGTH, MAX_NON_LEAP_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_NON_LEAP_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}


else if(isLeapYear)
while(inputValidation(MIN_MONTH_LENGTH, MAX_LEAP_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_LEAP_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}

else if(shortMonth)
while(inputValidation(MIN_MONTH_LENGTH, MAX_SHORT_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_SHORT_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}

else if(longMonth)
while(inputValidation(MIN_MONTH_LENGTH, MAX_LONG_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_LONG_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}

Date myDate(day, month, year); //creates myDate object

myDate.showShortDate(); //print short date
myDate.showLongDate(); //print long date
myDate.showEuroDate(); //print euro date

/*
Date noArgDate; //creates noArgDate object

noArgDate.showShortDate(); //print short date
noArgDate.showLongDate(); //print long date
noArgDate.showEuroDate(); //print euro date
*/

return 0;
}

/***************************************************
Sets a bool based on which month it is, and
another bool based on whether a leap year has
occurred
****************************************************/
bool getMonthLength(int month, int year, bool& isLeapYear, bool& shortMonth, bool& longMonth)
{
enum monthNameList { January = 1, February, March, April, May, June, July, August, September, October, November, December };

isLeapYear = shortMonth = longMonth = false;

//Determines leap year
if((year % 4) == 0)
{
if((year % 100) != 0)
return isLeapYear = true;
else if(((year % 100) == 0) && ((year % 400) == 0))
return isLeapYear = true;
else if (((year % 100) == 0) && ((year % 400) != 0))
return isLeapYear;
}

//Determines month
if(month == April || month == June || month == September || month == November)
return shortMonth = true;

else if(month == February)
{
if(isLeapYear)
return isLeapYear = true;
else
return isLeapYear;
}

else if(month == January || month == March || month == May || month == July || month == August || month == September || month == October || month == December)
return longMonth = true;
}

/***************************************************
Validates the user's input based on the min/max
arguments sent by the function call

****************************************************/
bool inputValidation(int minInputRange, int maxInputRange, int& userInput)
{
if(userInput > maxInputRange || userInput < minInputRange)
return true;
else
return false;
}



Date.h:
Code
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
class Date
{
private:
int m_day,
m_month,
m_shortYear,
m_year;

public:
Date()
{
m_day = 1;
m_month = 1;
m_year = 2001;
}

Date(int day, int month, int year);

const int getDay();
const int getMonth();
const int getYear();

int makeShortYear();
void makeMonthName(int month);

void showShortDate();
void showLongDate();
void showEuroDate();
};
#endif // DATE_H_INCLUDED


Date.cpp:
Code
#include <iostream>
#include "Date.h"

/***************************************************
Constructor initalizes private member variables to
their "twins" inside main which have been validated

****************************************************/
Date::Date(int day, int month, int year)
{
m_day = day;
m_month = month;
m_year = year;
}

/***************************************************
Will return private member variable m_day


****************************************************/
const int Date::getDay()
{
return m_day;
}

/***************************************************
Will return private member variable m_month


****************************************************/
const int Date::getMonth()
{
return m_month;
}

/***************************************************
Will return private member variable m_year


****************************************************/
const int Date::getYear()
{
return m_year;
}

/***************************************************
Prints the short version of a date: 1/1/01
Will work only for dates greater than 1899 and less
than 2100
****************************************************/
void Date::showShortDate()
{
std::cout << getMonth() << "/" << getDay() << "/";

if(makeShortYear() == 0) //in case of year 2000, will add another 0 to display "00"
std::cout << "0" << makeShortYear() << std::endl;
else
std::cout << makeShortYear() << std::endl;
}

/***************************************************
Prints the long version of a date: January 1, 2001


****************************************************/
void Date::showLongDate()
{
makeMonthName(m_month);
std::cout << " " << getDay() << ", " << getYear() << std::endl;
}

/***************************************************
Prints the euro version of a date: 1 January 2001


****************************************************/
void Date::showEuroDate()
{
std::cout << getDay() << " ";
makeMonthName(m_month);
std::cout << " " << getYear() << std::endl;
}

/***************************************************
Prints the name of the month based on the value
entered by the user that was validated in main

****************************************************/
void Date::makeMonthName(int monthValue)
{
switch(monthValue)
{
case 1:
std::cout << "January";
break;

case 2:
std::cout << "February";
break;

case 3:
std::cout << "March";
break;

case 4:
std::cout << "April";
break;

case 5:
std::cout << "May";
break;

case 6:
std::cout << "June";
break;

case 7:
std::cout << "July";
break;
case 8:
std::cout << "August";
break;

case 9:
std::cout << "September";
break;

case 10:
std::cout << "October";
break;

case 11:
std::cout << "November";
break;

case 12:
std::cout << "December";
break;
}
}

/***************************************************
Create the two digit version of the year for
the short version of date. Will only work for values
1900-2099
****************************************************/
int Date::makeShortYear()
{
int shortYearTemp = getYear();

if(shortYearTemp > 1999)
return shortYearTemp - 2000;

else if (shortYearTemp < 2000)
return shortYearTemp - 1900;
}


This post was edited by cssa on Oct 23 2013 04:18am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll