Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Plz help c++ Plz Plz  (Read 6638 times)

0 Members and 1 Guest are viewing this topic.

his111

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    Plz help c++ Plz Plz
    « on: August 16, 2012, 09:56:57 AM »
    include<iostream.h>
    #include<conio.h>
    using namespace std;
    int main()
    {
    unsigned long int birthmonth,birthyear,birthhour;
    unsigned long int currentmonth,currentyear,currenthour;
    unsigned long int agey,agem,aget,ages;
    double agemils,agemics,agens,ageps,agefs;
    char ans;
    top:
    cout<<"\n\n\t\t\t WELCOME TO AGE CALCULATOR\n\n";
    cout<<"Enter Your Birth Year(Eg:1989):";
    cin>>birthyear;
    cout<<"\n\nEnter Your Birth Month(Eg:7):";
    cin>>birthmonth;
    cout<<"\n\nEnter your birth hour(eg:3):";
    cin>>birthhour;
    cout<<"\nEnter The Current Year(Eg:2010):";
    cin>>currentyear;
    cout<<"\nEnter The Current Month(Eg:7):";
    cin>>currentmonth;

    cout<<"\nEnter The Current Hour(Eg:6):";
    cin>>currenthour;
    agey=currentyear-birthyear;
    if(currentmonth>birthmonth)
    {
    agem=((currentyear-birthyear)*12)+(curre…
    }
    else
    {
    agem=((currentyear-birthyear-1)*12)+(cur…
    }

    aget=agem*30*24;
    ages=aget*60;
    agemils=aget*60*1000;
    agemics=agemils*1000;
    agens=agemics*1000;
    ageps=agens*1000;
    agefs=ageps*1000;
    cout<<"\n\nYour Age in years= "<<agey<<"Years;\n\nYour Age in months="<<agem<<"Months\n\nYour Age in Hours="<<aget<<"Hours\n\nYour Age in seconds="<<ages<<"seconds";
    cout<<"\n\nYour Age in Milliseconds= "<<agemils<<"milliseconds;\n\nYour Age in Microseconds= "<<agemics<<"microseconds;\n\nYour Age in NanoSeconds= "<<agens<<"nanoseconds";
    cout<<"\n\nYour Age in Picoseconds= "<<ageps<<"Picoseconds;\nYour Age in femtoseconds= "<<agefs<<"femtoseconds";
    cout << "\n Do you want to Try Again? (Y/N)" <<

    endl;

    cout << "Option: ";

    char runAgain;

    cin >> runAgain;



    if (!(runAgain == 'N' || runAgain == 'n'))
    {
    goto top;
    }

    if (!(runAgain == 'Y' || runAgain == 'Y'))

    {
    cout<<"Thanks For Trying";
    }


    getch();
    }


    Question:
    I am doing a c++ project to check age from years,months.....femtoseconds. The problem is i am not getting the output required. i am getting correct
    results upto agemils(age in milli seconds) then i get something e+xxxxx(x= number) like that. so i tried cout.precision() statement. then i dont get e+(exponential form(i guess!!)
    but still i am not getting correct results. for conversion of milli to micro,micro to nano,nano to pico,pico to femto i just need 3 zeroes extra at every successive outputs keeping numbers same. but i am getting different numbers . this is quite strange i changed all data types. please help me with this problem soon.


    Point out my mistakes and give new ideas (if any)

    I am beginner to c++ so please explain me in detail

    sorry for such a long question

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Plz help c++ Plz Plz
    « Reply #1 on: August 16, 2012, 10:28:30 AM »
    Quote
    then i get something e+xxxxx(x= number)
    the output is correct. It is in scientific notation.
    I was trying to dereference Null Pointers before it was cool.

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Plz help c++ Plz Plz
    « Reply #2 on: August 16, 2012, 10:43:26 AM »
    Yes its the correct output however if you want a long long number vs scientific notation, you could take a number like 284,000,000,000,000,000,000 and divide it to use the 284 and concatonate your 0's on the tail of the output.

    such as

    Code: [Select]
    int After_Division;

    cout<<After_Division<<",000,000,000,000,000,000\n";
    to get 284,000,000,000,000,000,000

    You can use your existing output from ages below and include that in every cout<<ages   then for agemils have cout<<ages<<"000\n"; and have cout<<ages<<"000000\n"; for agemics, then cout<<ages<<"000000000\n"; for agens and then cout<<"000000000000\n"; for ageps outputs and stack your 0's on the end.
    Quote
    aget=agem*30*24;
    ages=aget*60;
    agemils=aget*60*1000;
    agemics=agemils*1000;
    agens=agemics*1000;
    ageps=agens*1000;
    agefs=ageps*1000;

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Plz help c++ Plz Plz
    « Reply #3 on: August 16, 2012, 10:58:51 AM »
    Code: [Select]
    double someval=40E120;
    cout<<fixed<<someval<<endl;
    I was trying to dereference Null Pointers before it was cool.

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Plz help c++ Plz Plz
    « Reply #4 on: August 16, 2012, 11:27:46 AM »
    BC can you explain

    double someval=40E120;
    cout<<fixed<<someval<<endl;

    I dont see any scale to this concationation of an INT and DOUBLE for each ms, ns, ps etc?

    Also

    aget=agem*30*24;
    ages=aget*60;
    agemils=aget*60*1000;
    agemics=agemils*1000;
    agens=agemics*1000;
    ageps=agens*1000;
    agefs=ageps*1000;

    is easier to follow if you change  agemils=aget*60*1000; to agemils=ages*1000; and the calculation is partially redundant to the prior formula above it. It looks much cleaner as below even though you will end up with same output.

    aget=agem*30*24;
    ages=aget*60;
    agemils=ages*1000;
    agemics=agemils*1000;
    agens=agemics*1000;
    ageps=agens*1000;
    agefs=ageps*1000;



    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Plz help c++ Plz Plz
    « Reply #5 on: August 16, 2012, 11:39:11 AM »
    Looking at this conversion formula I think there is a typo too

    Code: [Select]
    aget=agem*30*24;
    ages=aget*60;
    agemils=aget*60*1000;

    if Agem starts as 12, we end up with:

    aget=12*30*24=8640
    ages=8640*60=518400
    agemils=8640*60*1000=518400000

    AND; for the 'fixed' version:

    Code: [Select]
    aget=12*30*24=8640
    ages=8640*60=518400
    agemils=518400*1000=518400000

    The results are the exact same.

    I was trying to dereference Null Pointers before it was cool.

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Plz help c++ Plz Plz
    « Reply #6 on: August 16, 2012, 11:50:59 AM »
    After I submitted it I realized it wasnt typo just a longer than needed formula... was editing my post then got pulled away from my lunch and came back and you beat me to the correction to my original post that I corrected.

    I should have eaten my lunch to increase  blood sugar = better thinking, before posting that, and corrected it sooner than later...LOL  ;D

    Hypoglycemia is not fun  :(

    his111

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Unknown
      Re: Plz help c++ Plz Plz
      « Reply #7 on: August 16, 2012, 07:32:23 PM »
      Yes its the correct output however if you want a long long number vs scientific notation, you could take a number like 284,000,000,000,000,000,000 and divide it to use the 284 and concatonate your 0's on the tail of the output.

      such as

      Code: [Select]
      int After_Division;

      cout<<After_Division<<",000,000,000,000,000,000\n";
      to get 284,000,000,000,000,000,000

      You can use your existing output from ages below and include that in every cout<<ages   then for agemils have cout<<ages<<"000\n"; and have cout<<ages<<"000000\n"; for agemics, then cout<<ages<<"000000000\n"; for agens and then cout<<"000000000000\n"; for ageps outputs and stack your 0's on the end.

      Good idea to suffix with 0's

      DaveLembke



        Sage
      • Thanked: 662
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Plz help c++ Plz Plz
      « Reply #8 on: August 17, 2012, 09:20:57 AM »
      Was looking back and noticed that you are using a goto statement.

      goto top;

      I'd wrap it all within a while loop and do away with the goto. If this is school work, which I am thinking it may be, an instructor might complain with that goto's lead to spaghetti code. Having started with GW-Basic as my first programming language with line numbers, when I referred to goto's when we were discussing loops and redirection, I was scolded with NEVER USE GOTO's its poor programming technique and spaghetti code.

      Maybe you haven't played with loops yet and will soon. They are lots of fun. C/C++ is one of my favorite languages and powerful. PERL is my 2nd favorite.

      Personally since then, I have used goto's as a quick fix at times than to nest everything within loops, but when creating programs I try to avoid them whenever possible vs making sloppy code with goto here, goto there, goto this_alternative_object, goto beginning_to_restart


      TechnoGeek

      • Guest
      Re: Plz help c++ Plz Plz
      « Reply #9 on: August 17, 2012, 09:44:02 AM »
      There's also another issue with goto: it's not usually implemented well (or at all) in other programming languages, decreasing portability.

      An example is TI-BASIC (Used on the TI-83/84 series of calculators), where if statements and loops used a stack mechanism to remember the beginning of the statement until it was ended. Goto could be used to jump out of one of these structures, possibly starting another loop. Eventually, an ERR: MEMORY would come up because of how much memory was used by the stack mechanism and how many loops had piled up unended. Needless to say, that was a pretty serious problem.

      Veltas



        Intermediate

        Thanked: 7
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Linux variant
      Re: Plz help c++ Plz Plz
      « Reply #10 on: August 17, 2012, 09:55:37 AM »
      There's also another issue with goto: it's not usually implemented well (or at all) in other programming languages, decreasing portability.

      An example is TI-BASIC (Used on the TI-83/84 series of calculators), where if statements and loops used a stack mechanism to remember the beginning of the statement until it was ended. Goto could be used to jump out of one of these structures, possibly starting another loop. Eventually, an ERR: MEMORY would come up because of how much memory was used by the stack mechanism and how many loops had piled up unended. Needless to say, that was a pretty serious problem.

      Its implementation in C++ can also be confusing.  Especially if used within loops, in fact, although one would hope you wouldn't mix goto with loops.

      his111, don't feel like there's something terribly wrong with using goto in a small program like yours, it's just we try to avoid it for the reasons described and it's a practice that can quickly make programs far harder to read.  I'm sure Dave identified it because you're new and it's something a new programmer should know.  And I too hope you enjoy learning C++, it is a very nice language sometimes.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Plz help c++ Plz Plz
      « Reply #11 on: August 17, 2012, 11:35:39 AM »
      BC can you explain

      double someval=40E120;
      cout<<fixed<<someval<<endl;

      I dont see any scale to this concationation of an INT and DOUBLE for each ms, ns, ps etc?
      casting from a int to a double does not result in any concatenation. Only narrowing conversions need a cast.



      Quote
      and the calculation is partially redundant to the prior formula above it.
      There is no redundancy. Any compiler worth using will optimize the operations at compile time anyway.

      Quote
      It looks much cleaner as below even though you will end up with same output.
      This is true.

      I was trying to dereference Null Pointers before it was cool.