Calculate age in seconds - strange result - c++

I'm new in programming and new in here.
Sorry for stupid question but i have problem with result in my "calculate your age in seconds" code. It gives me weird result like 6.17725e+10 or -6.17414e+10.
Program isn't finished yet but everything except results looks fine (i don't get any error.
Sorry again and I hope for your understanding:)
#include <iostream>
using namespace std;
void title()
{
cout << "Age Calculator" << endl << endl;
}
int byear()
{
cout << "Enter your birth year: ";
int by;
cin >> by;
return by;
}
int bmonth()
{
cout << "Enter your birth month: ";
int bm;
cin >> bm;
return bm;
}
int bday()
{
cout << "Enter your birth day: ";
int bd;
cin >> bd;
return bd;
}
int cyear()
{
int cy;
cout << "Enter current year ";
cin >> cy;
return cy;
}
int cmonth()
{
cout << "Enter current month: ";
int cm;
cin >> cm;
return cm;
}
int cday()
{
cout << "Enter current day: ";
int cd;
cin >> cd;
return cd;
}
void calculate(int by, int bm, int bd, int cy)
{
double y = 31104000;
long double cby = y * by;
long double cbm = 259200 * bm;
long double cbd = 8640 * bd;
long double ccy = 31104000 * cy;
cout << endl << cby << endl;
cout << endl << ccy << endl;
cout << endl << ccy - cby << endl;
}
int main()
{
title();
int by = byear();
int bm = bmonth();
int bd = bday();
int cy = cyear();
int cm = cmonth();
int cd = cday();
calculate(by, bm, bd, cy);
cin.get();
return 0;
}

Like Kenny Ostrom commented, the shown values may look strange due to the scientific notation used by cout. To show all digits, you can change cout's precision using cout.precision(your_precision_here). See question below.
How do I print a double value with full precision using cout?

First, the numeric format you are confused by is "scientific notation". That will be enough info to open up a world of google searches, or you can just force it not to print in scientific notation.
Second, you really want to use a time library for any calendar stuff. It will handle all kinds of calendar weirdness for you, including leap years. Fortunately we have time.h
Third, I recommend using an integer type for seconds, partly to avoid rounding errors and ugly decimals, but mainly because that's what time.h uses. Just make sure it is big enough. My compiler uses a 64 bit integer for time_t, so I used that:
#include <time.h>
#include <memory>
time_t get_age_in_seconds(int year, int month, int day)
{
struct tm birthday;
memset(&birthday, 0, sizeof(birthday));
birthday.tm_year = year - 1900; // years since 1900
birthday.tm_mon = month - 1; // months since January (0,11)
birthday.tm_mday = day; // day of the month (1,31)
time_t birthday_in_seconds = mktime(&birthday);
time_t now = time(NULL);
return now - birthday_in_seconds;
}

Don't use doubles to do the calculation. You're not going to have any fractional values since you're not doing any division.
More importantly, look into mktime(), time(), and difftime(). You should be using these to do your calculation.

Related

Calculations wont display in output

I'm a student in a basic programming class and I'm trying to complete this program for a class assignment. It's a simple program that calculates compounded interest by the inputs of the user. However, when writing the code, I noticed that the the result is 0 even though based on the input I would expect otherwise. Could anyone tell me why the program isn't showing results?
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}
Yes. You are not calling the futureValue function which would compute the value for you. Due to the value not being computed, it remains 0. Fix:
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
futureValue(); //Here we compute the value
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}

Subtracting an arbitrary date from the current date C++

I'm brand new to C++. In my Programming I class, I've been given the following assignment as a midterm:
-Calculate how many days you have been alive
(epoch time from 01/01/1970 stored by seconds)
-Command Line Args for Birth Year, Birth Month, Birth Day
Now, I've already figured out the function itself for actually subtracting two dates from each other, and I've figured out how to grab the current date and stick it in that function. The problem I'm having is how to get the user's entered date and send that to the function.
I've been playing with a "test" piece of code to get this to work, unfortunately when I try to compile this I just get a MOUNTAIN of errors that I don't understand at all, so I'm clearly doing something wrong.
#include <iostream>
#include <ctime>
using namespace std;
struct tm a(int year, int month, int day)
{
struct tm birth {0};
cin >> year >> endl;
cin >> month >> endl;
cin >> day >> endl;
birth.tm_year = year - 1900;
birth.tm_mon = month;
birth.tm_mday = day;
return birth;
}
int main()
{
int year, month, day;
cout << "Enter a year, month, and day: " << endl;
cin >> year >> month >> day >> endl;
tm a(year, month, day);
time_t x = mktime(&a);
time_t y = time(0);
if ( x != (time_t)(-1) && y != (time_t)(-1) )
{
double difference = difftime(y, x) / (60 * 60 * 24);
cout << ctime(&x);
cout << ctime(&y);
cout << "difference = " << difference << " days" << endl;
}
return 0;
}
I've tried googling some of these errors, and the results I'm seeing keep talking about "pointers". I have no clue what pointers are, and flipping through our textbook, it looks like something that's about three or four chapters ahead of where we are now. I tried asking my professor about this the other day, and he just sort of giggled and said something to the effect of "Well yeah, that's the point of the midterm." I don't understand if that means I'm supposed to figure out pointers on my own or if I'm not supposed to use them.
I'm trying to get a year, month, and day from arguments entered by the user at the point of execution and stick them into a struct tm so that my function at the bottom will work.
EDIT: I figured out that I am trying to use the struct like a function, which is wrong. I have made the following changes:
#include <iostream>
#include <ctime>
using namespace std;
struct tm bday(int year, int month, int day)
{
struct tm r {0};
r.tm_year = year - 1900;
r.tm_mon = month;
r.tm_mday = day;
return r;
}
int main()
{
int year, month, day;
cout << "Enter a year, month, and day: " << endl;
cin >> year >> endl;
cin >> month >> endl;
cin >> day >> endl;
struct tm a = bday(year, month, day);
time_t x = mktime(&a);
time_t y = time(0);
if ( x != (time_t)(-1) && y != (time_t)(-1) )
{
double difference = difftime(y, x) / (60 * 60 * 24);
cout << ctime(&x);
cout << ctime(&y);
cout << "difference = " << difference << " days" << endl;
}
return 0;
}
Now getting the following errors when compiling:
filename.cpp:22:17: error: no match for 'operator>>'
filename.cpp:23:18: error: no match for 'operator>>'
filename.cpp:24:16: error: no match for 'operator>>'
This is not correct:
cin >> year >> endl;
You cannot read anything into endl. You probably confused input with output where std::cout << x << std::endl; is a common pattern. However, as a sidenote, you dont need endl after each line for output either. endl not only adds a newline character, but also flushes the stream. If you just want to start a new line then use \n only.

