1

Im new to c++ and right now going through a course.

Im coding a bulls and cows guess my word game.

I finished the code, but it didnt work the way i wanted.

It fails when i try to pass variables between two functions.

thats the code:

 #include <iostream>
#include <string>

using namespace std;

void PrintIntro();         <-- the function that passes the variable
void PlayGame();           <-- the function trying to get the vriable
string PlayersGuess();

int main()
{
    // Printing the Intro and Instrations of the game
    PrintIntro();
    // Function to play our game
    PlayGame();

    return 0; // exits the application
}

    void PrintIntro()
    {
        // introduce the game
        constexpr int WORD_LENGTH = 5;
        cout << "Welcome to Bulls and Cows" << endl;
        cout << "Can you guess the " << WORD_LENGTH << " letter word I'm thinking of?" << endl;
        cout << endl;
        PlayGame(WORD_LENGTH);        <-- Taking the variable
        return;
    }

    string PlayersGuess()
    {
        // get a guess from the player
        cout << "Enter your guess: ";
        string Guess = "";
        getline(cin, Guess);
        cout << endl;
        return Guess;
    }

    void PlayGame(const int &passed)    <-- passing through here
    {
        // Game Intro
        for (int i = 0; i < passed; i++)     <-- to here
        {
            // Players Guess
            string Guess = PlayersGuess();
            cout << "Your guess is: " << PlayersGuess() << endl;
            cout << endl;
        }
    }

The result is a fail and it says "Function does not take 1 argument"

What is the right way to pass it?

Kiper
  • 309
  • 3
  • 17

4 Answers4

1

Change the declaration :

void PlayGame()

To:

void PlayGame(const int &passed)
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • I tried this in C++ Shell (ccp.sh) and it failed with a number of errors including: "1:43: error: expected unqualified-id before '<' token In function 'int main()':". Are there other things about the code needing fixing too? – Peter David Carter Apr 20 '16 at 08:24
1

The declaration void PlayGame(); in the beginning does not accept parameter. Change the declaration to accept parameter of the required type. Declaration and definition must match. Hope this helps.

ArtBajji
  • 949
  • 6
  • 14
0

Signatures of functions' declaration and definition must match each other. You need to declare functions like this:

void PlayGame(const int &passed);

In your code you have two different functions with name PlayGame. In the moment when PlayGame() is called with a parameter a compiler hasn't met the appropriate function yet so it gives the error.

Andrey Lyubimov
  • 663
  • 6
  • 22
  • I tried this in C++ Shell (ccp.sh) and it failed with a number of errors including: "1:43: error: expected unqualified-id before '<' token In function 'int main()':". Are there other things about the code needing fixing too? – Peter David Carter Apr 20 '16 at 08:25
  • @PeterDavidCarter well, now all the calls to this function must be **with** a parameter – Andrey Lyubimov Apr 20 '16 at 08:26
  • Ah, ok. C++ isn't my strong-point. I read a lot about C# just after it came out and did a lot of Pascal at college and played with some Java recently, but I haven't looked at C++ since ages ago so I am probably missing some obvious things you left out of your answer thinking everyone would know them, at a guess :~). – Peter David Carter Apr 20 '16 at 08:29
  • @PeterDavidCarter you definitely right! people always make assumptions that what is obvious for them is also obvious for others – Andrey Lyubimov Apr 20 '16 at 09:50
  • I have barely looked at C++ before but I have met the guy who wrote Faster Than Light http://www.ftlgame.com/ who told me he wrote it all in C++ so I want to learn it. – Peter David Carter Apr 20 '16 at 09:52
0

If you want a function to take an argument, you have to tell the compiler it takes an argument.

The function prototype declaration

void PlayGame();

tells the compiler that the PlayGame function takes no arguments, and return no value. If you then try to call it using an argument that doesn't match the declaration and you will get an error.

On the other hand, if you declare it like your definition:

void PlayGame(const int &passed);

then you tell the compiler that the function must take an argument (a reference to a constant int), and you can not call the function without argument.

If you want different behavior depending on the argument (or lack thereof) passed, then you have two solutions: Function overloading, or default arguments.

For example, you can have two different functions with the same name, but different signature (basically different arguments), so you can have e.g.

void PlayGame() { ... }
void PlayGame(int passed) { ... }

Then (with the right forward declarations) you can call it with either no arguments in which case the first function will be called, or with an integer value in which case the second function will be called.

Using default arguments you can do something like

void PlayGame(int passed = 0) { ... }

Then if you call the function with an argument that argument will be passed, if you pass it without any argument the default value (0 in my example) will be used.

Also note that I removed the constant reference part, that's not really needed for simple int values.

All of this should be clearly explained in any good book.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks, but now what should i write in my main function? there is a playgame() also and i dont need it to return there nothing. just write the same here? – Kiper Apr 20 '16 at 08:20
  • I tried this in C++ Shell (ccp.sh) and it failed with a number of errors including: "1:43: error: expected unqualified-id before '<' token In function 'int main()':". Are there other things about the code needing fixing too? – Peter David Carter Apr 20 '16 at 08:21
  • Thanks for the edit. Lots more in there now! I'll have a look myself, see if even a C++ ingenue can make the above code work from your now extended suggestions! :~) – Peter David Carter Apr 20 '16 at 08:32