Here is my code I cant figure it out

This is a discussion on Here is my code I cant figure it out within the C++ Programming forums, part of the General Programming Boards category; //Problem is i cant get my inches left over i cant think in my head what i have to do!!!!! ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    Thumbs down Here is my code I cant figure it out

    //Problem is i cant get my inches left over i cant think in my head what i have to do!!!!! ive gotten this far

    Code:
    #include <iostream>
    
    using namespace std; 
    
    //CONSTANTS
    const double CM_PER_INCH	= 2.54;
    const double INCH_PER_FOOT	= 12;
    const double INCH_PER_YARD	= 36;
    
    
    int main ()
    {	
    
    //VARIABLES
    int feet;
    double cm;
    double totalinches;
    int totalyards;
    int roundedinch;
    int inchesleft;
    //END 
    
    cout << "Please enter the length in centimeters: ";
    cin >> cm;
    
    //converting to inch
    
    totalinches = cm / CM_PER_INCH;
    
    //roundedinches
    
    roundedinch = totalinches + .05;
    
    
    //TOTAL YARDS
    
    totalyards = roundedinch / INCH_PER_YARD;  
    
    
    //TOTAL FEET
    
    feet = (roundedinch - (totalyards * INCH_PER_YARD )) / INCH_PER_FOOT; 
    
    //LEFT OVER INCHES
    
    inchesleft = 
    
    
    
    //Results
    
    cout << " Total Yards: " <<totalyards << " Total Feet: " <<feet << " Inches: "<<inchesleft <<endl;
    
    
    
    return 0;
    
    
    
    
    }

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You were getting 2.16667 yards as a double, which would mean you have 2 yards, and .16667 left over to convert to inches. The problem was that you were using int for yards, meaning it left you with 2 yards, so no feet would be needed, and your inches algorthm wasn't filled in.. it would have been whatevers left over in feet (e.g. 3.0374 would be 3 feeet, .0374 feet to convert to inches).

    You could do this by static casting<int> and float, or (float) and (int) a lot, but I suggest just doing all the calculations in double, then static_cast the final outcome.

    Code:
    #include <iostream>
    
    using namespace std; 
    
    //CONSTANTS
    const double CM_PER_INCH	= 2.54;
    const double INCH_PER_FOOT	= 12;
    const double INCH_PER_YARD	= 36;
    
    
    int main ()
    {
    //VARIABLES
      double totalfeet;
      double cm;
      double totalinches;
      double totalyards;
      double inchesleft;
    //END 
    
      cout << "Please enter the length in centimeters: ";
      cin >> cm;
    
    //converting to inch
    
      totalinches = cm / CM_PER_INCH;
    
    
    //TOTAL YARDS
    
      totalyards = totalinches / INCH_PER_YARD;
    
    
    //TOTAL FEET
      totalfeet = ((totalyards - static_cast<int>(totalyards)) * INCH_PER_YARD) / INCH_PER_FOOT;
    
    //LEFT OVER INCHES
    
      inchesleft = (totalfeet - static_cast<int>(totalfeet)) * INCH_PER_FOOT;
    
    
    
    //Results
      cout << "Total Yards: " << static_cast<int>(totalyards) 
      << " Total Feet: " << static_cast<int>(totalfeet) 
      << " Inches: "<< static_cast<int>(inchesleft) <<endl;
    
    
      cin.ignore();
      cin.get();
    
    return 0;
    }
    The reason its totalfeet - static_cast<int>(totalfeet) is because you are taking the 2.16667 yards, then making yards an int, making it 2 yards, and minusing: 2.1667 yards - 2 yards = 0.1667 yards left to convert to feet. And the same goes for feet-> inches.
    Last edited by Dae; 10-01-2005 at 04:20 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. 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
  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