Open Side Menu Go to the Top
Register
Looking for info on C++ Looking for info on C++

02-21-2012 , 10:03 AM
Quote:
Originally Posted by meekrab
This is actually not completely true in C++. The labels in a switch must be constants, whereas in an if/else you're free to compare with things that are evaluated at runtime.
It also may compile into different code depending on what the constants are. In some cases if you use for example ints as constants and those ints are close in value, the code atleast in many implementations compile to a jump table which is considerably faster than the if/else if
Looking for info on C++ Quote
02-21-2012 , 01:54 PM
Quote:
Originally Posted by Ryanb9
the main thing im worried about is learning something I shouldn't be learning for fear of developing bad habits. for instance iv been using "using namespace std ; " since i started a week or so ago and and now im reading about how this is horrible because it replaces all instances of something or other
This is the exact reason why I recommend reading books when you're first learning. Good (emphasis on good) programming books will teach you to program correctly while also touching on how not to program. Practicing programming is important to learning it but if you are practicing techniques that you picked up from trial and error or some ****ty blog on the internet... you now have bad habits.
Looking for info on C++ Quote
02-21-2012 , 02:40 PM
Quote:
Originally Posted by meekrab
This is actually not completely true in C++. The labels in a switch must be constants, whereas in an if/else you're free to compare with things that are evaluated at runtime.
Also, switches can only use chars or ints for the different cases, where as an if statement is not bound by those restraints. ie: switches can't handle a range of inputs

/obv
Looking for info on C++ Quote
02-21-2012 , 03:56 PM
Best bet is to take a community college class imo
Looking for info on C++ Quote
02-21-2012 , 05:57 PM
Quote:
Originally Posted by daveT
A switch statement is just syntactic sugar on an if-else block:
Switch statements also let you define a default case which is hugely useful in terms of security.
Looking for info on C++ Quote
02-21-2012 , 08:34 PM
Quote:
Originally Posted by RICHI8
Switch statements also let you define a default case which is hugely useful in terms of security.
how is that better than an else (in terms of security)? I mean your case 1-10 pick up anything in the range of 1-10 and your default gets the rest, but cant you just have 10 if else and than an else that picks up the rest?

Also, the default is not universal is that correct? I thought I read somewhere that the default case is not iso or is not on all compilers or something like that and that's why its optional.
Looking for info on C++ Quote
02-21-2012 , 08:37 PM
I have no idea what you mean by a switch default being good for security?
Looking for info on C++ Quote
02-21-2012 , 08:39 PM
Quote:
Originally Posted by Gullanian
I have no idea what you mean by a switch default being good for security?
I think he means something like you can make it so that by default, you dont have access =P
Looking for info on C++ Quote
02-21-2012 , 09:09 PM
If anyone is bored and has a C++ compiler:

Spoiler:
I just finished making my first game (blackjack). Code is here https://docs.google.com/open?id=0B96...Q3NmNiODFlMDYy
Its just a Win32 Console Application. Id like feedback on things I could improve tho (I have a feeling organization will be one of them >.< )
Looking for info on C++ Quote
02-22-2012 , 12:57 AM
4 thousand lines? For blackjack? 0_0
Looking for info on C++ Quote
02-22-2012 , 01:05 AM
Quote:
Originally Posted by Ryanb9
If anyone is bored and has a C++ compiler:

Spoiler:
I just finished making my first game (blackjack). Code is here https://docs.google.com/open?id=0B96...Q3NmNiODFlMDYy
Its just a Win32 Console Application. Id like feedback on things I could improve tho (I have a feeling organization will be one of them >.< )
I don't really do C++ and it's 5am but at a quick glance there is a lot of repeating code (for example `if card == 48`). You can remove a lot of this, and you should also be storing a lot of the data in arrays and not as separate string variables.

Any time you find yourself writing the same thing over and over or copy and pasting, 99.99% of the time there's a better way of doing it
Looking for info on C++ Quote
02-22-2012 , 01:54 AM
it looks like you like typing



-use an array for the deck, http://www.cplusplus.com/doc/tutorial/arrays/

-dont write over 10 nearly identical functions to draw a random card.
write one function that shuffels the deck.
write one function that returns a card from the deck.