Why this output is 0%?

This program is going to calculate the ratio. The correct output should be 4.50492%, but my output is 0%. I doubt it's the size problem. However, the size of double is 8 bytes, and so type long long is. What's wrong with my program? Thanks.
#include <iostream>
int main()
{
using namespace std;
//cout << "Enter the world's population: ";
long long world = 6898758899;
//cin >> world;
//cout << "Enter the population of US: ";
long long us = 310783781;
//cin >> us;
double ratio = us/world * 100;
char percentage = '%';
cout << "The population of the US is " << ratio << percentage << " of the world population." << endl;
return 0;
}
because "us" and "world" type is long long.So "us/world" is long long.(long)0.0450492 == 0.
the correct spelling is
double ratio = (double)us/(double)world * 100.0;

C++ calculating salary issue

Hello I am trying to make a program that will calculate an employees bonus when given his salary and job performance, although each time i run the code the bonus is calculated to 1 regardless of the salary I input. and I have to keep my function i created it is part of my homework. Any guidance on what the issue is would be appreciated
#include <iostream>
using namespace std;
double bonus(double salary,int a);
int main()
{
double salary;
enum jobp{poor = 0,average = 1,good = 2};
jobp performance;
int a = performance;
cout<<"Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)s$
cin>>salary>>a;
bonus(salary,a);
cout<<"your bonus is "<<bonus;
return 0;
}
double bonus(double salary, int a)
{
double bonus;
if (a == 2)
{
double c;
c = .10;
bonus = salary * c;
return bonus;
}
else if (a == 1)
{
double b;
b = .05;
bonus = salary * b;
return bonus;
}
else
{
return 0;
}
}
Hi this should help you fix your problem.
Change this:
cout<<"Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)s$
cin>>salary>>a;
bonus(salary,a);
cout<<"your bonus is "<<bonus;
To this:
cout << "Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)" <<endl;
cin >> salary >> a;
cout << "your bonus is " << bonus(salary, a);
catch the value of returned bonus
#include <iostream>
using namespace std;
double bonus(double salary,int a); //this is for function right?
int main()
{
double salary;
double bonus; //create a variable for bonus
enum jobp{poor = 0,average = 1,good = 2};
jobp performance;
int a = performance;
cout<<"Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)s$
cin>>salary>>a;
bonus = bonus(salary,a); //catch the return
cout<<"your bonus is "<<bonus;
return 0;
}
Try to have the user to enter salary and performance separately.
cout << "Please enter your salary: " << endl;
cin >> salary;
cout "Please job performance (as a 0 for poor,1 for average and 2 for good)" << endl;
cin >> a;

C++ Why is this outputting the wrong compound interest?

I've been trying to figure this out for sometime and I think it has something to do with the values I'm using for the calculations. I'm not exactly familiar with compound interest so I'm not sure were I'm going wrong. Any help would be appreciated.
#include <iostream>
#include <cmath>
using namespace std;
double interest_credit_card(double initial_balance, double interest_rate, int payment_months);
// Calculates interest on a credit card account according to initial balance,
//interest rate, and number of payment months.
int main ()
{
double initial_balance, interest_rate;
int payment_months;
char answer;
do
{
cout << "Enter your initial balace: \n";
cin >> initial_balance;
cout << "For how many months will you be making payments?\n";
cin >> payment_months;
cout << "What is your interest rate (as a percent)?: %\n";
cin >> interest_rate;
cout << endl;
cout << "You will be paying: $ " << interest_credit_card( initial_balance, interest_rate, payment_months) << endl;
cout << "Would you like to try again? (Y/N)\n";
cin >> answer;
}while (answer == 'Y' || answer == 'y');
cout << "Good-Bye.\n";
return 0;
}
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double compound_interest, compounding, compounding2, compounding3, compounding4;
while(payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate/100.0);
compounding = (interest_rate /12);
compounding2 = compounding + 1;
compounding3 = interest_rate * (payment_months/12);
compounding4 = pow(compounding2, compounding3);
compound_interest = initial_balance * compounding4;
initial_balance = initial_balance + compound_interest;
payment_months--;
}
return initial_balance;
}
Inputs and expected outputs:
Enter your initial balance: 1000
For how many months will you be making payments?: 7
What is your interest rate (as a percent)?: 9
You will be paying: $1053.70
It looks like you were trying a bunch of things and then left them in. The first solution you tried was almost right, you just forgot "/12":
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
while (payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate / 100.0/12);
payment_months--;
}
return initial_balance;
}
With a little better style:
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double total_payment = initial_balance;
double monthly_rate = interest_rate / 100.0 / 12;
for (int month = 1; month <= payment_months; ++month)
total_payment += total_payment * monthly_rate;
return total_payment;
}