Console Application Advice

Hi all,

I'm new here and new to C++, probably spent two weeks learning through things and have tried to make my own console application from the ground up as I was told it was the best way to learn.

I started my console application a few days ago trying to put what knowledge I have to use, the application returns the accurate results I'm wanting, but as I'm new to C++ I would like an overview of my code to see how I can Improve it or what I have done wrong so I can cut out bad habits in the early stages, or introduce new ways of doing things.

Any feedback would be great, and please bare in mind I'm new to this so don't be too harsh on my first effort :)

Kind Regards

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
#include <iostream>

using namespace std;


int main() {
	double fence_spacing;
	double u_perim;
	cout << "Moduteq L2W Calculator\n" << endl;
	cout << "Please Enter the site Perimeter in Meters: " << endl;
	cin >> u_perim;
	cout << "Please enter the Sensor Spacing for the perimeter: " << endl;
	cin >> fence_spacing;
	cout << "Number of D221's Required " << endl;
	double l2w = (double)u_perim / (double)fence_spacing;
	int y = static_cast<int>(l2w+1);
	cout << y << endl;

	// Fiber Zones
	int f_zones;
	int d250t = 1;
	int d250r = 1;
	cout << "How many Fiber Zones Required?\n"; cin >> f_zones;
	cout << "D250 Units Required" << endl;
	cout << "D250T \t" << d250t * f_zones << endl;
	cout << "D250R \t" << d250r * f_zones << endl;

	int d250t_combined = (int)d250t * (int)f_zones + (int)y;
	int d250r_combined = (int)d250r * (int)f_zones;
	cout << "Update Slave Count" << endl;
	cout << d250t_combined + d250r_combined << endl;


	int max_sensor = 32;
	int total = (int)y / (int)max_sensor +1;
	cout << "Number of L2W's Required" << endl;
	cout << total << endl;

	// PSU LPI & LC
	int max_range = 401;
	double PLL = int(u_perim) / int(max_range);
	int update = static_cast<int>(PLL +1);
	cout << "PSU Required" << endl;
	cout << update / update +1  << endl;
	cout << "LPI Required" << endl;
	cout << update / update + 1 << endl;
	cout << "LC Required" << endl;
	cout << update / update + 1 << endl;
	cin.get();
	return 0;
}
You should test user/file inputs for consistency and for errors! F.e. your nice process fails, if it gets something other as a number as input in line 11. Give him/her a chance to retry or at least terminate the process regulary by printing an appropriate message.

Some minor tips:

Be as accurate and restrictive as possible.

Declare objects which should never change their values as const. F.e.: const int d250t = 1;

Even if C/C++ has a really weak type system try to be as restrictive as possible. Use unsigned int where variables couldn't/shouldn't have negative values. (This may be especially important for array indices.)

Avoid casting if possible.

F.e.: If an object is just an int you don't need to cast it again to an int.

Mostly there might be something wrong in your design if you have to cast values.

Minor minor tip.

I think this cin.get() statement at line 49 has something to do with MS-Windows limitations? To be somewhat more portable you may want to give the user a hint, why the program doesn't terminate. That he/she has to hit any key.
Thanks for the Feedback means alot! I only added the cin.get() statement to stop the console from shutting down once the program has finished during testing.

I'll make the suggested changes!
Consider not having a using namespace std; directive at the global namespace scope.
http://www.cplusplus.com/forum/general/72248/
Thanks!
Topic archived. No new replies allowed.