lo0p

why my while loop can't run???





#include<stdio.h>
int main(){

float money,money1,balance,balance1;
int deposite1,withdraw,deposite;
int password,choice;
char answer;



printf("Assumed your money for example\n");
scanf("%f",&money1);

money=money1;
printf("Your money is RM%.2f\n",money);
printf("********************************\n");
printf(" Welcome to BankRupted\n\n\n");
printf("********************************\n");

printf("\nPlease enter your password:\n");
scanf("%f",&password);

printf("Your password is valid!!\n");

do
{


printf("________________________________\n");
printf(" 1.Check your balance\n");
printf(" 2.Deposite\n");
printf(" 3.Withdraw\n");
printf("________________________________\n");
printf("Please enter your choice:\n");
scanf("%d",&choice);

switch(choice)
{

case 1:
{

printf("Your balance :RM%.2f\n", money);
break;

case 2 :

printf("You can only deposite note RM10, RM50 and RM100\n");
printf("Enter the amount of money to be deposite:\n");
scanf("%d",&deposite1);
deposite=deposite1/10;
printf("Your had deposite :RM10 x %d\n", deposite);
balance1=deposite1+money1;
printf("Your balance is :RM%.2f\n", balance1);
break;

case 3:

printf("Enter the amount of money to be withdraw:\n");
scanf("%d",&withdraw);
printf("Your amount is :RM%d\n", withdraw);
balance=money1-withdraw;
printf("Your balance is :RM%.2f\n", balance);

break;
}
}


printf("Do you want to continue(y=Yes/n=No)?\n");
scanf("%c", &answer);

}
while (answer=='y'||answer=='Y');{



printf("________________________________\n");
printf("Thank You for using our services\n");
printf("Please come again\n");
printf("********************************\n");
return 0;
}
}
You have brace issues.

The brace below case 1 lines up with the first brace after break, which I doubt you want.

Second, the while() you are referring to matches up with the "do" at the top.

Third, the last set of braces you have are meaningless.

The buffer is not empty when you call scanf("%c", &answer);
You can clear it by using getchar(); just before the scanf. However, you should really consider using c++ style i/o functions in general.
http://www.cplusplus.com/doc/tutorial/basic_io.html
These are inconsistent.
1
2
3
	int password,choice;
	printf("\nPlease enter your password:\n");
	scanf("%f",&password);


The following code is reading the <CR> you needed to press to enter the choice.
1
2
	printf("Do you want to continue(y=Yes/n=No)?\n");
	scanf("%c", &answer);
I adjusted your code.
You should definetely properly indent your code, otherwise it's unreadable.
Next time, do something like this, so you know where your int, do and while, etc , all end.

I also commented out the second part of kbw's comment. Where you ask if the user wants to continue. It's under the do loop, so the compiler expects to see a while.

And where do you define the password for the user anyway?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>

int main()
{
    float money,money1,balance,balance1;
    int deposite1,withdraw,deposite;
    int password,choice;
    char answer;

    printf("Assumed your money for example\n");
    scanf("%f",&money1);

    money=money1;
    printf("Your money is RM%.2f\n",money);
    printf("********************************\n");
    printf(" Welcome to BankRupted\n\n\n");
    printf("********************************\n");

    printf("\nPlease enter your password:\n");
    scanf("%f",&password);

    printf("Your password is valid!!\n");

        do
          {
           printf("________________________________\n");
           printf(" 1.Check your balance\n");
           printf(" 2.Deposite\n");
           printf(" 3.Withdraw\n");
           printf("________________________________\n");
           printf("Please enter your choice:\n");
           scanf("%d",&choice);

           switch(choice)
           {
              case 1:
              {
                printf("Your balance :RM%.2f\n", money);
                break;
              }
              case 2 :
              {
                printf("You can only deposite note RM10, RM50 and RM100\n");
                printf("Enter the amount of money to be deposite:\n");
                scanf("%d",&deposite1);
                deposite=deposite1/10;
                printf("Your had deposite :RM10 x %d\n", deposite);
                balance1=deposite1+money1;
                printf("Your balance is :RM%.2f\n", balance1);
                break;
              }
              case 3:
              {
                printf("Enter the amount of money to be withdraw:\n");
                scanf("%d",&withdraw);
                printf("Your amount is :RM%d\n", withdraw);
                balance=money1-withdraw;
                printf("Your balance is :RM%.2f\n", balance);
                break;
              }
            } // end of switch
         } // end of do
         
         // error: expected 'while' before "printf" and then code blocks { }
         printf("Do you want to continue(y=Yes/n=No)?\n");
         scanf("%c", &answer);

}while (answer=='y'||answer=='Y');

  
  {
  printf("________________________________\n");
  printf("Thank You for using our services\n");
  printf("Please come again\n");
  printf("********************************\n");
  return 0;
  }
}
i've run your coding..
but there are 6 errors..
why???
1
2
3
// error: expected 'while' before "printf" and then code blocks { }
         printf("Do you want to continue(y=Yes/n=No)?\n");
         scanf("%c", &answer);

The comment section explicitly explains the error. Just waiting for you to fix it.

and

1
2
   printf("\nPlease enter your password:\n");
    scanf("%f",&password);

As kbw pointed out, there's an inconsistency here.

We're not here to do your assignment for you.
We're just pointing out where there is a problem.


oh..
thanks!
Topic archived. No new replies allowed.