My first program

Hey this is my first completed program. Very basic, but I tried to incorporate serveral aspects into it. Any advice/tips/errors you see for me to make improvement would be helpful. Thanks.

It's basically a finance tool to track/estimate your income.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//

#include "stdafx.h"
#include <cmath> //rounding function
#include <iomanip> // setprecision function
#include <algorithm> // transform function
#include <conio.h> // getch() function
#include <string> // string function
#include <iostream> 
using namespace std;

double intround(double);
double calcRaiseAmount(double, double);
double calcNewPay(double, double);
void monthlyGross();
void projGross();
void showTitle();
void getName(string &);
void chooseMenu();


int main()
{
	double currentpay, annualraises, annualpay, raiserate, raiselength, raiseamount, newpay, weeklyhours, weeklypay;
	char start, pay;
	int intro = 0;
	string name;
	
	if (intro == 0)
	{
		showTitle();
		getName(name);
		chooseMenu();
		intro = 1;
	}

start:
	if (intro == 1)
	{
		cout << "\nEnter current hourly pay.\n";
	}
	intro = 1;
	cin >> currentpay;
	cin.ignore(100, '\n');
	while (currentpay <= 0)
	{
		cout << "\nError: Inproper amount.\n";
		cout << "Enter current hourly pay.\n";
		cin >> currentpay;
		cin.ignore(100, '\n');
	}
	cout << "\nEnter raise percent (decimal format).\n";
	cin >> raiserate;
	cin.ignore(100, '\n');
	while (raiserate < 0)
	{
		cout << "\nError: Inproper amount";
		cout << "\nEnter raise percent (decimal format).\n";
		cin >> raiserate;
		cin.ignore(100, '\n');
	}
	raiseamount = calcRaiseAmount(currentpay, raiserate);
	newpay = calcNewPay(currentpay, raiserate);
	cout << fixed << setprecision(2) << "\nYour raise is " << raiseamount << " per hour.\n";
	cout << fixed << setprecision(2) << "\nYour new pay is " << newpay << " per hour.";
	cout << "\nEnter your hours per week.\n";
	cin >> weeklyhours;
	cin.ignore(100, '\n');
	while (weeklyhours <= 0)
	{
		cout << "\nError: Inproper amount.\n";
		cout << "Enter your hours per week.\n";
		cin >> weeklyhours;	
		cin.ignore(100, '\n');
	}
	cout << "\nEnter the amount of hours or weeks it takes you to obtain a raise.\n";
	cin >> raiselength;
	cin.ignore(100, '\n');
	if ((raiselength <= 52)&&(raiselength >=1))
	{
		annualraises = (52/raiselength);
	}
	else if ((raiselength > 52)&&(raiselength <=8760))
	{
		annualraises = ((weeklyhours*52)/raiselength);
	}
	else if (raiselength == 0)
	{
		annualraises = 0;
	}
	while ((raiselength < 0)||(raiselength > 8760))
	{
		cout << "\nError: Inproper amount.\nEnter the amount of hours or weeks it takes you to obtain a raise.\n";
		cin >> raiselength;
		cin.ignore(100, '\n');
	}
pay:
	cout << "\nWould you like to use (C)urrent pay or (N)ew pay?\n";
	pay = getch();
	switch(toupper(pay))
	{
	case 'C':
		for (;annualraises>1;annualraises--)
		{
			currentpay = ((currentpay*raiserate)+currentpay);
		}
			weeklypay = (weeklyhours*currentpay);
			annualpay = ((weeklyhours*52)*currentpay);	
			break;
	case 'N':
		for (;annualraises>1;annualraises--)
		{
			newpay = ((newpay*raiserate)+newpay);
		}
			weeklypay = (weeklyhours*newpay);
			annualpay = ((weeklyhours*52)*newpay);
			break;
	default:
		cout << "Input error";
		goto pay;
	}

	cout << fixed << setprecision(2) << "\n" << name << " made " << weeklypay << " dollars this week.";
	cout << fixed << setprecision(2) << "\n" << name << " made a total of " << annualpay << " for the year.";
	cout << "\nWould you like to continue? (Y)es or (N)o";
	start = getch();

	switch(toupper(start))
	{
case 'Y':
	goto start;
	break;
	}
	return 0;
}

