While Loop Add/Subtract from Balance

I'm working on a code for a class to add and subtract from the balance based on user inputting D for Deposit, W for withdraw, or Q to quit. I'm very new to C++ and can't figure out what I'm missing to get it to function correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
	double balance = 0;
	double amount;
	char transCode;

	cout << endl << "Deposit Withdraw Quit - D/W/Q: ";
	cin  >> transCode;
	transCode = toupper(transCode);

	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.";
            }

	cout << endl << balance;

	cout << endl;

	return 0;
}
Line 15,21: You're going to prompt the user twice for the transcode the first time through the loop.

Line 28, 28: You have to ask the user how he wants to deposit or withdraw. You have no cin statement.

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;
Hello Etnies,

Consider this as a start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;  // <--- Best not to use.

int main()
{
    double balance{};
    double amount{};
    char transCode{};

    std::cout << std::fixed << std::setprecision(2);  // <--- Only needs done once.
    
    while (transCode != 'Q')
    {
        cout << "\n Deposit Withdraw Quit - D/W/Q: ";
        cin >> transCode;

        transCode = static_cast<char>(std::toupper(static_cast<unsigned char>(transCode)));

        if (transCode == 'D')
        {
            cout << "\n   Amount to  Deposit: ";
            std::cin >> amount;

            cout << "\n   New balance = " << (balance += amount) << '\n';  // <--- () needed.
        }

        else if (transCode == 'W')
            cout << "\n   Amount to Withdraw:   " << (balance -= amount);

        else if (transCode == 'Q')
            continue;

        else
           cout << "\n     Transaction code entered is incorrect.\n";
    }

    cout << '\n' << balance;

    cout << '\n';

    return 0;  // <--- Not required, but makes a good break point for testing.
}

My apologies. I forgot the part in bold because the rest of the line is just a past in.

There is another way to write the while loop that I have to work up 1st.

Andy
Hello Etnies,

While working on the program this is another idea I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (std::cout << "\n Deposit Withdraw Quit - D/W/Q: " && std::cin >> transCode)
{
    //cout << "\n Deposit Withdraw Quit - D/W/Q: ";
    //cin >> transCode;

    transCode = static_cast<char>(std::toupper(static_cast<unsigned char>(transCode)));

    if (transCode == 'D')
    {
        while (std::cout << "\n   Amount to  Deposit: " && std::cin >> amount && amount <= 0.00)
        {
            std::cerr << "\n     Deposit amount can not be less than or equal to 0.00.\n";
        }

        std::cout << "\n   New balance = " << (balance += amount) << '\n';  // <--- () needed.
    }

You can set up the else if for "W" the same way.

If you have studied "switch/case" you can replace the if/else if and else with the switch if you want.

To use this while condition you will also have to change the else if for "Q" from "continue" to "break".

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

using namespace std;

int main()
{
    bool keep_going{true};
    
    double balance = 0;
    double amount;
    char transCode;
    
    while (keep_going == true)
    {
        amount = 0;
        
        cout << endl << "Deposit Withdraw Quit - D/W/Q: ";
        cin >> transCode;
        transCode = toupper(transCode);
        
        if (transCode == 'D')
        {
            cout << endl << "   Amount to  Deposit:   ";
            cin >> amount;
            balance += amount;
            keep_going = true;
        }
        else if (transCode == 'W')
        {
            cout << endl << "   Amount to Withdraw:   ";
            cin >> amount;
            
            if(amount <= balance)
            {
                balance -= amount;
            }
            else
            {
                cout << "There is not enough in your account\n";
            }
            keep_going = true;
        }
        else if (transCode == 'Q')
        {
            keep_going = false;
        }
        else
        {
            cout << endl << "Transaction code entered is incorrect\n";
            keep_going = true;
        }
        
        cout << "Current balance $" << balance << '\n';
    }
    
    return 0;
}



Deposit Withdraw Quit - D/W/Q: d

   Amount to  Deposit:   99
Current balance $99

Deposit Withdraw Quit - D/W/Q: w

   Amount to Withdraw:   96
Current balance $3

Deposit Withdraw Quit - D/W/Q: d

   Amount to  Deposit:   7
Current balance $10

Deposit Withdraw Quit - D/W/Q: w

   Amount to Withdraw:   6
Current balance $4

Deposit Withdraw Quit - D/W/Q: u

Transaction code entered is incorrect
Current balance $4

Deposit Withdraw Quit - D/W/Q: q
Current balance $4
Program ended with exit code: 0
Topic archived. No new replies allowed.