...

View Full Version : structure



Xiang
04-01-2003, 01:25 PM
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

Shift4Sms
04-01-2003, 06:48 PM
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.

Jason
04-01-2003, 09:32 PM
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

djdante97
04-02-2003, 12:00 AM
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:


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

Phantom
04-02-2003, 11:23 AM
I think I've heard that explained as "++i increments i, then returns i; i++ returns i, then increments i" :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum