C++: Loops within Conditionals


#1

My below code is not working as designed. It appears that my if condition works but the loops inside this condition are not adhering to the if condition ~ skipped if you will. The first two while loops are skipped despite the conditional statements. The third loop is processed no matter what. I get the desired output via pointer but the while loop is designed to check for if a number entered is invalid. Please help as I do not know why this is invalid.

/*Withdraw a prompted amount of money from selected account type via * account_balance pointer*/
void Withdraw(double *account_balancePtr, char account_type)
{
double withdraw;
double *balance_holder = account_balancePtr;

printf("Enter WITHDRAWAL amount: $");
scanf("%lf", &withdraw);
if (account_type == "C")
{
while (withdraw > *balance_holder)
{
printf("Insufficient Withdrawal Amount. Please withdrawal no more than checking balance of %.2lf.", *balance_holder);
printf("Enter WITHDRAWAL amount: $");
scanf("%lf", &withdraw);
}

*account_balancePtr = *balance_holder - withdraw;
}
else if (account_type == "S")
{
while (withdraw > *balance_holder)
{
printf("Insufficient Withdrawal Amount. Please withdrawal no more than savings balance of %.2lf.", *balance_holder);
printf("Enter WITHDRAWAL amount: $");
scanf("%lf", &withdraw);
}

*account_balancePtr = *balance_holder - withdraw;
}
else
{
while (withdraw > (abs(MAXCREDIT) + *balance_holder))
{
printf("Insufficient Withdrawal Amount. Please withdrawal no more than maximum avaiable credit of %.2lf.", (abs(MAXCREDIT) + *balance_holder));
printf("Enter WITHDRAWAL amount: $");
scanf("%lf", &withdraw);
}

*account_balancePtr = *balance_holder - withdraw;
}

}

#2

That doesn’t compile with my compiler’s default settings (gcc 7.3.0), and I imagine that your compiler shows a bunch of warnings or at least that it would if you enabled warnings. (Not much point testing code that compiles with warnings, fix those first)

Oh and, your function would be simpler if it didn’t use pointers (just send in the number) and returned the result instead of modifying the parameter.