Is this code correct?

This is a discussion on Is this code correct? within the C++ Programming forums, part of the General Programming Boards category; Is the code below correct? My compiler says there are errors but i cant find any. Code: // depletion of ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    36

    Question Is this code correct?

    Is the code below correct? My compiler says there are errors but i cant find any.



    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
    Last edited by Sridar; 09-05-2004 at 05:00 PM. Reason: code tags added - please remember them in future

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    remove std:: from cin.get()
    Last edited by Vicious; 09-05-2004 at 04:19 PM.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my Dictionary removal code correct?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-17-2008, 09:45 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21