I am in my first year of coding in school. While on break I am continuing to learn C++ on my own. This is not a finished product due to the fact that I haven't wrote the function to keep score of the money earned or lost. I am trying to see if this code that I have written is efficient. Any input would be greatly appreciated.
cout << "Your new Hand is: \n";
for(int i = 0; i < HAND; i++)
{
cout << setw(3) << cardHand[i] << " ";
cout << suit[i] << endl;
}
cout << endl;
}
void exchangeCards(int fiveCardHand[], vector<int>& deck, int& sizeOfDeck, char suit[])
{
int howManyToSwap;
int cardToSwap, card;
vector<int> swapTheseCards;
if(wantToSwapcards())
{
do
{
cout << "How many cards do you want to swap? ";
cin >> howManyToSwap;
if(howManyToSwap < 1 || howManyToSwap > 5)
cout << "Invalid Entry!! Must be between 1-5\n";
}while(howManyToSwap < 1 || howManyToSwap > 5);
if(howManyToSwap > 1 && howManyToSwap < 5)
{
cout << "Enter the card(s) number (1-5) you want to exchange with a space between them. \n";
cout << "Swap card(s) number : ";
for(int i = 0; i < howManyToSwap; i++)
{
cin >> cardToSwap;
swapTheseCards.push_back(cardToSwap);
}
}
}
for(int i = 0; i < swapTheseCards.size(); i++)
{
card = deck[sizeOfDeck] % 13 + 1;
int num;
num = swapTheseCards[i];
num = num - 1;
fiveCardHand[num] = card;
suit[num] = deck[sizeOfDeck] % 4 + 3;
deck.pop_back();
--sizeOfDeck;
}
}
bool wantToSwapcards()
{
char answerSwap;
bool answer = false;
do
{
cout << "Do you want to swap any of your cards? (Y or N) ";
cin >> answerSwap;
if(!(answerSwap == 'y' || answerSwap == 'Y' || answerSwap == 'n' || answerSwap == 'N'))
cout << "Invalid entry enter Y or N ONLY!!\n";
}while(!(answerSwap == 'y' || answerSwap == 'Y' || answerSwap == 'n' || answerSwap == 'N'));
Lumpkin, sir, are you sure about the anti-virus? I've always read it was recommended not to use system("PAUSE") because not all OSes have a PAUSE option. I read it was better to just do getchar() or such to make the program hang. Sir, can you site where it says it attract AV attention because that sounds rather odd.
As for your code, you could have reduced typing a little, but in terms of efficiency, it's fine.
Unless you plan to have another program try to play a gazillion hands in less than a second.
(The limiting factor here is the user's response time, which is significantly longer than anything your program will do. As long as the program feels sufficiently responsive to the user -- and doesn't waste resources -- it is fine.)
Thanks everyone for the input and I will look into the system() situation causing AV.
For those who wanted the code tags here it is again with the tags
Lumpkin and Duoas, sirs, I thought that was a myth. My cousin is getting into programming with Bloodshed Dev-C++, but the included examples there use system() calls, but his Norton, McAfee, nor AVG pop up when executing them. He set up Ubuntu Linux for me so I will have to keep that fact in mind. Thank you, sirs.
well, it isnt generally a good one. it can work, but only if the buffer is clear. im sure there is something on windows that will achieve the same effect, but on linux what i do using a class i found (and edited a bit) i clear the buffer and then take unbuffered input
edit: appending to my post... at that point it is safe to use cin.get() as it will have to actually wait for the use to hit the keyboard again
it flushes it (source http://en.cppreference.com/w/cpp/io/manip/endl ) although i dont know if there is a difference. it would flush the output buffer not what the person is inputting (or at least thats what i think would happen. ill do some experimenting)
The 'not clear' bit of the buffer is half the question.
First, your program should manage input properly -- that is, it should read input to properly synchronize what the user gives it.
Second, this is why I advocate using cin.ignore( numeric_limits <streamsize> ::max(), '\n' ) instead of just cin.get() or some other simple thing.
If you really want to 'clear' or 'flush' the input stream, which is a Bad Idea -- because it forces user agents to have to handle your program specially -- there are OS-specific ways to test for ready input and read it.
Thank you to everyone for their input. It is greatly appreciated. I am going to create a new post to discuss another issue I have with the same program.