简体   繁体   中英

My Code of C++ is not Working

I'm a Student of C++ Programming i'm trying to create a age calculater in c++ but i'm Stuck at at multiplying a Variable with a Another Variable Here's the Code:

#include <iostream.h>
#include <conio.h>
void main()
{
    clrscr();
    int birthmonth,birthyear,birthdate;
    int currentmonth,currentyear,currentdate;
    int year,month,weeks,cal,calx,days,hours;
    cout<<"Hassan's Age Calculator\n\n";
    cout<<"Enter Your Birth Year(i.e:1996):";
        cin>>birthyear;
    cout<<"\nEnter Your Birth Month(i.e:10):";
        cin>>birthmonth;
    cout<<"\nEnter date of Birth(i.e:27):";
        cin>>birthdate;
    cout<<"\nEnter The Current Month(i.e:7):";
        cin>>currentmonth;
    cout<<"\nEnter The Current Year(i.e:2013):";
        cin>>currentyear;
    cout<<"\nEnter Current Date(i.e:24):";
        cin>>currentdate;
    year=currentyear-birthyear;
    month=year*12;
    weeks=month*4.34;
    cal=(year*365.242)+currentdate;
    calx=cal+30.43;
    days=calx-birthdate;
    hours=days*24;

    cout<<"\n\n\t\tYour Age is "<<year<< " in Years" ;
    cout<<"\n\n\t\tYour Age is "<<month<< " in Months" ;
    cout<<"\n\n\t\tYour Age is "<<weeks<<" in weeks";           
    cout<<"\n\n\t\tYour Age is "<<days<<" in days";
    cout<<"\n\n\t\tYour Age is "<<hours<<" in hours";
    getch();
}

see the variable name hours its not working it shows 18640 but it should be 149712 by multiplying the answer of variable (days) to 24, and answer of days is 6238 on the console screen i'm using turbo C 4.0++ and i need help what im doing wrong.

It looks like your prehistoric compiler has 16-bit int types. 149712 is too large to fit in 16 bits, so the calculation overflows and gives an incorrect value. You should:

  • use long or int32_t if you need a type that's guaranteed to be large enough to represent numbers up to a couple of billion; or perhaps float or double since you're doing floating-point arithmetic;
  • use a modern compiler. There have been two major updates to the language and standard library over the lifetime of that compiler, and the language you are writing is barely recognisable as C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM