Lab Assignment

i need help with my code and i am so stuck and i don't where my errors, i seriously need help fixing it, it supposed to be a program that displays what your balance is,your deposits, and withdrawals. i am also supposed to include double getAmount(string msg) and double getAmount(string msg, double balance), but I can't see where to put it.



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 "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int input;
const  int TOTAL_MONTHS = 3;




int main()

{
	double getAmount;
	double totalDeposit;
	double totalWithdrawal;
	double currentBalance;
	double startingBalance;
	double balance;
	double withdrawal;
	double deposit;
	double totalBalance;
	double finalBalance;

	cout << "Enter your current Balance";
	cin >> startingBalance;
	cout << "Enter the amount you want to deposit.\n";
		cin >> deposit;
	for (input = 1; input <= TOTAL_MONTHS; input++)
	{
	while (deposit < 0)
	{
		cout << "Sorry, amount invalid, try again.\n" << endl;
		cin >> deposit;
	}
	cout << "Please enter the current withdrawal for the month\n";
	cin >> withdrawal;
	while (withdrawal < 0 || withdrawal > startingBalance)
	{
		cout << "Sorry, amount invalid, your withdrawal can not be      greater than your current balance.Please try again.\n";
		cin >> withdrawal;
	}
	totalBalance = startingBalance - finalBalance;
	startingBalance = totalBalance + deposit;
	finalBalance = totalBalance - withdrawal;
	
}
	cout << "Here is your starting balance at the beginning of the three-month period." << startingBalance
	<< endl;
	cout << "Here is your total deposit during the three month period" << deposit << endl;
	cout << " Here is your total withdrawals" << withdrawal << endl;
	cout << "And your final balance at the end of the three-month period" << totalBalance << endl;
	system("pause");
	return 0;
}


Last edited on
closed account (48T7M4Gy)
It appears you have an uncontrolled loop regarding withdrawals.

Any errors along these lines will show up by referring back to your pseudocode or other schematic plan for this project. Perhaps you'd like to show us that plan and we can go through it with you so you can make the changes to your code.


But you really need to describe the problem you are having because our mind-readers are away again today. :)
closed account (48T7M4Gy)
It appears that the loop going out of control is because you are not resetting the deposits and withdrawals to an acceptable value for the additional loops through the while structures. They are using values you have left from the previous month.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for (input = 1; input <= TOTAL_MONTHS; input++)
    {
        while ( depositAccepted == false )
        {
            cout << "Month " << input << "  Enter the amount you want to deposit: ";
            cin >> deposit;
            
            if ( deposit > 0 )
                depositAccepted = true;
            else
            {
                cout << "Sorry, amount invalid, try again.\n" << endl;
                depositAccepted = false;
            }
        }
        depositAccepted = false;
        balance += deposit;
        totalDeposit += deposit;
what I planned on doing with the program was for it to display my options correctly once without it skipping any lines a or repeating the option several times, seems I have messed up at either my my loop function or initializing my variables properly. Also for some reason, I don't why the program requires me to put in two deposits instead of one when it ask for "enter current deposit for the month"? When I made my loop the first time, it didn't react this way. I guess I may have switch some things from the loop.
closed account (48T7M4Gy)
If you look very carefully at the code extract I've posted above you will be able to extend it to withdrawals and complete the for loop. It is very nearly a cut and paste job to incorporate it with your code.
Oh, ok and the variables I have initialized in the code, are they correct or do I need to work on those?
closed account (48T7M4Gy)
Try it ...
oh i even forgot, how can I add dollar values to my code? i really dont want to use cout, if there is a function that can apply to my entire code, but if not then how will i display it in cout.
closed account (48T7M4Gy)
Sounds like a job for printf() functionality if you must.
hmmm ill try it, if doesn't work out, then i guess I have to go the long method. Thank you.
closed account (48T7M4Gy)
Well done. There is no harm in trying. Good luck.
Topic archived. No new replies allowed.