Hello Etnies,
Thank you for using code tags.
Given this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
while (transCode != 'Q')
{
cout << endl << "Deposit Withdraw Quit - D/W/Q: ";
cin >> transCode;
transCode = toupper(transCode);
if (transCode == 'D')
cout << endl << " Amount to Deposit: " << balance = balance + amount;
else if (transCode == 'W')
cout << endl << " Amount to Withdraw: " << balance = balance - amount;
else
cout << endl << "Transaction code entered is incorrect.";
}
|
What is the value of "balance" and where does it change?
What is the value of "amount" and where does it change?
Some things that you can improve on"
1 2 3 4 5 6 7 8 9
|
transCode = toupper(transCode);
// Should be written as:
static_cast<char>(std::toupper(static_cast<unsigned char>(transCode)));
balance = balance + amount
// can be written as:
balance +=amount and (-=)
|
Most places you can use the new line, (\n), in place of (endl).
But first you need to get a value for "balance" and "amount" before the if statements will work.
Andy
You should initialize all your variables as in:
1 2 3
|
double balance{};
double amount{};
char transCode{};
|
The empty {}s will allow the compiler to initialize the variables to (0)zero based on the variables type. "std::string"s are empty when defined and do not need initialized.
Initializing "transCode" will satisfy the while condition making it true thus you only need 1 prompt and input.
You would need to add:
1 2
|
else if (transCode == 'Q')
continue;
|