Christian M.

asked • 02/10/22

Endless C loop for input

This is the code I've worked on until now, when inputting large decimals, eg 8347.93, loop begins, any assistance to resolve this problem would be appreciated


#include <stdio.h>

int main(void) {

while (1) {

int client_id;

double bal;

char type, choice1;

double amount;

double deposit = 0;

double withdraw = 0;

printf("Enter the Client ID\n");

scanf("%d", &client_id);


printf("Enter the balance\n");

scanf("%lf", &bal);


while (1) {

printf("Enter the type of transaction\n'D' for deposit\n'W' for withdraw\n");

scanf("%c", &type);


if ((type == 'W') || (type == 'w')) {

printf("Enter the amount to withdraw: \n");

scanf("%lf", &amount);


withdraw = withdraw + amount;

bal = bal - amount;


}

else if ((type == 'D') || (type == 'd')) {

printf("Enter the amount to deposit: \n");

scanf("%lf", &amount);

deposit = deposit + amount;

bal = bal + amount;


}

else {

printf("Invalid input");

continue;

}

printf("If you want to do another transaction click 'Y'/'y' \n");

printf("If you want to exit, click 'N'/'n' \n");

scanf("%c", &choice1);

if ((choice1 == 'Y') || (choice1 == 'y')) {

continue;

}

else if ((choice1 == 'N') || (choice1 == 'n')) {

break;

}

else {

printf("Invalid Input");


}

}

printf("Total Amount Deposited = %lf\n", deposit);

printf("Total Amount Withdrawn = %lf\n", withdraw);

printf("Final Balance = %lf\n", bal);

printf("Client ID = %d\n", client_id);


//break;

}


return 0;

}


1 Expert Answer

By:

Chris T. answered • 02/11/22

Tutor
0 (0)

Senior Engineer @ Google | Former TA in operating systems

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.