void showTitle()
{
	cout << "********************\n********************\n********************\n";
	cout << "**** WELCOME TO ****\n**** XYRIENE'S  ****\n**** PAY CHART  ****\n";
	cout << "********************\n********************\n********************\n\n";
}

void chooseMenu()
{
	char choice;
choice:
	cout << "What would you like to do?\n";
	cout << "(H)ourly gross, (M)onthly gross, (P)rojected gross\n";
	choice = getch();
	switch(toupper(choice))
	{
		case 'M':
			monthlyGross();
			break;
		case 'P':
			projGross();
			break;
		default:
			cout << "Error: Invalid input";
			goto choice;
			break;
	}
}

void getName(string &user)
{
	cout << "Name: ";
	getline(cin, user, '\n');
	transform(user.begin(), user.end(), user.begin(), tolower);
}

	double intround(double r) {
	return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
	}

	double calcRaiseAmount(double currentpay, double raiserate)
	{	
		return (currentpay*raiserate);
	}
	
	double calcNewPay(double currentpay, double raiserate)
	{
		return (calcRaiseAmount(currentpay, raiserate) + currentpay);
	}

	void monthlyGross()
	{
			int x, y, intro = 0;
			string months[12] = {"Janurary", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
			double income[12] = {}, avgIncome, totlIncome;
			double percent[12] = {};
			
			cout << "\n\nEnter your income for the following months.\n";
			for (x=0;x<12;x++)
			{
				cout << months[x] << ": ";
				cin >> income[x];
				cin.ignore(100, '\n');
			}
		
			x=0;
			y=1;	
		for(x=0;y<12;x++)
		{
			if (income[x] > income[y])
			{
				percent[x] = (1-(income[y]/income[x]));
				cout << fixed << setprecision(2) << months[x] << "-" << months[y] << " you had a " << percent[x]*100 << "% decrease. ";
			}
			if (income[x] < income[y])
			{
				percent[x] = (income[y]/income[x]);
				cout << fixed << setprecision(2) << months[x] << "-" << months[y] << " you had a " << percent[x]*100 << "% increase. ";
			}
			if (income[x] == income[y])
			{
				percent[x] = 0;
				cout << fixed << setprecision(2) << months[x] << "-" << months[y] << " you had a " << percent[x]*100 << "% increase. ";
			}

				y = (y+1);
		}
			totlIncome = (income[0]+income[1]+income[2]+income[3]+income[4]+income[5]+income[6]+income[7]+income[8]+income[9]+income[10]+income[11]);
			avgIncome = (totlIncome/12);
			cout << fixed << setprecision(2) << "\n\nYour average monthly income is $" << avgIncome << ". \n";
			cout << fixed << setprecision(2) << "Your total annual income is $" << totlIncome << ".";
			int main();
	}
	
void projGross()
	{
		int x, y = 0;
		double incomes[12][3] = {};
		string  months[12] = {"Janurary", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
		cout << "\nEnter your current income and your projected income for each month.\n";
			for (x = 0; x < 12; x++)
			{
				y = y++;
				cout << months[x] << ": ";
				cin >> incomes[x][0];
				cin >> incomes[x][1];
			}

			for (x = 0; x < 12; x++)
			{
				if (incomes[x][0] > incomes[x][1])
				{
					incomes[x][2] = ((incomes[x][1]/incomes[x][0])*100);
					cout << fixed << setprecision(2) << "\nYou have met your goal for the month of " << months[x] << " by " << incomes[x][2] << "%.\n";
				}
				if (incomes[x][0] < incomes[x][1])
				{
					incomes[x][2] = ((1-(incomes[x][0]/incomes[x][1]))*100);
					cout << fixed << setprecision(2) << "\nYou have not yet met your goal for the month of " << months[x] << " by " << incomes[x][2] << "%.\n";
				}
				if (incomes[x][0] == incomes[x][1])
				{
					incomes[x][2] = 100;
					cout << fixed << setprecision(2) << "\nYou have met your goal for the month of " << months[x] << " by " << incomes[x][2] << "%.\n";
				}
			}
			int main();
	}
Last edited on
You should get rid of stdafx.h - you need to change project type, on VC++ start with an Empty Project -
I strongly recommend to remove conio.h
Your indentation is not always consistent

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
void chooseMenu()
{
	char choice;
choice:  // don't reuse the same identifier for different things
	cout << "What would you like to do?\n";
	cout << "(H)ourly gross, (M)onthly gross, (P)rojected gross\n";
	choice = getch(); // you can replace this with  cin >> choice;
	switch(toupper(choice))
	{
		case 'M':
			monthlyGross();
			break;
		case 'P':
			projGross();
			break;
		default:
			cout << "Error: Invalid input";
			goto choice; // you can replace the goto with a loop
			break;
	}
}


262
263
264
			}
			int main(); // what is this doing here?
	}

