Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: structure

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Malaysia
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    structure

    Dear sir,

    Refers to the below coding. Do you know, where is my mistake, as after I enter an account number the program will stop and back to the source file. Why???

    #include <stdio.h>
    #include <conio.h>

    void readinput (int i);
    void writeoutput (int i);

    struct date {
    int month;
    int day;
    int year;
    };

    struct account {
    char name[80];
    char street[80];
    char city[80];
    long int acct_no;/* (positive integer) */
    char acct_type;/* C (ccurent), O (overdue), or D (deliquent) */
    float oldbalance;/* (nonnegative quantity) */
    float newbalance;/* (nonnegative quantity) */
    float payment;/* (nonnegative quantity) */
    struct date lastpayment;
    } customer[100];/* maintain as many as 100 customers */

    void main(void)
    {
    int i, n;

    clrscr();

    printf("CUSTOMER BILLING SYSTEM\n\n");
    printf("How many customers are there? ");
    scanf("%d", &n);

    for (i = 0; i < n; ++i)
    {
    readinput(i);

    /* determine account status */
    if (customer[i].payment > 0)
    customer[i].acct_type = (customer[i].payment < 0.1 * customer[i].oldbalance) ? 'O' : 'C';

    else
    customer[i].acct_type = (customer[i].oldbalance > 0) ? 'D' : 'C';

    /* adjust account balance */
    customer[i].newbalance = customer[i].oldbalance - customer[i].payment;
    };

    for (i = 0; i < n; ++i)
    writeoutput(i);

    getch();
    }


    void readinput(int i)

    /* read input data and update record for each customer */

    {
    printf("\nCustomer no. %d\n", i + 1);

    printf(" Name: ");
    scanf(" %[^\n]", customer[i].name);
    fflush(stdin);

    printf(" Street: ");
    scanf(" %[^\n]", customer[i].street);
    fflush(stdin);

    printf(" City: ");
    scanf(" %[^\n]", customer[i].city);
    fflush(stdin);

    printf(" Account number: ");
    scanf("%ld", &customer[i].acct_no);
    fflush(stdin);

    printf(" Previous balance: ");
    scanf("%f", &customer[i].oldbalance);
    fflush(stdin);

    printf(" Current payment: ");
    scanf("%f", &customer[i].payment);
    fflush(stdin);

    printf(" Payment date (mm/dd/yyy): ");
    scanf("%d/%d/%d", &customer[i].lastpayment.month,
    &customer[i].lastpayment.day,
    &customer[i].lastpayment.year);
    return;
    }


    void writeoutput(int i)

    /* display current information for each customer */

    {
    printf("\nName: %s", customer[i].name);
    printf(" Account number: %d\n", customer[i].acct_no);
    printf("Street: %s\n", customer[i].street);
    printf("City %s\n\n", customer[i].city);
    printf("Old balance: %7.2f", customer[i].oldbalance);
    printf(" Current payment: %7.2f", customer[i].payment);
    printf(" New balance: %7.2f\n\n", customer[i].newbalance);
    printf("Account status: ");

    switch (customer[i].acct_type)
    {
    case 'C' : printf("CURRENT\n\n");
    break;
    case 'O' : printf("OVERDUE\n\n");
    break;
    case 'D' : printf("DELINQUENT\n\n");
    break;
    default : printf("ERROR\n\n");
    }
    return;
    }


    Thanks,

    Xiang

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Las Vegas, NV - USA
    Posts
    104
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm assuming that this is a simple test program, if not, scanf() is not very user friendly! Anyway, your immediate problem is that "n" is never set so the value is unknown.

    Wait, strike that. I do see where you set "n". The next issue is that your loop "for(i=0; i<n; ++i)" should probably be "for(i=0; i<n; i++)" -- the difference between "++i" and "i++" is that the first value that "i<n" will see is 1 if the former is used.
    Last edited by Shift4Sms; 04-01-2003 at 06:52 PM.
    Steven Sommers (blog)
    Shift4 Corporation -- www.shift4.com

    Creators of $$$ ON THE NET(tm) payment processing services.

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't see the problem, at least not yet. But there are two things Ive seen, one is that you have a ';' after one of te for() { } loops and then your date is mm/dd/yyy didn't get the 3y's but that is your code....still looking though. And I don't get why ++i and i++ are different never had a difference in them before.



    Jason

  • #4
    New Coder
    Join Date
    Dec 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Jason
    And I don't get why ++i and i++ are different never had a difference in them before.
    the difference is ++i is pre-increment, and i++ is post increment.
    example:
    Code:
    int i, s, t;
    i = 1;
    s = i++; // s gets 1 since i is incremented post-operation
    t = ++i; // t gets 3 since i is incremented pre-operation

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I've heard that explained as "++i increments i, then returns i; i++ returns i, then increments i"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •