locked
Code help RRS feed

  • Question

  • Hi there I'm having trouble getting this function to call any help would be greatly appreciated!

    Thanks in advance!

    #include<iostream>
    #include<cstdlib>    
        using namespace std;

        int CardEvaluate (char tempValue);

        int main()
        {
            int Numberofcards = 0;
            char value1 ='a', value2 ='a',
                 value3 ='a', value4 ='a',
                 value5 ='a', tempValue;
            int Total_Value = 0, NumOfAces;
            char answer;

            do
            {

                cout << "BlackJack Hand Evaluator \nThis program is designed to total your hand in a game of Blackjack" << endl << endl;
                cout << "\nPlease enter the number of cards in your hand between 2 and 5: ";
                cin >> Numberofcards;

            while((Numberofcards < 2) || (Numberofcards > 5))
            {
                cout << "\nThat is not a valid hand. ";
                cout << "\nPlease enter the number of cards in your hand between 2 and 5: ";
                cin>> Numberofcards;
            }
            for(int i=0; i < Numberofcards; i++)
            {
                cout << "\nPlease unter the value of card.  Use 2 - 9 for numeric cards. "
                     << "\n Use 'T' for 10, 'J' for Jack, 'Q' for Queen, 'K' for King, and"
                     << "\n 'A' for Ace."
                     << i + 1 << "\n: ";
                if (i == 0)
                    cin >> value1;
                else if (i == 1)
                    cin >> value2;
                else if (i == 2)
                    cin >> value3;
                else if (i == 3)
                    cin >> value4;
                else if (i == 4)
                    cin >> value5;
            }

            Total_Value = 0;
            NumOfAces = 0;

            for (int i = 0; i < Numberofcards; i++)
        {
                if (i == 0)
                    tempValue = value1;
                else if (i == 1)
                    tempValue = value2;
                else if (i == 2)
                    tempValue = value3;
                else if (i == 3)
                    tempValue = value4;
                else if (i == 4)
                    tempValue = value5;


                Total_Value += CardEvaluate (tempValue);

        }
                if (NumOfAces > 0)
                {
                    if ((Total_Value + 11) > 21)
                        Total_Value +=1;
                    else
                    Total_Value += 11;
                    cout << "Your total hand value is: ";
                }

                else
                    cout << "Your total hand value is: ";
                    cout << Total_Value;

                if (Total_Value > 21)
                    cout << " You busted.";

                if (Total_Value == 21)
                    cout << " Winner winner chicken dinner! You got 21!";

                cout << "\n\nWould you like to total another hand?";
                cout << "\nEnter 'Y' to contine, or 'N' to close program: ";
                cin >> answer;
            } while(answer == 'y' || answer == 'Y');

                return 0;

        }
        int Total_Value = 0, NumOfAces;

        int CardEvaluate (char tempValue)

        {
            //funtion goes here.
            switch (tempValue)
            {
                case '2':
                Total_Value += 2;
                break;
                case '3':
                Total_Value += 3;
                break;
                case '4':
                Total_Value += 4;
                break;
                case '5':
                Total_Value += 5;
                break;
                case '6':
                Total_Value += 6;
                break;
                case '7':
                Total_Value += 7;
                break;
                case '8':
                Total_Value += 8;
                break;
                case '9':
                Total_Value += 9;
                break;
            case 't':
            case 'T':
            case 'j':
            case 'J':
            case 'q':
            case 'Q':
            case 'k':
            case 'K':
                Total_Value += 10;
                break;
            case 'a':
            case 'A':
                NumOfAces += 1;
                break;

            }

            return (CardEvaluate);

        }

    error C2440: 'return' : cannot convert from 'int (__cdecl *)(char)' to 'int'
    1>          There is no context in which this conversion is possible

                                                                
    Sunday, February 16, 2014 2:51 AM

Answers

  • The way you are using Total_Value in CardEvaluate will work only for the very first hand you evaluate.

    The variable NumofAces you define at file scope is never used.

    Go back to my previous message and look at the difference between how I suggested you change CardEvaluate and what you did.  Hint: an int is completely different than an int*.  C++ passes by value.  The value of NumOfAces after you increment it in CardEvalaute is never seen by main.

    You need to step through the code with the debugger and watch what is happening to your variables.

    What is the purpose of the cin.ignore statement?

    • Marked as answer by Anna Cc Tuesday, February 25, 2014 2:03 AM
    Tuesday, February 18, 2014 8:44 AM
  • Curse the darkness or light a candle.  The only way to eliminate confusion is to expand you knowledge.

    If you want to program, learn to use the tools available.  This is a simple enough program that using the debugger should be relatively straightforward.

    While you are at it, you might reduce your level of confusion if you made it a point to not use the same variable name for different variables.  You have three completely different variables named NumOfAces and two named Total_Value.  Your code behaves as if changing one of the variables somehow magically changes the value of some other variable with the same name.

    • Marked as answer by Anna Cc Tuesday, February 25, 2014 2:04 AM
    Tuesday, February 18, 2014 10:30 PM

