Your Opinion and Feedback

Feb 19, 2013 at 7:16am
Hello all. I am in my 6th week of my C++ Programming class. We were given the "Bumpkins of Bumpus" assignment. This is my completed program:
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
//KHWGW
//2/19/2013
//CIST2361
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

float height_conversion (float, char);

int main()
{
	float dHeight,	
		bHeight;	

	int numBump,	
		numDoors;	

	char dUnit,		
		bUnit;		
	string bumpName,
		fileName;	
	fstream iFile;	


	iFile.open("bumpkin.in");

	while(!iFile)
	{
		cout << "File destination invalid, please input file destination: ";
		cin >> fileName;
		iFile.open(fileName);
	}

	
	iFile >> numBump;
	
	
	for(int i = 1; i <=numBump; i++)
	{
		iFile >> bumpName >> numDoors >> bHeight >> bUnit;

		bHeight = height_conversion (bHeight, bUnit);

		cout << bumpName << endl;
		cout << "--------\n";

		for(int c = 1; c <= numDoors; c++)
		{
			iFile >> dHeight >> dUnit;

			dHeight = height_conversion(dHeight, dUnit);

			cout << "Door " << c << ": ";

			if (dHeight > bHeight * 1.25)
				cout << "Stilts\n";
			else if (dHeight > bHeight * 1.05)
				cout << "Walk\n";
			else if (dHeight > bHeight * 0.65)
				cout << "Duck\n";
			else if (dHeight > bHeight * 0.40)
				cout << "Crawl\n";
			else if (dHeight > bHeight * 0.25)
				cout << "Limbo\n";
			else
				cout << "Blocked\n";
		}

		cout << endl;
	}

	iFile.close();

	system("pause");
	return 0;

}

float height_conversion(float height, char unit)
{
	switch (unit)
	{
		case 'c': ;
			break;
		case 'i': height *= 2.54;
			break;
		case 'f': height *= 30.48;
			break;
		case 'y': height *= 91.44;
			break;
		case 'm': height *= 100;
			break;
	}
}


The header file bumpkin.in contains:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3
Mookin 3 150.4 c
75 i
2 f
151 c
Kimkin 2 67.3 i
204.5 c
1000 c
Lumkin 5 43.1 i
12 i
44 i
25 i
35 i
60 i


I also have a completely commented version if that would help people to understand it better. I can post the assignment itself too. This is my first post, so if i didn't format something correctly please let me know so i can fix it and know for future posts.
Thank you in advance,
Karl-Heinz
Feb 19, 2013 at 8:15am
line 32, I prefer to use getline(cin, str...) instead of just cin because it sometimes acts wonky.

Other than that, the only other thing that is really not a big deal, but I will go ahead and point it out is using zero as starting point in for-loops instead of 1. Like I said it is not a big deal, but if you are working with arrays and storing values in memory, the first memory address is zero and so if you start at 1, you might miss something or in some cases run into a segmentation fault.
Feb 19, 2013 at 1:34pm
Your height_conversion function doesn't return anything - that ought to have attracted a warning from the compiler. Make sure you have all the warnings turned on in your compiler.

I use gcc with -Wall -Wextra -pedantic

I could even use -Werror which promotes warnings to errors.

Also, always put a default clause in your switch - this will allow you to handle any bad input.

As Smac89 was saying, the idiom for doing something n times is:

1
2
3
for (int a = 0;a < n;a++) {
//do something
}


HTH
Feb 19, 2013 at 11:20pm
Thank you both for the feedback, it was very helpful. When setting the counter in the for loop to 0, would i then just use:
 
cout << "Door " << c + 1 << ": ";


or is there a better way of doing that?
Feb 20, 2013 at 1:31am
That's how I would do it.
Topic archived. No new replies allowed.