Last edited on
Every time you have a label: and a goto, you can replace all that stuff with a function which is called from a loop.

Also, as Bazzy already noted, I'm not sure what you are doing on line 263. If you are trying to recurse on main, please know that that is very bad. Don't do that. (As it is currently, you are declaring a local prototype for main(), which does nothing.)

The <conio.h> functions aren't so horrible, except that they only exist in old 16-bit DOS compilers, and are only emulated (with varying degrees of success) in modern compilers, if at all.


Otherwise, you have made a good start. Keep programming dude!
Hey thanks guys, I got rid of the <conio.h> and replaced my "x" = getch() with cin >> "x".

As for the int main() I was thinking that i needed to call back to the main function although I had started off there so my bad. So I removed those.

Pretty much I fixed up everything you guys mentioned except replacing goto with a loop I don't quite get how that would work.
The reason that you use goto, based on my examination, is so that you can start again right? So you can get back to where you were. This is wasteful and poor use of goto (which in general should not be used; it was phased out a while back).
The solution is to wrap the whole thing (from where you placed the label, all the way down to where you gotoed to the label) in a loop. You go around the loop again based on the same condition as that which controlled your goto-ing. Example
1
2
3
4
5
6
7
cout << "Hey!";
int goagain;
start:
cout << "now it's starting!";
cin >> goagain;
if (goagain == 1)
    goto start;

This code uses goto to restart based on a condition. So you could replace that with an acceptable programming construct like this:
1
2
3
4
5
6
7
cout << "Hey!";
int goagain;
do
{
    cout << "now it's starting!";
    cin >> goagain;
while (goagain == 1);

The functionality is about the same but it's much easier to follow because, instead of using the messy spaghettifying goto keyword, a more efficient and reliable loop construct was used.
Last edited on
The goto statement was never "phased out" -- it hasn't even been deprecated.

Goto hatred is part of the "structured programming" paradigm that occurred back in the 70's to get programmers to use smart things like loops and subroutines instead of jumping all over the place.

(Most of you have never seen true spaghetti code. Trust me.)
Well I don't mean that it was literally phased out. It's still a keyword. Its use was discouraged in the beginning days of the structured paradigm, to the point where it is effectively never used. (I think Java's treatment of goto is better; as a language-reserved word but not actually a command. Goto is just so difficult to use well that it's best left untaught or unavailable.)
And I thank whatever code gods are up there when I remember that I've never seen true spaghetti-worthy code; one of the few joys of our dark world.
Alright I really appreciate the advice and I replaced the "goto"'s with some loops and I cleaned up my indention a little better.
I also moved most of my function in int main() to it's own separate function.
Here's a pastebin link for those interested so I don't take up too much space on this page.

http://pastebin.com/m3a630d51
Glad to be of assistance.
(I've said that a total of three or four times now across various threads.)
Topic archived. No new replies allowed.