All replies

  • On 2/15/2014 9:51 PM, Snipe_Mercenary wrote:

             return (CardEvaluate);


    error C2440: 'return' : cannot convert from 'int (__cdecl *)(char)' to 'int'
    1>          There is no context in which this conversion is possible

    Which value of type int do you want your function to return? Total_Value, NumOfAces, something else? If, say, it's Total_Value, then write "return Total_Value;"


    Igor Tandetnik
    Sunday, February 16, 2014 3:39 AM
  • Thanks! I got it to run using Total_Value, but now it will not total the cards correctly ie: a hand of two cards each with the value of 2 is totaling to 6.  Any suggestions there?
    Sunday, February 16, 2014 4:09 AM
  • On 2/15/2014 11:09 PM, Snipe_Mercenary wrote:

    Thanks! I got it to run using Total_Value, but now it will not total the cards correctly ie: a hand of two cards each with the value of 2 is totaling to 6.  Any suggestions there?

    You are double-counting. You increment Total_Value inside CardEvaluate, and then again outside at "Total_Value += CardEvaluate (tempValue);"


    Igor Tandetnik
    Sunday, February 16, 2014 5:10 AM
  • Are you aware that you have two different variables named Total_Value and two different variables named NumOfAces?  The first of each is defined at the top of main and is accessible only within that function.  The second of each is defined at file scope in between the end of main and the beginning of CardEvaluate. 

    The second of each is the only set that CardEvaluate can access.  However, since main cannot access either of these, any changes CardEvaluate makes to them is irrelevant to the processing performed by main.  In particular, NumOfAces in main will always be zero.

    You called the function CardEvaluate, not HandEvaluate.  main expects it to return the value of a single card, not the value of the hand.  If it did that, the computation of Total_Value in main would be correct except for the handling of aces.  If CardEvaluate returned 0 for an ace, you could check that in main and increment NumOfAces when appropriate.  Then your current code of adding 11 or 1 would be correct to Total_Value in main would be correct.  And the two file scope versions of the variables could be deleted.

    Sunday, February 16, 2014 5:35 PM
  • That is what I am currently looking at right now haha.  As it shows I'm sure I am very green at writing code and this is my first time using functions I really appreciate all the help!
    Monday, February 17, 2014 4:56 AM
  • Ok this is going to sound really dumb but how would you get NumOfAces to increment in main as everything I am trying is making the program crash....
    Monday, February 17, 2014 5:11 AM
  • Show your current code.

    Have you stepped through your code with the debugger to see exactly where it is crashing?

    Did you try what I suggested?  Or are you trying to increment NumOfAces in CardEvaluate?

    In addition to what I suggested, there are many other methods to increment NUmOfAces:

         Easiest (but not recommended) is to define NumOfAces exactly once before the definition of your first function.  This makes the variable visible to every function in your source file.  CardEvaluate can increment it and main can see the incremented value.

         Change CardEvaluate to process a second parameter of type int*.  When calling this function, use the address of NumOfAces as the second argument.  In CardEvaluate, add the appropriate code to increment the integer that the new parameter points to.

         Change CardEvaluate to return an integer of the form xyy.  yy would be the value of the card, 1-10.  x would be either 0 or 1 depending on whether the card is an ace or not.  In main, use integer division and the mod operator to extract the values of x and yy and process as desired.

    Monday, February 17, 2014 9:03 PM
  • #include<iostream>
    #include<cstdlib>    
        using namespace std;

        int CardEvaluate (char tempValue , int NumOfAces);

        int main()
        {
            int Numberofcards = 0;
            char value1 ='a', value2 ='a',
                 value3 ='a', value4 ='a',
                 value5 ='a', tempValue;
            int Total_Value = 0, NumOfAces;
            char answer;

            do
            {

                cout << "BlackJack Hand Evaluator \nThis program is designed to total your hand in a game of Blackjack" << endl << endl;
                cout << "\nPlease enter the number of cards in your hand between 2 and 5: ";
                cin >> Numberofcards;

            while((Numberofcards < 2) || (Numberofcards > 5))
            {
                cout << "\nThat is not a valid hand. ";
                cout << "\nPlease enter the number of cards in your hand between 2 and 5: ";
                cin>> Numberofcards;
            }
            for(int i=0; i < Numberofcards; i++)
            {
                cout << "\nPlease unter the value of card.  Use 2 - 9 for numeric cards. "
                     << "\n Use 'T' for 10, 'J' for Jack, 'Q' for Queen, 'K' for King, and"
                     << "\n 'A' for Ace."
                     << i + 1 << "\n: ";
                if (i == 0)
                    cin >> value1;
                else if (i == 1)
                    cin >> value2;
                else if (i == 2)
                    cin >> value3;
                else if (i == 3)
                    cin >> value4;
                else if (i == 4)
                    cin >> value5;
            }

            Total_Value = 0;
            NumOfAces = 0;

            for (int i = 0; i < Numberofcards; i++)
        {
                if (i == 0)
                    tempValue = value1;
                else if (i == 1)
                    tempValue = value2;
                else if (i == 2)
                    tempValue = value3;
                else if (i == 3)
                    tempValue = value4;
                else if (i == 4)
                    tempValue = value5;


                Total_Value = CardEvaluate (tempValue , NumOfAces);

        }
                if (NumOfAces > 0)
                {
                    if ((Total_Value + 11) > 21)
                        Total_Value +=1;
                    else
                    Total_Value += 11;
                    cout << "Your total hand value is: ";
                }

                else
                    cout << "Your total hand value is: ";
                    cout << Total_Value;

                if (Total_Value > 21)
                    cout << " You busted.";

                if (Total_Value == 21)
                    cout << " Winner winner chicken dinner! You got 21!";

                cout << "\n\nWould you like to total another hand?";
                cout << "\nEnter 'Y' to contine, or 'N' to close program: ";
                cin >> answer;
            } while(answer == 'y' || answer == 'Y');

                cin.ignore();
                return 0;

        }
        int Total_Value, NumOfAces;

        int CardEvaluate (char tempValue , int NumOfAces)

        {
            //funtion goes here.
            switch (tempValue)
            {
                case '2':
                Total_Value += 2;
                break;
                case '3':
                Total_Value += 3;
                break;
                case '4':
                Total_Value += 4;
                break;
                case '5':
                Total_Value += 5;
                break;
                case '6':
                Total_Value += 6;
                break;
                case '7':
                Total_Value += 7;
                break;
                case '8':
                Total_Value += 8;
                break;
                case '9':
                Total_Value += 9;
                break;
            case 't':
            case 'T':
            case 'j':
            case 'J':
            case 'q':
            case 'Q':
            case 'k':
            case 'K':
                Total_Value += 10;
                break;
            case 'a':
            case 'A':
                NumOfAces += 1;
                break;

            }

            return (Total_Value );

        }

    I've tried adding it to the function but I don't seem to be getting a proper response. It is not crashing anymore but It still is not recognizing the ace.  Another bug that I have found is that it is adding the current total hand to the previous total if I run the program more than once.  IE: a hand totaling 20 in the first hand totaling the same hand again will yield 40 and so on.

    Tuesday, February 18, 2014 5:56 AM
  • The way you are using Total_Value in CardEvaluate will work only for the very first hand you evaluate.

    The variable NumofAces you define at file scope is never used.

    Go back to my previous message and look at the difference between how I suggested you change CardEvaluate and what you did.  Hint: an int is completely different than an int*.  C++ passes by value.  The value of NumOfAces after you increment it in CardEvalaute is never seen by main.

    You need to step through the code with the debugger and watch what is happening to your variables.

    What is the purpose of the cin.ignore statement?

    • Marked as answer by Anna Cc Tuesday, February 25, 2014 2:03 AM
    Tuesday, February 18, 2014 8:44 AM
  • I've never used the debugger before... I'm so confused and quite frustrated.
    Tuesday, February 18, 2014 5:47 PM
  • Curse the darkness or light a candle.  The only way to eliminate confusion is to expand you knowledge.

    If you want to program, learn to use the tools available.  This is a simple enough program that using the debugger should be relatively straightforward.

    While you are at it, you might reduce your level of confusion if you made it a point to not use the same variable name for different variables.  You have three completely different variables named NumOfAces and two named Total_Value.  Your code behaves as if changing one of the variables somehow magically changes the value of some other variable with the same name.

    • Marked as answer by Anna Cc Tuesday, February 25, 2014 2:04 AM
    Tuesday, February 18, 2014 10:30 PM