Ronak Patel
Ronak Patel

Reputation: 11

switch case loop statement

I am having a problem with my one assignment. I made one function name chequingsaving and when I call this function, it prints some data which is correct, but when I entered character depends on my selection of account it directly goes out of the loop. If I entered a correct character value, then same thing happens. That function code is below. Help me to find out my mistake. The thing is I also used a switch case, but still it directly goes to default. Same thing happens with if/else loop.

void chequingsaving()  /*Chequing and saving account function*/
{
    char a;
    double r, Initialamount = 0;
    printf(" c) Chequing\n");
    printf(" s) Savings\n");
    printf("Which account would you like to use?\n");
    scanf("% c", &a);

    if (a == 'c' || a == 'C')
        {
            printf("Please enter the amount for deposit\n");
            scanf("% lf", &r);
            Initialamount = Initialamount + r;
            printf("Your chequing account balance is=%lf", Initialamount);
        }
    else
        if (a == 's' || a == 'S')
        {
            printf("Please enter the amount for deposit\n");
            scanf("%lf", &r);
            Initialamount = Initialamount + r;
            printf("Your chequing account balance is=%lf", Initialamount);
        }
        else

        printf("Invalid character\n");

} 

Upvotes: 1

Views: 45

Answers (2)

Mourad Karim
Mourad Karim

Reputation: 171

Your scanf is not correct, you have space "% c".

Upvotes: 0

Renzo
Renzo

Reputation: 27424

Remove the space in scanf:

scanf("%c", &a);

and also in the following scanf:

scanf("%lf", &r);

Upvotes: 1

Related Questions