#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Unintentional indenting happening with loops


    This is the (unintended) out put I'm getting.
    Code:
    10.00 20
     10.00 25
     10.00 30
    
    10.25 20
     10.25 25
     10.25 30
    
    10.50 20
     10.50 25
     10.50 30
    
    10.75 20
     10.75 25
     10.75 30
    
    11.00 20
     11.00 25
     11.00 30
    
    11.25 20
     11.25 25
     11.25 30
    
    11.50 20
     11.50 25
     11.50 30
    
    11.75 20
     11.75 25
     11.75 30
    
    12.00 20
     12.00 25
     12.00 30
    
    12.25 20
     12.25 25
     12.25 30
    
    12.50 20
     12.50 25
     12.50 30
    
    12.75 20
     12.75 25
     12.75 30
    
    13.00 20
     13.00 25
     13.00 30
    
    13.25 20
     13.25 25
     13.25 30
    
    13.50 20
     13.50 25
     13.50 30
    
    13.75 20
     13.75 25
     13.75 30
    
    14.00 20
     14.00 25
     14.00 30
    I have no idea why the space is there for the second and third lines for each group of three lines.

    This is the code that's doing the printing:
    Code:
        for(i = 0; i < siz; i += 3)
        {
            for(j = i; j < i + 3; ++j)
                printf("%.2f %.2d\n ", data[j].rate, data[j].duration);
            putchar('\n');
        }
    This is the entire program:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define LEN 15
    #define SIZE 100
    typedef struct
    {
        double rate;
        int duration;
        double monthly, total;
    } mortgage_t;
    
    int store_rate(double min_rate, double max_rate, mortgage_t data[SIZE]);
    void store_duration(mortgage_t data[SIZE], int siz);
    
    int main()
    {
        mortgage_t data[SIZE];
        double min_rate, max_rate;
        int siz, i, j;
    
        min_rate = 10;
        max_rate = 14;
    
        siz = store_rate(min_rate, max_rate, data);
        store_duration(data, siz);
        for(i = 0; i < siz; i += 3)
        {
            for(j = i; j < i + 3; ++j)
                printf("%.2f %.2d\n ", data[j].rate, data[j].duration);
            putchar('\n');
        }
    
    
    }
    
    void store_duration(mortgage_t data[SIZE], int siz)
    {
        int i, j, d;
        for(i = 0; i < siz; i += 3)
        {
            d = 20;
            for(j = i; j < i + 3; ++j)
            {
                data[j].duration = d;
                d += 5;
            }
        }
    }
    
    int store_rate(double min_rate, double max_rate, mortgage_t data[SIZE])
    {
        double rate;
        int i, j;
        rate = min_rate;
        for(i = 0; i < SIZE && rate <= max_rate; i += 3)
        {
            for(j = i; j < i + 3; ++j)
            {
                data[j].rate = rate;
    
            }
            rate += 0.25;
        }
        return j;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    printf("%.2f %.2d\n ", data[j].rate, data[j].duration);

    You placed a space after the \n, writing a space at the beginning of the next line. The next printf starts printing after that initial space.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Didn't notice that there... What a stupid mistake.
    Thanks for the help.

IMN logo majestic logo threadwatch logo seochat tools logo