I've just started a C++ class so I am a total beginner at this. I am having a problem figuring out how to pass by reference, and pass by value using the enum data type. The data types are as follows:

enum suits {hearts, spades, diamonds, clubs}
enum cardValues{two=2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace}

I would post the whole code, but I'm trying to figure it out myself first...with no success so far.

So, with this data type, how does one declare the variable within the function? I will post what I have of one function (so far) and maybe someone can give me enough hints to fix it and make it work? The first line within the brackets is where I am trying to do the declaration but nothing I try works. Thanks to anyone that can help from the foolish beginner.

void getCardValue(cardValues & userCardValue);
{
    userCardValue    ; 
    cout << " What is the value of your card? => ";
    cin >> userCardValue;

    return;
}

How does

void getCardValue(cardValues & userCardValue)
{
    int value;
    cout << " What is the value of your card? => ";
    cin >> value;

    if( value >= two && value <= ace )
        userCardValue = cardValues( value );

    return;
}

work for you?

Val

That actually works very well, but what does the call from main look like?

It works great for either card value OR card suit, but I need to call both functions from main. The calls work, but how are they declared there?

Oh, last but not least, thank you very much for the help, I've been wracking my brain for two days on this with no clue how to fix it.

Okay, I've changed the calls from main, but now I have to use an integer rather than typing in the suit, or card number. How do I fix that? I've posted the code this time.

#include <iostream>
using namespace std;


enum cardValues {two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace };
enum suit {hearts, spades, diamonds, clubs};

void getCardValue (cardValues & userCardValue);
void getCardSuit (suit & userCardSuit);


void main()
{  
	suit myCardSuit;
	getCardSuit (myCardSuit);
	cardValues myCardValue; 
	getCardValue (myCardValue);
	 

	switch(myCardSuit)
	{
	  case spades  : cout << "My card's suit = spades" << endl;  break;
	  case hearts  : cout << "My card's suit = hearts" << endl;  break;
	  case clubs   : cout << "My card's suit = clubs"  << endl;  break;
	  case diamonds: cout << "My card's suit = diamonds" << endl; break;
	  default      : cout << "INVALID SUIT!";
	}



	switch(myCardValue)
	{
		case ace  : cout << "My card's value = Ace"   << endl; break;
		case king : cout << "My card's value = King"  << endl; break;
		case queen: cout << "My card's value = Queen" << endl; break;
		case jack : cout << "My card's value = Jack"  << endl; break;
		default   : cout << "My card's value = " << myCardValue << endl;
	}
    

	system("PAUSE");

}

void getCardValue(cardValues & userCardValue)
{
    int value;
    cout << " What is the value of your card? => ";
    cin >> value;

    if( value >= two && value <= ace )
        userCardValue = cardValues( value );

    return;
}
void getCardSuit(suit & userCardSuit)
{
    int value;
    cout << " What is the suit of your card? => ";
    cin >> value;

    if( value >= hearts && value <= clubs )
        userCardSuit = suit( value );

    return;
}

Here's a little demo for you.

In the long run, you probably should create a struct Card that holds a facevalue and a suit. Then you can create arrays of cards for doing the things you probably need to do.

#include <iostream>
using namespace std;

enum suits {hearts, spades, diamonds, clubs};
enum cardValues{two=2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

void getCard(cardValues & value, suits & suit);


int main( )
{
    cardValues cardFace;
    suits cardSuit;

    getCard( cardFace, cardSuit );

    //do something with the card here.

    return 0;
}


void getCard(cardValues & value, suits & suit)
{
    int v;
    int s;
    cout << " What is the value of your card? => ";
    cin >> v;
    if( v >= two && v <= ace )
        value = cardValues( v );
     //do something to handle bad input?

    cout << " What is the suit of your card? => ";
    cin >> s;
    if( s >= hearts && s <= clubs )
        suit = suits( s );
    //and handle bad input here as well

}

Val

And the proper way to post code is to put a code tag before it
(omit the blank space) //your code here [/code ] (again, close up the space)

Val[code ] (omit the blank space)
//your code here
[/code ] (again, close up the space)

Val

Val, if you want to type out bbcode without adding spaces, you can wrap it in the tag. :)[noparse] tag. :)

commented: I wish I'd known about that before! +4

Okay, what you sent me worked, but my instructor tells me it's C style cast, and not technically pass by reference because I am declaring the int within the function. She told me to use a static_cast to do it, but I'm not sure what I'm doing wrong. Every time I try to put together the functions I get a c2679 error.

void getCardSuit(suit & userCardSuit)
{
    userCardSuit;
    cout << " What is the suit of your card? => ";
    cin >> userCardSuit; 
    userCardSuit= static_cast<suit&> (userCardSuit); 
    return; 
}

Any tips as to what I'm doing wrong this time?

Respectfully submitted, either you misunderstood what your teacher said about pass by reference, or your teacher is not making him/herself clear. My sample clearly passed your enum types by reference. Whatever variables are allocated in the function as intermediates does not change that fact.

That does not change the fact that you cannot directly input your enum types, the input operator does not know about them. Casting, either in the older C style or in the C++ style, will not work on the input stream.

So, using the C++ style of typecasting:

void getCard(cardValues & value, suits & suit)
{
    int v;
    int s;
    cout << " What is the value of your card? => ";
    cin >> v;
   if( v >= two && v <= ace )
        value = static_cast<cardValues>( v );
     //do something to handle bad input?

    cout << " What is the suit of your card? => ";
    cin >> s;
    if( s >= hearts && s <= clubs )
        suit = static_cast<suits>( s );
    //and handle bad input here as well

}

Val

Me being the new to this, I'm probably misunderstanding. As I understand it, using the ampersand in the prototype reserves the memory space for the value(s), implementing the function (typed in exactly the same as the prototype) passes by reference. I have found out the hard and frustrating way about >> not knowing about my enum types within the function. So if I'm understanding what you're saying, I can declare any variables I choose within the function and it is still passing by reference because of the prototype, call, and implementation?

She tells me I'm "cheating", humorously, by declaring an intermediate variable...if I don't do that though, I have no idea how to get the job done.

If she knows some way to do input directly to the the enum type, I wish she'd share.

Using the ampersand in the function parameter list means that the parameter will access the memory assigned of the actual argument you pass to it. In effect, it's an address that is passed to the parameter.

I've sent her an email asking for a hint at exactly what she's trying to get me to figure out. I told her I'm at a loss as to how to do it without declaring an intermediate variable. When it comes in I'll let you know what she says.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.