1.

Solve : Plz help c++ Plz Plz?

Answer»

include
#include
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= "<cout<<"\n\nYour Age in Milliseconds= "<cout<<"\n\nYour Age in Picoseconds= "<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 questionQuote

then i get something e+xxxxx(x= number)
the output is correct. It is in scientific notation.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<Quote
aget=agem*30*24;
ages=aget*60;
agemils=aget*60*1000;
agemics=agemils*1000;
agens=agemics*1000;
ageps=agens*1000;
agefs=ageps*1000;
Code: [Select]double someval=40E120;
cout<<fixed<<someval<<endl;
BC can you explain

double someval=40E120;
cout<
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;


Quote from: DaveLembke on August 16, 2012, 11:27:46 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.

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

Hypoglycemia is not fun Quote from: DaveLembke 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.

Good idea to suffix with 0'sWas 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

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.Quote from: TechnoGeek 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.

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.Quote from: DaveLembke 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?
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.



Discussion

No Comment Found