NO

Author Topic: every users' balance won't come out properly  (Read 3159 times)

fyzafuaad

  • Guest
every users' balance won't come out properly
« on: June 03, 2016, 05:18:37 PM »
I hope this is my last question tho hahaha. okayy . so all the error are now gone and all, but the problem is, every users' balance value should be different right? but they all just print out the same value.

Is there anything wrong ? I only posted some of the codes(I'm sorry if it is too long, I don't know how to reduce for you guys to read), if there is no problem with it, then you can asked for the other codes.

Code: [Select]
struct bank_acc {
int accID; //"account ID"
char name[20];
float balance;
char password[10];
};

struct bank_acc accList[5] = { { 11111, "Susan Lee", 10000.0, "passwd1" },{ 22222, "James Mason", 20000.0, "passwd2" },{ 33333, "Jane Han", 30000.0, "passwd3" }};

int main(void)
{
int i;

printf("Welcome to Ewha Bank\n\n");
printf("1\tDeposit\n");
printf("2\tBalance\n");
printf("Choose an action\n");
scanf("%d", &i);

switch (i)
{
case 1: deposit();
break;
case 2: balance();
break;
}
return 0;
}

int login()
{
int result = FALSE;
int accID; //shortform of "account ID"
char password[10];
int count = 0;

do {
if (count == 3);

printf("\nPlease enter your account ID and password:");
scanf("%d %s", &accID, password);

for (int index = 0; index < 5; index++)
{

if (accID == accList[index].accID && strcmp(accList[index].password, password) == 0)
{
result = TRUE;
return result;
}
}

if (result == FALSE)
{
printf("\nThis account does not exist!\n");
count++;
}
if(count==3)
{
main();
}

} while (count);
return result;
}

void deposit()
{
int index;
float deposit, newbalance;

printf("\nPlease enter the amount you would like to deposit: \n");
scanf("%f", &deposit);

index = login();

if (index >= 0)
{
newbalance = deposit + accList[index].balance;
printf("\n***Your current balance is %.2f***\n", newbalance);
}
main();
}

« Last Edit: June 03, 2016, 05:23:29 PM by fyzafuaad »

Scripter

  • Guest
Re: every users' balance won't come out properly
« Reply #1 on: June 03, 2016, 10:05:31 PM »
I hope this is my last question tho hahaha. okayy . so all the error are now gone and all, but the problem is, every users' balance value should be different right? but they all just print out the same value.

Is there anything wrong ? I only posted some of the codes(I'm sorry if it is too long, I don't know how to reduce for you guys to read), if there is no problem with it, then you can asked for the other codes.

Code: [Select]
struct bank_acc {
int accID; //"account ID"
char name[20];
float balance;
char password[10];
};

struct bank_acc accList[5] = { { 11111, "Susan Lee", 10000.0, "passwd1" },{ 22222, "James Mason", 20000.0, "passwd2" },{ 33333, "Jane Han", 30000.0, "passwd3" }};

int main(void)
{
int i;

printf("Welcome to Ewha Bank\n\n");
printf("1\tDeposit\n");
printf("2\tBalance\n");
printf("Choose an action\n");
scanf("%d", &i);

switch (i)
{
case 1: deposit();
break;
case 2: balance();
break;
}
return 0;
}

int login()
{
int result = FALSE;
int accID; //shortform of "account ID"
char password[10];
int count = 0;

do {
if (count == 3);

printf("\nPlease enter your account ID and password:");
scanf("%d %s", &accID, password);

for (int index = 0; index < 5; index++)
{

if (accID == accList[index].accID && strcmp(accList[index].password, password) == 0)
{
result = TRUE;
return result;
}
}

if (result == FALSE)
{
printf("\nThis account does not exist!\n");
count++;
}
if(count==3)
{
main();
}

} while (count);
return result;
}

void deposit()
{
int index;
float deposit, newbalance;

printf("\nPlease enter the amount you would like to deposit: \n");
scanf("%f", &deposit);

index = login();

if (index >= 0)
{
newbalance = deposit + accList[index].balance;
printf("\n***Your current balance is %.2f***\n", newbalance);
}
main();
}

Because the index value inside your deposit function is not initialized with any value.
You should be passing the index into the function as a parameter.
 

fyzafuaad

  • Guest
Re: every users' balance won't come out properly
« Reply #2 on: June 04, 2016, 06:34:41 AM »
Quote
Because the index value inside your deposit function is not initialized with any value.
You should be passing the index into the function as a parameter.

What is the meaning of that ? I'm soooo sorry, I'm still noob in this proramming thing that I don't even get what you mean T.T

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: every users' balance won't come out properly
« Reply #3 on: June 04, 2016, 08:26:45 AM »
login should return accountid or -1?
May the source be with you

Scripter

  • Guest
Re: every users' balance won't come out properly
« Reply #4 on: June 05, 2016, 10:55:20 AM »
Quote
Because the index value inside your deposit function is not initialized with any value.
You should be passing the index into the function as a parameter.

What is the meaning of that ? I'm soooo sorry, I'm still noob in this proramming thing that I don't even get what you mean T.T

When you do this...
Code: [Select]
int index;... all the compiler does is make space for the variable. It does not set it to any specific value (like 0). Thus your index value is a random number, based on garbage left behind in memory.

The correct way to create a variable is this...
Code: [Select]
int index = 0;... or whatever value is appropriate to your program.

Now, why should you be passing an index value into that function?
Because you want to adjust the balance for the user who is currently using the program, which you determine as an index value into your accounts array.  To make sure the correct balances are adjusted you need to tell your function which one to adjust...
Code: [Select]
void Deposit(int index)
   {
      ....

      account[index].balance += deposit;
   }

Programming is 50% thinking about how to do things, 30% looking stuff up, 1% typing and the rest is a matter of finding your own mistakes. It's all pretty confusing at first but with some experience it will come naturally. 

You might also try some of the popular tutorials on the web...  For example: https://kldp.org/files/c+in+21+days.pdf