-dump the value lookup think for the cards. u can use a multidimensional array to store the value along with the card string.

- write a function to evaluate youre win/lose cases
- rewrite the main() part with your new functions
Looking for info on C++ Quote
02-22-2012 , 02:26 AM
Sweet thanks guys I will go back to the drawing board. I wish I would have thought about it more before I started. Also I was afraid of using arrays because I have read about the nasty runtime errors they can give you if you make a mistake.
Looking for info on C++ Quote
02-22-2012 , 04:49 AM
I really hesitated before I wrote the syntactic sugar comment, which why I linked to what I was hoping was a reputable source. It does illustrate a point I've come across a thought I've had several times: syntactic sugar is sort of a relative concept.

Quote:
Also I was afraid of using arrays because I have read about the nasty runtime errors they can give you if you make a mistake.
Chill out, man, you're just starting out....
Looking for info on C++ Quote
02-22-2012 , 08:18 AM
Wow, in a way I admire you for having the patience to do all that typing, but read up on loops and arrays. Also this type of project would be a good opportunity to practice OOP.

Maybe learn the basics of procedurral programming and arrays and loops and such on a smaller program
Looking for info on C++ Quote
02-22-2012 , 02:11 PM
Hey OP, didn't really read through the replies you have gotten so far but fwiw I have taken introductory c++ and data structures in c++ and have been tutoring students in c++ for 2 semesters now. If you are just learning a free compiler should be fine, and if you look around there are more than enough free resources to learn. Feel free to pm me if you have any general questions
Looking for info on C++ Quote
02-22-2012 , 02:18 PM
I wasn't at 100% when I posted that crappy one liner earlier, here's some constructive thoughts:

Quote:
Originally Posted by Ryanb9
Sweet thanks guys I will go back to the drawing board. I wish I would have thought about it more before I started. Also I was afraid of using arrays because I have read about the nasty runtime errors they can give you if you make a mistake.
The #1 error beginning programmers make is to just jump in and start coding without thinking about the problem they're trying to solve and the data structures that naturally go along with that problem.

For instance, you're making a card game, you should probably have a Card class (or struct). Representing cards as strings is handy when you go to print them, but leads to nonsense like your 300 line long dealer_hit function where you have to compare the new random number against 52 other numbers just to pick a card (this could also be avoided by putting the card strings into an array or vector and then indexing that. Point is it's a symptom of a bad design decision somewhere else) and the great thing about a Card class is that you can easily declare a member function to return that string when you need to print the hands, or to return its numerical value when you're totaling the hand value.

You can represent a deck of cards as a Vector (which is a fancy array that handes memory for you, so as long as you don't go asking a 20 element vector for it's 300th element, you'll be fine) and you can shuffle it with a simple for loop (start at 0, swap each card with a random position between 0 and 51) and then instead of generating random cards on the fly, you can simply deal the next card off the end of the Vector. Random generation when you need it is alright for a toy, but it's not ideal in the case where you don't want repeated numbers, as you discovered with your player_hit_ and dealer_hit functions. What if your company suddenly decided they want to make a 6 player online casino blackjack game? All those comparisons against the previously generated cards for every game on the server every time a new card is dealt get to be expensive.

Last two general pieces of advice, which are actually related. 1) learn how function arguments and return values work. All those duplicated void player_hit_X_function() should be one function that returns a card value to a variable in main, etc. and 2) you should really, really stop using non-constant global variables. There's almost never a legitimate need for them.

Hope this is helpful.
Looking for info on C++ Quote
02-22-2012 , 02:34 PM
The information meekrab just posted is very valuable. If you lay out the problem logically and think about it in terms of separation of concerns BEFORE you write a single line of code, the code writing will become much easier. This will come easier with more practice, of course. I think a nice whiteboard is a much better investment than any book on the subject.
Looking for info on C++ Quote
02-22-2012 , 02:56 PM
Quote:
Originally Posted by RICHI8
Switch statements also let you define a default case which is hugely useful in terms of security.
As opposed to this?

