Am I Over Commenting?

Am I Over-Commenting, here is a quick snapshot of a BlackJack program I have written, I was just commenting the code when I asked myself, am I over commenting? I am currently doing GCSE computing so I need some comments to make everyone understand what I'm writing, is it extraneous?

Note: I didn't include the header because I thought it wasn't necessary

Thanks for any help, Ctrlbadger

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
 #include "BlackJack v2.h"

int main() {
	// Create Vector with the players, using vec because of an undiefined amount of players
	std::vector<Players> VecPlayers;

	// StrLine for entering players  
	std::string StrLine = "";
	// Used as an index for VecPlayers
	unsigned int CountPlayer = 0;

	// Give players that are playing then add them to the vector
	std::cout << "Please give the players.\n: ";
	while (std::cin >> StrLine) {
		VecPlayers.push_back(Players());
		VecPlayers[CountPlayer].Name = StrLine;
		CountPlayer++;
		std::cout << ": ";
	}

	// Clear the input buffer
	std::cin.ignore('/n');
	std::cin.clear();

	// Reset CountPlay so that it starts at the first index value
	CountPlayer = 0;
	// Answer used for user input
	char Answer = 'N';

	do {
		// Seed for RNG
		srand(int(time(NULL)));
		// Start the turn of the player and say his name
		std::cout << std::string(100, '\n') << VecPlayers[CountPlayer].Name << "'s Turn: ";
		// Press enter to continue
		std::cin.ignore();

		// Give the player two cards
		VecPlayers[CountPlayer] = AddCard(VecPlayers[CountPlayer], 2);

		// Show the player the cards
		DisplayCard(VecPlayers[CountPlayer]);

		// Add the cards to together and show them
		VecPlayers[CountPlayer] = SumCard(VecPlayers[CountPlayer]);

		// Ask if they want another card and ask for input
		std::cout << std::endl << "Do you want another card? Y/N? \n:";
		std::cin >> Answer;

		// If they want another card
		while (Answer == 'Y') {
			// Give the player one card
			VecPlayers[CountPlayer] = AddCard(VecPlayers[CountPlayer], 1);

			// Show all the playerscards
			DisplayCard(VecPlayers[CountPlayer]);

			// Add the cards toghether
			VecPlayers[CountPlayer] = SumCard(VecPlayers[CountPlayer]);
			// If your cards sum is less than or equal to 21
			if (VecPlayers[CountPlayer].Sum <= 21) {
				// Ask if the want another card
				std::cout << std::endl << "Do you want another card? Y/N? \n:";
				std::cin >> Answer;
				// Capitalise all input so no need for or operator
				Answer = toupper(Answer);
			}
			// If they have too many cards
			else {
				std::cout << "Bust! Bad luck, the RNG gods were not on your side!" << std::endl;
				// Exit the while loop
				break;
			}
		}

		// If the current player is the last player in the vector
		if ((VecPlayers.size() - 1) == CountPlayer) {
			// GreatestSum is used to see who has the best set
			// WinnersIndex stores the winners index
			int GreatestSum = 0, WinnersIndex = 0;

			// Go through each index to see who has the largest sum
			for (unsigned int Count = 0; Count < VecPlayers.size(); Count++) {
				// Added so that new rounds have no cards
				VecPlayers[Count].Cards = 0;

				// If the current sum from the vector is the largest found then that is the winner
				if (VecPlayers[Count].Sum > GreatestSum && VecPlayers[Count].Sum < 22) {
					// Assign the Sum of the current player to GreatestSum
					GreatestSum = VecPlayers[Count].Sum;
					// Assign the Index of the current player to WinnersIndex
					WinnersIndex = Count;
				}
			}

			// Print out the winners name and sum of his cards
			std::cout << std::string(100, '\n') << "The winner is " << VecPlayers[WinnersIndex].Name
				<< " with the sum of " << VecPlayers[WinnersIndex].Sum << "\n:";
			// Press enter to continue
			std::cin.ignore();
			CountPlayer = 0;



		}
		else CountPlayer++;
		std::cin.ignore();
	} while (true);
	return 0;
}


BlackJack v2.h

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
#include <iostream>
#include <string>
#include <vector>
#include <time.h>

struct Players {
	std::string Name = "";
	unsigned int Wins = 0;
	unsigned int Cards = 0;
	int Sum = 0;
	unsigned int IndexSuit[10] = { 0 }; // Suit is from 0 to 3
	unsigned int IndexCard[10] = { 0 }; // Card is from 0 to 12
};

int RandomNumber(int Start, int End) {
	int RNG = (rand() % End) + Start;
	return RNG;
}

Players AddCard(Players Player, unsigned int NumberCards) {
	for (unsigned int Count = 0; Count < NumberCards; Count++) {
		Player.IndexSuit[Player.Cards] = RandomNumber(0, 3);
		Player.IndexCard[Player.Cards] = RandomNumber(0, 12);
		Player.Cards++;
	}
	return Player;
}

void DisplayCard(Players Player) {
	// Displays Suit of card 
	const std::string CardSuit[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
	// Displays Name of card
	const std::string CardName[] = { "Ace", "Two", "Three", "Four", "Five", "Six", 
		"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

	std::cout << "You have:" << std::endl << std::endl;
	for (unsigned int CardCount = 0; CardCount < Player.Cards; CardCount++) {
		std::cout << "The " << CardName[Player.IndexCard[CardCount]] << " of " 
			<< CardSuit[Player.IndexSuit[CardCount]] << std::endl;
	}

	return;
}

Players SumCard(Players Player) {
	Player.Sum = 0;
	

	for (unsigned int CardCount = 0; CardCount < Player.Cards; CardCount++) {
		if (Player.IndexCard[CardCount] < 10) {
			std::cout << (Player.IndexCard[CardCount] + 1);
			Player.Sum = Player.Sum + (Player.IndexCard[CardCount] + 1);
		}
		else if (Player.IndexCard[CardCount] == 0) {
			std::cout << "You have an Ace, (H)igh or (L)ow? \n:" << std::endl;
			char Answer = ' ';
			std::cin >> Answer;
			if (Answer == 'H') {
				std::cout << "11";
				Player.Sum = 11;
			}
			else {
				std::cout << "1";
				Player.Sum = 1;
			}
		}
		else {
			std::cout << "10";
			Player.Sum = Player.Sum + 10;
		}
		if ((CardCount + 1) == Player.Cards) std::cout << " = ";
		else std::cout << " + ";
	}
	std::cout << Player.Sum << std::endl;
	return Player;
}
Last edited on
for example following comment is redundant:
1
2
// Create Vector with the players, using vec because of an undiefined amount of players
std::vector<Players> VecPlayers;


anyone using the language is supposed to understand what the above code does.

you usually use comments to describe a function or class which is complex or long.
also comments can be used to indicate portion of the code that needs fix up or improvement.
closed account (E0p9LyTq)
While I don't necessarily agree with the author's ideas on commenting, something to read and ponder upon:

5 Best Practices for Commenting Your Code
http://improvingsoftware.com/2011/06/27/5-best-practices-for-commenting-your-code/
I see what your getting at but sometimes, I need to because I the rest of my peers in my class have had a small browse through python, its to make my code understandable to anyone that walks of the street!

I could just comment in just larger code blocks instead of every line of code?

Also, you have picked on the weirdest typo I have made.
Thanks for the link, I'm going to try and merge the practises that I've been doing and the ways of more experienced programmers and try and reach an equilibrium!
Topic archived. No new replies allowed.