pseudo code Help

I just started a c++ class in college and with this whole craziness its been hard to really learn pseudo-code can anyone help me translate this for HW

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  int main()
{


	float AIR, MIR, months,

		deposit, withdraw, monthlyInterest,

		balance, depositSum = 0,

		withdrawSum = 0, interestSum = 0;

	ifstream inputFile;
	ofstream outputFile;


	inputFile.open("SavingsAccountBalanceSpaces.txt");
	outputFile.open("SavingsAccountBalancesSpacesoutputFile.txt");



	if (!inputFile)
	{
		cout << "Error!" << endl;
	}

	else
	{

		inputFile >> AIR >> balance >> months;
		MIR = AIR / 12;
		AIR /= 100;


		outputFile << "Report for Savings Account\n";
		outputFile << "-----------------------------------------\n";


		outputFile << "Annual Interest Rate (AIR)  : " << setw(10) << (AIR * 100) << "%" << endl;
		outputFile << "Monthly Interest Rate (MIR) : " << setw(11) << "(AIR/12)" << endl;
		outputFile << "Number of Months            : " << setw(11) << months << endl;
		outputFile << "Starting Balance (SB)       : " << setw(11) << showpoint << balance << endl;
		outputFile << "\nMonth" << "\t\tMonthly Beginning Balance (MBB)" << "\t\tDeposits (D)" << "\t\tWithdrawals (W)";
		outputFile << "\t\tBalance After Transactions (BAT)" << "\t\tInterest Amount (IA)" << "\t\tEnding Balance (EB)" << endl;
		outputFile << "-----------------------------------------------------------------------------------------------------------";
		outputFile << "-----------------------------------------------------------------------------------------------------------\n\n";

		inputFile >> deposit >> withdraw;

		while (inputFile)
		{
			for (int counter = 0; counter < months; counter++)
			{

				outputFile << setprecision(2);

				outputFile << counter + 1;
				outputFile << setw(20) << "$" << setw(15) << balance;
				outputFile << setw(30) << deposit;
				outputFile << setw(25) << withdraw << setw(20);

				balance += deposit;
				depositSum += deposit;

				balance -= withdraw;
				withdrawSum += withdraw;

				outputFile << "$" << setw(15) << balance << setw(30);

				monthlyInterest = balance * MIR;
				balance += monthlyInterest;
				interestSum += monthlyInterest;

				outputFile << "$" << setw(15) << monthlyInterest << setw(15);
				outputFile << "$" << setw(10) << balance << endl;

				inputFile >> deposit >> withdraw;

			}
		}


		outputFile << "-----------------------------------------------------------------------------------------------------------";
		outputFile << "Account Balance (AB)" << setw(9) << "$" << setw(15) << balance << endl;
		outputFile << "Year to Date (YTOD)" << setw(47) << depositSum << setw(25) << withdrawSum;
		outputFile << setw(65) << "$" << setw(15) << interestSum;

		cout << "An output file containing a report for this savings account has been created." << endl;
	}

	inputFile.close();
	outputFile.close();



	return 0;
}
Hello Weechi,

I am not the best at writing pseudo-code, but the general idea is to use plain English, or whatever you language may be, to explain what needs to be done.

Going from a working program to pseudo-code is easy because you just have to explain what you did in the program.

To get you started:

define "main"

define variables. "double" is the preferred floating point type, but a "float" may work if your decimal number is small.

Define file streams.

open input file.
check if open.
   if (!inputFile)
      print error message.
      return 1;  // <--- Leave program and fix problem.
   "else" block not needed.

open output file.
  Check not needed unless you use a path to the file.

Give the rest a try and see what you come up with.


The "else" block is not needed just the code because if you leave the program in the if block the else block is not reached.

If you bypass the if block then you would continue with the program.

Just because you have an if statement does not mean that you always have to have an "else" to go with.

Another point is is usually a good idea to initialize your variables when defined.E.G., :[code]double AIR{}, MIR{}, months{}, [/ code]. The empty{}, the uniform initializer, will set the variables to "0.0". Or you could put a number inside the{}s if you need to give it a value to start with.The{}s are available from C++11 standards on.

Andy
Hello Weechi,

It would be helpful if you could provide the input, so that everyone can use the same information.

I do see some problems with the program, but without the input it is hard to test.

Still working on what I believe could be done better and the comments.

Andy
Topic archived. No new replies allowed.