-
Is this code correct?
Is the code below correct? My compiler says there are errors but i cant find any. :confused: :rolleyes: :(
Code:
// depletion of CFC compounds
#include <iostream.h>
int main()
{
double numyears;
double half_life;
double amount;
double name;
const double limit = 0.01; // amount we want from what start with
cout <<"Enter half life of the element/compound:" ;
cin >>half_life;
std::cin.get();
amount = 1.0; // % of what we start with
numyears=0.0; //number of years we have waited
while (amount > limit) //the new concept here. see NOTE
{
numyears=numyears + half_life;
amount = amount*0.5;
}
cout <<"You would have to wait " <<numyears << " years" <<endl;
std::cin.get();
return 0;
}
//NOTE
//the new concept here. It means...while the amount >limit , it keeps looping.
//Stops only if the stuff in() is false
//the next line is the main meat of loop
//it means, DO number of years =numyears+halflife while numyears increases by 1.
// While that happens, the amount is halved
-
remove std:: from cin.get()
-
Ignore Vicious and change
Code:
#include <iostream.h>
To
Code:
#include <iostream>
How about telling us what the error you recieved is. Also please see the post at the top of the forum about posting code.
-
>Is the code below correct?
No. When it comes to the standard headers, either go all the way or don't go at all. Mixing old style headers with namespaces will cause you no end of grief. Aside from that, some inconsistent formatting, and an unused variable, all seems well.
Code:
#include <iostream>
int main()
{
double numyears;
double half_life;
double amount;
const double limit = 0.01; // amount we want from what start with
std::cout<<"Enter half life of the element/compound:" ;
std::cin>> half_life;
std::cin.get();
amount = 1.0; // % of what we start with
numyears = 0.0; //number of years we have waited
while (amount > limit) //the new concept here. see NOTE
{
numyears = numyears + half_life;
amount = amount * 0.5;
}
std::cout<<"You would have to wait "<< numyears <<" years"<<std::endl;
std::cin.get();
return 0;
}