Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, fellow programmers.
I am programming about an age calculator.
It's simple:
1, get the input date 'today', check if its valid
2, get the input date 'birthday', check if it's valid and before 'today'
3, calculate the difference and output.
This is a assignment due tomorrow and I have written the most of it, just needs some debugging, mostly about pass by reference. I declared date1&date2 at main and I have no idea how to call the value of them if I want to calculate them at another function. I guess I should probably declare them at struct Date, but I am not sure.
I would really appreciate it if you guys could tell me what to do.
Code:
/************************************************************
*
* Project 4: How Old Are You Really?
*
* Author: xx
* Date: 6 May 2015
*
* This is a program designed to calculates the difference
* between two dates, which will be expressed in terms of
* years, months and days.
*
***********************************************************/
#include <bjarne/std_lib_facilities.h>
struct Date {
int month;
int day;
int year;
// Member functions
Date get_date();
Date get_birth_date();
bool is_valid_date(int year, int month, int day);
bool is_before(Date& date1, Date& date2);
Date calculate_age(Date& date1,Date& date2);
};
// Declaration
Date get_date();
Date get_birth_date();
bool is_valid_date(int year, int month, int day);
bool is_before(Date& date1, Date& date2);
Date calculate_age(Date& date1,Date& date2);
int main()
{
Date date1 = get_date();
Date date2 = get_birth_date();
Date get_date();
// Check if the date is correct
if (! is_valid_date(year, month, day))
error("Date is not valid.");
// Run how old or not?
char go_on;
cout << "Would you like to see how old you are (y/n)?\n";
cin >> go_on;
if (go_on=='n'){
cout << "You are so chicken!";
return 0;}
else if (go_on!='y')
error("Please enter y for yes or n for no.");
Date get_birth_date();
// Is birthday valid as well?
if (! is_valid_date(int year, int month, int day))
error("Date is not valid.");
// Is the birthday before 'today'?
if (!(is_before(Date& date1, Date& date2)))
error("Your birthday should not be later than today, terminator.");
// Calculate and give the answer.
Date calculate_age();
}
Date get_date()
{
Date date1;
cout << "Welcome to the age calculator!\n";
cout << "Please enter today's date (mm/dd/yyyy): ";
int m;
int d;
int y;
// To ignore the '/' during reading
char slash;
cin >> m;
date1.month = m;
cin >> slash;
cin >> d;
date1.day = d;
cin >> slash;
cin >> y;
date1.year = y;
cout << "Date entered was "<< date1.month << "/" << date1.day << "/" << date1.year <<"\n";
}
Date get_birth_date()
{
Date date2;
cout << "Please enter your birth date (mm/dd/yyyy): ";
int m;
int d;
int y;
// To ignore the '/' during reading
char slash;
cin >> m;
date2.month = m;
cin >> slash;
cin >> d;
date2.day = d;
cin >> slash;
cin >> y;
date2.year = y;
cout << "Your birthday is "<< date2.month << "/" << date2.day << "/" << date2.year <<"\n";
}
// Is date valid?
bool is_vaild_date(int year, int month, int day)
{
if (day<0)
return false;
if (month<1 || month>12)
return false;
// Check if the date exists or not considering there are have
// different days when month varies
int days_in_month=31;
switch (month){
case 2:
days_in_month=28;
break;
case 4: case 6: case 9: case 11:
days_in_month=30;
break;
}
if (days_in_month<day)
return false;
return true;
}
//Date check - is birthday before 'today'?
bool is_before(Date& date1, Date& date2)
{
if (Date& date1.year<Date& date2.year)
return false;
else if (Date& date1.month<Date& date2.month)
return false;
else if (Date& date1.day< Date& date2.day)
return false;
return true;
}
// Calculation
Date calculate_age(Date& date1,Date& date2)
{
Date date3;
date3.day = date1.day - date2.day;
date3.month = date1.month - date2.month;
date3.year = date1.year - date2.month;
if (date3.day<0){
date3.day = 30 + date3.day;
date3.month = date3.month - 1;
}
if (date3.month<0){
date3.month = 12 + date3.day;
date3.year = date3.year - 1;
}
cout <<"You are " << date3.year <<" years, "<< date3.month <<" months, and " << date3.day <<" days old.\n";
}
you need to switch on compiler warnings (-Wall) and you will see a lot of problems.
For example the functions get_date() and get_birth_date() do not return a Date.
As suggested, there are a ton of syntax errors here.
Structure of the program is important and it will help you to determine code block areas where brackets are missing
Using compiler error and warning reports to pre-diagnose will help you immensely here
In my book this is very bad code:
Code:
if (day<0)
return false;
if (month<1 || month>12)
return false;
No proper spacing
No indentations
No brackets. Yes I realize they are not required for one-line statements, but I feel that they should be used because when you're new you'll make errors. I myself always do use brackets
Similarly the line testing for month is relying on operator precedence and I do not agree with that either, I would instead use parentheses around each test term to ensure that the test matched my intentions
Also, I just "picked" something which happened to be on the screen while I was preparing my response here, I'm sure if I looked extensively at the entire body of code I'd have similar reservations.
In my book it's not fine to code "cutely" because you get into trouble. Especially when you're new.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.