Help with writing code

I have my code complete pretty much but the last part is that I need to have a counter for how many candidates were accepted into the program and the percentage.

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



 int main()

{

		// INPUT: The users input information.

	 	char gender;
	 	bool heightOk;
	 	bool weightOk;
	 	int height;
	 	int weight;





		// PROCESSING: Entering the users information for qualification.

		cout << "Please enter the candidates information (Enter 'x' to exit) " << endl;
		do
		{
			cout << setw(10) << "Gender: ";
			cin.get(gender);
			cin.ignore(1000, '\n');

			if (gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M') {
				cout << "***** Invalid gender; please enter M or F *****" << endl;
			}


		}while(gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M');




		while(gender != 'x' || gender != 'X')
		{

			if (gender == 'f' || gender == 'F') {

			do
			{
				cout << setw(10) << "height: ";
				cin >> height;
				heightOk = (height >=62 && height <= 75);


			}while(height >=25 && height <= 110);
				cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****" << endl;

			do
			{
				cout << setw(10) << "weight: ";
				cin >> weight;
				weightOk = (weight >= 110 && weight <= 185);

			}while(weight >= 50 && weight <= 1400);
				cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400. *****" << endl;

			}
			else if(gender == 'm' || gender == 'M')
			{

			do
			{

				cout << setw(10) << "height: ";
				cin >> height;
				heightOk = (height >=65 && height <= 80);

			}while(height >=25 && height <= 110);
			 cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****" << endl;

			do
			{
				cout << setw(10) << "weight: ";
				cin >> weight;
				weightOk = (weight >= 130 && weight <= 250);
				cin.ignore(1000, '\n');

			}while(weight >= 50 && weight <= 1400);
				cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400. *****" << endl;
			}


			if (heightOk == 1 && weightOk == 1) {

				cout << "\n\nThis candidate has been ACCEPTED!" << endl << endl;}

			counter = counter +1+1

			else if(heightOk == 0 && weightOk == 1) {


				cout << " \nThis candidate has been rejected based on the HEIGHT requirement" << endl;}

			else if(heightOk == 1 && weightOk == 0) {

				cout << "\nThis candidate has been rejected based on the Weight requirement" << endl;}

			else if (heightOk == 0 && weightOk == 0) {

				cout << "\nThis candidate has been rejected based on the Weight and HEIGHT requirement" << endl;}


			cout << "\n\n\nPlease enter the candidates information (Enter 'x' to exit) " << endl;

			do
			{

				cout << setw(10) << "Gender: ";
				cin.get(gender);
				cin.ignore(1000, '\n');

				if (gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M') {
					cout << "invalid entry" << endl;
				}


			}while(gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M');



		}



 	 	// OUTPUT: Details of what is being output.

		return 0;

}


Last edited on
closed account (oN3AqMoL)
And your question is..... Are we supposed to write you a counter or something........


I hope thats not what this post was about because this is not FREECODEWRITERS.org.
Last edited on
in the relevant loop place counter++ which has been previously initialized to zero. It will keep track of the number of loops.
}while(gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M');

I just want to put forward the idea that I personally hate constructs like this. You notice that the tests are repeated throughout the code a number times. And it is harder to extend.

It is much better to use a switch to do this, and make use of the toupper function so you don't have to test for 'm' & 'M' for example. Each case clause in the switch should call a function which deals with that case.

I also personally hate do loops, and only use them if I really have to (which is rare) but that is just my opinion.

This will tidy up your code considerably.

Also, when using bool variables, you can use true or false instead of 1 or 0.

Hope this helps.
Last edited on
I'm sorry I just noticed I didnt ask the question properly....Well for this assignment since its for class I have to construct it like this with the loops. And my question is pretty much I know where to include the counter I just am not sure because this program outputs the number of accepted candidates and the percentage
Well for this assignment since its for class I have to construct it like this with the loops.


Are you sure? I am sure you could get better marks if you structured it differently.
Yes because I went over this with the lab aides that have taken the course and it was correct
Yes because I went over this with the lab aides that have taken the course and it was correct


Maybe I should have been more blunt: Your code is a bit messy IMO. I think it is a poor show if your lab aides are saying it was correct. Remember there is difference between code that works and code that is elegant. Maybe they don't have much time, and anything that works is OK. The other thing is interpretation, they may have said to use loops, but I would not have imagined something like what you have.

My main criticism is the repeated tests. Lines 26 to 37 gets the gender with an ugly test, and these tests are repeated throughout the code. If you didn't want to or not allowed to use a switch, you could use else if instead, and have each else if call a function to process that option. With loops, make use of a bool variable to control the loop. This avoids the repeated tests and makes the code much cleaner.

I am confused about this:

1
2
3
	weightOk = (weight >= 110 && weight <= 185);

			}while(weight >= 50 && weight <= 1400);


The condition for weightOk is different from that of the do loop - why not make use of the weightOk variable in the do loop condition? That way the loop could be a while loop. I always try to avoid do loops whenever I can.

As I said earlier these sorts of tests are ugly and not scalable:

}while(gender != 'x' && gender != 'X' && gender != 'F' && gender != 'f' && gender != 'm' && gender != 'M');

Also make use of the toupper function so you don't have to make 2 tests instead of one. Or in a switch:

1
2
3
case 'm':
case'M':
     ProcessMale();


Even better is to have a menu with numerical options, that avoids the testing for upper / lower case altogether.

If I was a teacher I would allocate about 60% of the marks for well organised, elegant, easy to read code with functions. So if someone wrote a program that worked perfectly, but the code was an unintelligible mess to read - they would get a 40% score.

This sounds harsh, but compare it to communication skills - which should always be clear and simple as possible. It is the same for programming.

I hope this helps - I don't know whether it is too late for this assignment - even so, good advice for the future I hope.
Topic archived. No new replies allowed.