is there a cleaner way

This is a discussion on is there a cleaner way within the C++ Programming forums, part of the General Programming Boards category; howdy, this code does basically what i want except it seems pretty ugly. the idea is to get a value ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    is there a cleaner way

    howdy,
    this code does basically what i want except it seems pretty ugly.
    the idea is to get a value from a function, look through the array and return vales baaed on the ranges set up in the loops
    this seems pretty messy to me. is there a cleaner way of establishing the ranges and going through the arrays?

    ~~~~~~~~~~~~~~~~~code~~~~~~~~~~~~~~~~~~~~~~~~
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>


    void main (void)
    {

    system ("clear");
    start:
    cout<<"\n\n";
    double a[29] ={3160, 2940, 2390, 2250,2100, 1670, 1580, 1280, 1190, 926, 804, 486, 447, 305, 272, 229, 218, 147, 124, 64.9, 57.6, 42.4, 36.7, 26.3, 22.1, 15.2, 12.3, 6.79, 6.08};
    char b[29][9]={"S24x121","S24X106","S24x100","S24x90","S24x80", "S20x96","S20x86","S20x75","S20x66","S18x70","S18x 54.7","S15x50","S15x42.9",
    "S12x50","S12x40.8","S12x35","S12x31.8","S10x35"," S10x25.4","S8x23","S8x18.4","S7x20","S7x15.3","S6x 17.25","S6x12.5","S5x14.75","S5x10","S4x9.5","S4x7 .7"};

    double k = 0;
    cout<<" Enter a value: ";
    cin>>k;
    int i;
    double d=0.0;
    if(k>3160 || k<.1){cout<<"Value is out of range!\n";}
    if(k>=449){
    for(i = 0; i < 29; i++){
    if( a[i] >=k && a[i] < ( k +550) ){
    cout<<a[i]<<" is in the range >>-<< ";
    cout<<b[i]<<" is the shape\n";
    }
    }
    }
    if(k<=448 && k>201){
    for(i = 0; i < 29; i++){
    if( a[i] >=k && a[i] < ( k +175) ){
    cout<<a[i]<<" is in the range >>-<< ";
    cout<<b[i]<<" is the shape\n";
    }
    }
    }
    if(k<=201 && k>41){
    for(i = 0; i < 29; i++){
    if( a[i] >=k && a[i] < ( k +75) ){
    cout<<a[i]<<" is in the range >>-<< ";
    cout<<b[i]<<" is the shape\n";
    }
    }
    }
    if(k<=41 && k>=.1){
    for(i = 0; i < 29; i++){
    if( a[i] >=k && a[i] < ( k +10) ){
    cout<<a[i]<<" is in the range >>-<< ";
    cout<<b[i]<<" is the shape\n";
    }
    }
    }


    cout<<"Press any key to continue.\n";
    goto start;


    //menu();
    }
    ~~~~~~~~~~~~~~~~~code~~~~~~~~~~~~~~~~~~~~~~~
    BTW the goto is in place so i could test the code without restarting it and will be removed when i get it finished.

    thanks
    M.R.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    How about

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    const int SIZE = 29;
    
    double a[SIZE] = {
        3160, 2940, 2390, 2250, 2100,
        1670, 1580, 1280, 1190, 926,
        804, 486, 447, 305, 272,
        229, 218, 147, 124, 64.9,
        57.6, 42.4, 36.7, 26.3, 22.1,
        15.2, 12.3, 6.79, 6.08
    };
    char *b[SIZE] = {
        "S24x121","S24X106","S24x100","S24x90","S24x80",
        "S20x96","S20x86","S20x75","S20x66","S18x70",
        "S18x54.7","S15x50","S15x42.9","S12x50","S12x40.8",
        "S12x35","S12x31.8","S10x35","S10x25.4","S8x23",
        "S8x18.4","S7x20","S7x15.3","S6x17.25","S6x12.5",
        "S5x14.75","S5x10","S4x9.5","S4x7.7"
    };
    
    void checkit( double lower, double upper ) {
        for( int i = 0; i < SIZE; i++){ 
            if( a[i] >= lower && a[i] < upper ){
                cout<<a[i]<<" is in the range >>-<< ";
                cout<<b[i]<<" is the shape\n";
            }
        }
    }
    
    int main(void) {
        start:
        cout<<"\n\n";
    
        double k = 0;
        cout<<" Enter a value: ";
        cin>>k;
    
        if ( k > 3160 || k < 0.1 ) {
            cout << "Value is out of range!\n";
        } else
        if ( k >= 449 ) {
            checkit( k, k+550 );
        } else
        if ( k <= 448 && k > 201 ) {
            checkit( k, k+175 );
        } else
        if ( k <= 201 && k > 41 ) {
            checkit( k, k+75 );
        } else
        if ( k <= 41 && k >= 0.1 ) {
            checkit( k, k+10 );
        } 
    
        cout<<"Press any key to continue.\n";
        goto start;
    
        return 0; 
    }

  3. #3
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    VERY Slick

    howdy salem,
    very coooool. you real programer types are amazing. thanks for your time!

    M.R.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MRU Cleaner with VC++2005
    By Adock in forum Tech Board
    Replies: 1
    Last Post: 08-20-2008, 04:32 AM
  2. Disk Cleaner?
    By Loic in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2008, 09:32 PM
  3. registry cleaner
    By h_howee in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-05-2006, 09:13 AM
  4. What's wrong with my code (HELP)
    By n00by in forum C Programming
    Replies: 20
    Last Post: 08-11-2004, 03:15 PM
  5. UPS/power cleaner
    By Bajanine in forum Tech Board
    Replies: 1
    Last Post: 12-24-2002, 02:31 AM

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