I separated my program mainly into two parts. -One is the function which I used 'if' in a 'while' loop,
- one is the main body which is still a 'while' loop but the inside is a switch statement
When I first input d, the main body activates the function and asks for a number. My goal is when the user input a positive value the function will exit and continue to read the code below. However, the 'break' could never be read and formed a loop to ask the amount of money again and again.
I tried to make the function on a separate page that is not within the 'switch' statement, it works! But just doesn't when I insert it in the 'switch' statement.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int GetTransactionAmount()
{
double transactionAmount;
while(1)
{
printf("What is the amount of money you would like to deposit/withdraw?\n $");
scanf("%lf",&transactionAmount);
if (transactionAmount <=0)
{
printf("ERROR! Please input a valid number above 0\n");
continue;
}
else
{
break;
}
}
return transactionAmount;
}
int main()
{
char action;
double totalDeposits, totalWithdrawal, transactionAmount;
printf("If you would like to deposit, please input D \n");
printf("If you would like to withdraw, please input W \n");
scanf("%c", &action);
while(1) {
if(action == 'D'||action == 'd'||action == 'W'|| action =='w')
{
switch(action)
{
case 'D':
case 'd':
GetTransactionAmount();
break;
case 'W':
case 'w':
GetTransactionAmount();
break;
}
//printf("c");
}
else
{
printf("invalid input");
break;
}
//printf("d");
}
//getch();
return 0;
}
I tried to make the function on a separate page that is not within the 'switch' statement, it works! But just doesn't when I insert it in the 'switch' statement.