1

Okay i am apparently having problems on understanding exactly how string works. The problem im having is I get an error at the part where it assigns the cards. Please help me! Much appreciated!

#include "stdafx.h"
#include <string>
#include <time.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string getsuit();
	string getcard();
	string card[6];
	srand(time(0));
	int c;
	system("cls");
	cout<<"Press end to exit.\n\nWelcome to the card game!\nPress 'h' to deal!\n";		
	

		c=_getch();

		if(c=='h'){
//this part
			card[1]=getcard()+getsuit();
			card[2]=getcard()+getsuit();
			card[3]=getcard()+getsuit();
			card[4]=getcard()+getsuit();
			card[5]=getcard()+getsuit(); 
			cout<<"\n"<<card[1]<<"\n"
				<<"\n"<<card[2]<<"\n"
				<<"\n"<<card[3]<<"\n"
				<<"\n"<<card[4]<<"\n"
				<<"\n"<<card[5]<<"\n";
		}
		

		
		system("pause");


	return 0;
}

	string getsuit(){
		int c;
		c=(1 + rand() % (4));
		switch(c){
			case 1:
				return "Hearts";
				break;
			case 2:
				return "Diamonds";
				break;
			case 3:
				return "Spades";
				break;
			case 4:
				return "Clubs";
				break;
		}
	}

	string getcard(){
		
		int c;
		c=(1+ rand() % (52));
		switch(c){
			case 1:
				return "Ace";
				break;
			case 2:
				return "Two";
				break;
			case 3:
				return "Three";
				break;
			case 4:
				return "Four";
				break;
			case 5:
				return "Five";
				break;
			case 6:
				return "Six";
				break;
			case 7:
				return "Seven";
				break;
			case 8:
				return "Eight";
				break;
			case 9:
				return "Nine";
				break;
			case 10:
				return "Ten";
				break;
			case 11:
				return "Jack";
				break;
			case 12:
				return "Queen";
				break;
			case 13:
				return "King";
				break;
		}
	}

Thanks you alls!

Votes + Comments
Used code tags on first post.
3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by ArkM
0

What happens if you generate a number greater than 13?

string getcard(){
		
		int c;
		c=(1+ rand() % (52));
		switch(c){
			case 1:
				return "Ace";
				break;
			case 2:
				return "Two";
				break;
			case 3:
				return "Three";
				break;
			case 4:
				return "Four";
				break;
			case 5:
				return "Five";
				break;
			case 6:
				return "Six";
				break;
			case 7:
				return "Seven";
				break;
			case 8:
				return "Eight";
				break;
			case 9:
				return "Nine";
				break;
			case 10:
				return "Ten";
				break;
			case 11:
				return "Jack";
				break;
			case 12:
				return "Queen";
				break;
			case 13:
				return "King";
				break;
		}
	}
0

Oh my goodness! I'm stupid! Typo! Thanks so much! That 52 is supposed to be 13! THANK YOU SO MUCH!

0

Oh my goodness! I'm stupid! Typo! Thanks so much! That 52 is supposed to be 13! THANK YOU SO MUCH!

You're welcome. Thanks for using code tags on your first post! :cool:

0

Try much more simple and effective solution:

namespace { // Locals:
const std::string card[] = 
{
    "Ace",  "Two", "Three", "Four",
    "Five", "Six", "Seven", "Eight",
    "Nine", "Ten", "Jack", "Queen",
    "King"
};
const std::string suit[] = {
    "Hearts", "Diamonds", "Spades", "Clubs"
};
} // End of Locals
const std::string& randCard()
{
    return card[rand()%(sizeof card/sizeof*card)];
};
const std::string& randSuit()
{
    return suit[rand()%(sizeof suit/sizeof*suit)];
}
0

Try much more simple and effective solution:

namespace { // Locals:
const std::string card[] = 
{
    "Ace",  "Two", "Three", "Four",
    "Five", "Six", "Seven", "Eight",
    "Nine", "Ten", "Jack", "Queen",
    "King"
};
const std::string suit[] = {
    "Hearts", "Diamonds", "Spades", "Clubs"
};
} // End of Locals
const std::string& randCard()
{
    return card[rand()%(sizeof card/sizeof*card)];
};
const std::string& randSuit()
{
    return suit[rand()%(sizeof suit/sizeof*suit)];
}

Yeah thanks for the advice! But i am having difficulties on understanding pointers, and pointing at char arrays. Also since this was just a little game i didnt think it would really need a namespace. Also i just learned those... like... 5 days ago... so Thanks anyways! I will play around with what you have done and use it in future projects :)

0

Anonymous namespace does not bear a relation to the project size. It's C++ feature to declare variables (and functions) with module (source file) scope and static duration.

It's interesting that there are no pointers to char arrays in my snippet. I have declared arrays of std::strings. Both functions return not pointers but const references to strings. Try to understand reference concept in C++ as soon as possible.

Pay attention to constant expression sizeof array/sizeof*array . It's equal to the number of array elements.

Next time try to avoid long case lists and complex cascaded ifs: as usually, it's a symptom of bad (or incomplete) design.

Good luck!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.