Thread: Help with for loop

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    Help with for loop

    I'm writing a program where I need to generate a conversion table for wrench sizes with the following columns: Size (Inches) [Fraction], Size (Inches) [Decimal rounded to 3], Next Bigger Metric (mm), Difference (Inches) [Next Bigger Metric-Inches], Closest Metric (mm) [to original size], and Difference (Inches) [Closest Metric-Inches]. I've created the column for the Size in fraction form relatively quickly but I cant seem to produce the decimal version. Instead, I get 0.000. Can somebody please take a look and see what I've missed? Thank you!

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    
    int main(int argc, char *argv[])
    {
    
    
        float wrenchSize , difference1 , difference2 , SI;
        int i, nextmetric, closestMetric;
        cout.setf(ios::fixed);
        cout.precision(3);
    
    
            cout << "  Size     Size   Next Bigger   Difference     Closest    Difference" << endl;
            cout << "(Inches) (Inches) Metric (mm)    (Inches)    Metric (mm)   (Inches)" << endl;
    
    
        for(int i = 5; i <= 32; i++)
        {
            SI = i/32 ;
            nextmetric = ceil(SI) ;
    
    
            cout << setw(3) << i << "/32 " << setw(9) << SI << setw(8) << nextmetric << endl;
            //cout << setw(13) << difference1 << setw (14) << closestMetric << setw (13) << difference2 << endl;
        }
    
    
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you realize that when you do math using two int values you get an int. So 1/32 would yield zero. If you want a floating point answer at least one of the terms must be a floating point number: 1.0/32 .

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    I changed my i value from an integer to float and in the for command as well to float i = 5.0. I'm guessing that's way wrong because now when I run the program my left most column gets a precision by 3 decimals. What did I mess up?

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    Nevermind! Got it!

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    This is what I have so far:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    
    int main()
    {
        float wrenchSize , difference1 , difference2 , SI;
        int nextmetric, closestmetric;
        cout.setf(ios::fixed);
        cout.precision(3);
    
    
            cout << "  Size       Size     Next Bigger   Difference     Closest     Difference" << endl;
            cout << "(Inches)   (Inches)   Metric (mm)    (Inches)    Metric(mm)     (Inches)" << endl;
    
    
        for(int i = 5.0 ; i <= 32; i++)
    
    
        {
            SI = i/32.0 ;
            nextmetric = ceil(SI*25.4) ;
            difference1 = nextmetric*(1/25.4) - SI ;
            closestmetric = floor(SI*25.4) ;
            difference2 = closestmetric*(1/25.4) - SI ;
    
    
            cout << setw(4) <<  i << "/32 " << setw(10) << SI << setw(9) << nextmetric << setw(16) ;
            cout << difference1 << setw (12) << closestmetric << setw (15) << difference2 << endl;
        }
    
    
        system("PAUSE");
        return 0;
    }
    I'm trying to finish my fifth column but I'm struggling. Basically this column is meant to show the closest metric in mm rounding either up or down and I'm lost. Can anyone help?
    Last edited by Allen Sarkisov; 02-15-2014 at 06:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop stops working when loop >45 ??
    By Zul56 in forum C Programming
    Replies: 3
    Last Post: 12-11-2012, 03:40 PM
  2. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  3. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM

Tags for this Thread