Code:
else{
//default case
}
Looking for info on C++ Quote
02-22-2012 , 08:32 PM
Meekrab posted some good advice, but I would say that global variables are fine to use in small (i.e. single file) programs. As long as you understand that using global variables is a poor design in large scale systems, you should be fine.
Looking for info on C++ Quote
02-23-2012 , 12:28 AM
Thanks a lot guys! I will make it harder better faster stronger xD
Looking for info on C++ Quote
02-23-2012 , 10:45 AM
Think I'm going in the right direction?


Spoiler:
Code:
#include "stdafx.h"
#include <iostream> 
#include <string> 
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <sstream>

using namespace std ; 

	string array_card_string[2][52] = 
	{{"Ah", "2h", "3h", "4h", "5h", "6h", "7h", "8h", "9h", "10h", "Jh", "Qh", "Kh",
	"Ad", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "10d", "Jd", "Qd", "Kd",
	"As", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s", "10s", "Js", "Qs", "Ks",
	"Ac", "2c", "3c", "4c", "5c", "6c", "7c", "8c", "9c", "10c", "Jc", "Qc", "Kc"},
	{"11", "2", "3", "4", "5", "6", "7", "8", "9", "10", "10", "10", "10", 
	"11", "2", "3", "4", "5", "6", "7", "8", "9", "10", "10", "10", "10",
	"11", "2", "3", "4", "5", "6", "7", "8", "9", "10", "10", "10", "10",
	"11", "2", "3", "4", "5", "6", "7", "8", "9", "10", "10", "10", "10",}};
	vector<int> card_vector;
	int string_to_int_converter(string abc);
	int RNG(int cards_in_deck);
	void refresh_deck();
	int deal_a_card(int cards_in_deck);
	
int main () 
{
	srand(unsigned (time(0)));
	int cards_in_deck = 53; 
	refresh_deck();
int test = 0; 
do{
	int card1 = deal_a_card(cards_in_deck--);
	int card2 = deal_a_card(cards_in_deck--);
	cout << endl << "Your hand is " << array_card_string[0][card1] << " " << array_card_string[0][card2] << endl ;
	cout << "Your hand value is " << ((string_to_int_converter(string (array_card_string[1][card1]))) + (string_to_int_converter(string (array_card_string[1][card2])))) << endl ; 
	cin >> test; 
} while (test != 0) ; 
	return 0 ; 
}
int RNG(int cards_in_deck) 
{
	int random_number = rand() % cards_in_deck ; 
	return random_number; 
}

void refresh_deck()
{
	for (int y = 0 ; y != 52 ; y++) {
	card_vector.push_back (y); 
	};
}

int deal_a_card(int cards_in_deck)
{
	cards_in_deck--;	
	int ticker = 0 ; 
	do {
		swap (card_vector[0], card_vector[RNG(cards_in_deck)]) ; // shuffles the deck
		ticker++ ; 
	} while (ticker != cards_in_deck) ; 
	int xyz ;  
	xyz = card_vector.at(0); 
	card_vector.erase (card_vector.begin());
	return xyz; 
}

int string_to_int_converter(string abc)
{
std::string be_converted = abc;
int value = atoi(be_converted.c_str());
return value;
}


I'm trying to do it in as few lines as possible but its starting to hurt my brain. I think I can get rid of the card1 but not sure how yet.
Looking for info on C++ Quote
02-23-2012 , 11:06 AM
Have you actaully run that program? you have cards_in_deck = 53, why not 52? Seems you may end up indexing outside array/vector.

Also in for-loops the way you use it in refresh_deck a clearer syntax would be for(int y = 0; y < 52; y++) instead of !=.

Also generally its better to avoid constants such as 52 in the code. Better to use:
const int some_name = 52 and then do: for(int i = 0; i < som_name; i++)

You also have a ; at the end of the for-loop on the closing brace, that isn't necessary, it simply becomes an empty statement
Looking for info on C++ Quote
02-23-2012 , 11:14 AM
To get the first element in a vector you can use my_vector.front() I think. And to remove it my_vector.pop_front(). simplifies the code somewhat
Looking for info on C++ Quote
02-23-2012 , 11:33 AM
I start at 53 because I take one card out of the deck at the start of the function.
Looking for info on C++ Quote

      
m