NO

Author Topic: why does my printf statement won't come out, and it continues as infinity loop?  (Read 4379 times)

fyzafuaad

  • Guest
I have 3 problems right now. first is, this code can execute but then, when I enter the correct account ID and password, the printf statement for the new balance won't come out.

second, it keeps executing the codes even after the 3rd attempt (as infinity loop).

third one is the  printf("Authentication failed in too many times. Goodbye!\n\n"); statement is supposed to execute only after the 3rd attempt of entering the password and ID. and after this code execute, it should go back to the main function again. so did I put this code at the wrong place ?

can anyone point out what is wrong with this code ?

Code: [Select]
#include <stdio.h>
#include <string.h>

void info();
void deposit();
void balance();
void withdrawal();
void quit();

//structure declared
struct bank_acc {
int accID; //shortform of "account ID"
char name[20];
float balance;
char password[10];
};

//structure assigned
struct bank_acc account_1 = {11111, "Susan Lee", 10000.0, "passwd1"};
struct bank_acc account_2 = {22222, "James Mason", 20000.0, "passwd2"};
struct bank_acc account_3 = {33333, "Jane Han", 30000.0, "passwd3"};
struct bank_acc account_4 = {44444, "Brian Jacobs", 250000.0, "passwd4"};
struct bank_acc account_5 = {55555, "Clara Wyoming",2500.0, "passwd5"};

int main (void)
{
int i = 1;
while(i < 4)
{
int i;

printf("Welcome to Ewha Bank\n\n");
printf("1\tDeposit\n");
printf("2\tBalance\n");
printf("3\tWithdrawal\n");
printf("4\tQuit\n\n");
printf("Choose an action\n");

scanf("%d", &i);

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

void deposit()
{
int i = 1;
int accID; //shortform of "account ID"
char password[10];
float deposit, newbalance;

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

while (i < 3)
{
printf("Please enter your account ID and password\n\n");
scanf("%d %s", &accID, password);

if (accID==account_1.accID && strcmp(account_1.password, "passwd1"))
{
newbalance = deposit + account_1.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_2.accID && strcmp(account_2.password, "passwd2"))
{
newbalance = deposit + account_2.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_3.accID && strcmp(account_3.password, "passwd3"))
{
newbalance = deposit + account_3.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_4.accID && strcmp(account_4.password, "passwd4"))
{
newbalance = deposit + account_4.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_5.accID && strcmp(account_5.password, "passwd5"))
{
newbalance = deposit + account_5.balance;
printf("Your current balance is %.2f", newbalance);
}
else
{
printf("Authentication failed in too many times. Goodbye!\n\n"); //this is supposed to print out after 3rd attempt
}
}
}


void balance()
{
//not yet
}

void withdrawal()
{
//not yet
}

void quit()
{
//not yet
}

the result was this

Quote
Welcome to Ewha Bank

1       Deposit
2       Balance
3       Withdrawal
4       Quit

Choose an action
1

Please enter the amount you would like to deposit:
222

Please enter your account ID and password
11111
passwd1
Authentication failed in too many times. Goodbye!

Please enter your account ID and password
22222
passwd2
Authentication failed in too many times. Goodbye!

Please enter your account ID and password
33333
passwd3
Authentication failed in too many times. Goodbye!

Please enter your account ID and password
44444
passwd4
Authentication failed in too many times. Goodbye!

Please enter your account ID and password


.... (continue as infinity loop)


« Last Edit: May 28, 2016, 04:32:59 PM by fyzafuaad »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
where you update i value?
May the source be with you

fyzafuaad

  • Guest
what do you mean by where did I update my i value ?

UPDATE: I already fixed the infinity loop problem. Only left with both of the printf statements problem.

Code: [Select]
#include <stdio.h>
#include <string.h>

void info();
void deposit();
void balance();
void withdrawal();
void quit();

//structure declared
struct bank_acc {
int accID; //shortform of "account ID"
char name[20];
float balance;
char password[10];
};

//structure assigned
struct bank_acc account_1 = {11111, "Susan Lee", 10000.0, "passwd1"};
struct bank_acc account_2 = {22222, "James Mason", 20000.0, "passwd2"};
struct bank_acc account_3 = {33333, "Jane Han", 30000.0, "passwd3"};
struct bank_acc account_4 = {44444, "Brian Jacobs", 250000.0, "passwd4"};
struct bank_acc account_5 = {55555, "Clara Wyoming",2500.0, "passwd5"};

int main (void)
{
int i;

printf("Welcome to Ewha Bank\n\n");
printf("1\tDeposit\n");
printf("2\tBalance\n");
printf("3\tWithdrawal\n");
printf("4\tQuit\n\n");
printf("Choose an action\n");

scanf("%d", &i);
printf("\n");

switch(i)
{
case 1 : deposit();
break;
case 2 : balance();
break;
case 3 : withdrawal();
break;
case 4 : quit ();
break;
}

return 0;
}

void deposit()
{
int i = 1;
int accID; //shortform of "account ID"
char password[10];
float deposit, newbalance;

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

while (i < 4)
{
printf("Please enter your account ID and password\n");
scanf("%d %s", &accID, password);

if (accID==account_1.accID && strcmp(account_1.password, "passwd1"))
{
newbalance = deposit + account_1.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_2.accID && strcmp(account_2.password, "passwd2"))
{
newbalance = deposit + account_2.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_3.accID && strcmp(account_3.password, "passwd3"))
{
newbalance = deposit + account_3.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_4.accID && strcmp(account_4.password, "passwd4"))
{
newbalance = deposit + account_4.balance;
printf("Your current balance is %.2f", newbalance);
}
else if (accID==account_5.accID && strcmp(account_5.password, "passwd5"))
{
newbalance = deposit + account_5.balance;
printf("Your current balance is %.2f", newbalance);
}
else
{
i++;

}
}
}


void balance()
{
//not yet
}

void withdrawal()
{
//not yet
}

void quit()
{
//not yet
}
« Last Edit: May 28, 2016, 05:17:06 PM by fyzafuaad »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
what do you mean by where did I update my i value ?

UPDATE: I already fixed the infinity loop problem.
Well,....
I would rather say you kind of bypassed the problem, somehow, with consequences.
Quote
Only left with both of the printf statements problem.

One of those is pretty easy, you don't have that "Authentication failed...." message in your code at all anymore.

And the second one ( I am currently not on a Windows machine, so I can't test/verify it), you should look up your C book(s) about the "\n" character and also read up on "buffered output"...

Ralf

PS: As mentioned before, you should get yourself acquainted with "arrays", it would help to reduce on code and improve on clarity on your case....

fyzafuaad

  • Guest
I tried using array but I just couldn't understand at all about using array to simplify my codes T.T

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
you can use loops for checking id and password in 1 - N accounts.
and check that strcmp()  again.
May the source be with you