-1

I just can't work out what I'm doing wrong here, but putting any parameters into my attempt function gives me an error.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int guesses = 0;

int attempt(int guess) {
    if(guesses > 0) {
           cout << "Take a guess!";
           cout << endl;
           cin >> guess;
           }
}

int main() {

    int answer = (rand() % 10) + 1;
    int guess;

    srand(time(0));    

    cout << "I am thinking of a number between 1 and 10. Try to guess it in 3 attempts!";
    cout << endl;
    attempt();

    cin.get();
    return 0;
}

I should add that I'm a beginner, so please explain this to me like a 5 year old.

Taint
  • 67
  • 1
  • 1
  • 10
  • 1
    A very basic, 5yo type question: what error do you get? –  Jan 29 '14 at 02:30
  • This doesn't have anything to do with ELI5, does it? – chris Jan 29 '14 at 02:31
  • `int attempt(int guess)` should be `int attempt(int& guess)`, and in main, `attempt();` should pass a `int&`. It's better to learn basics of C++ before writing code. – Mine Jan 29 '14 at 02:32
  • "Too few arguments to function" – Taint Jan 29 '14 at 02:33
  • `int guesses = 0;` ... `if(guesses > 0) { ...` And `guesses` isn't modified anywhere. Nothing within the `attempt()` function will execute, ever. – nhgrif Jan 29 '14 at 02:33
  • If you haven't already, I suggest getting a good [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – chris Jan 29 '14 at 02:37
  • I actually do have a couple of books, I've read them all more than once too, I'm just in the process of trying to pick up where I left off a few months back. – Taint Jan 29 '14 at 02:42

4 Answers4

3

You'd be better off with something like:

int attempt() {
    int guess;
    cout << "Take a guess!\n";
    cin >> guess;
    return guess;
}

and calling it with:

guess = attempt();

There's no point in passing the current guess into attempt and it would only be reflected back to the caller if it were a reference parameter, which it isn't.

I'm also not entirely certain what you were hoping to gain by only asking for a guess when guesses had already been made but that was the effect of your old code.

Other issues you may have:

You call rand before srand. That means you'll get the same random number on each run since the behaviour of rand where no seed has been set is as if you'd called srand(1).

The guesses variable is set to zero and never changed. Presumably you'll fix this when you get around to implementing the "three guesses only" functionality (with a loop in main presumably).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

When you declare a function with value parameters, such as

void func(int i)

you are declaring inputs.

#include <iostream>

void func(int i)
{
    // the 'i' you see here is a local variable.
    i = 10;
    // we changed the local thing called 'i', when
    // we exit in a moment that 'i' will go away.
}

int main()
{
    int i = 1;
    func(i);
    // if we print 'i' here, it will be our version of i.
    std::cout << "i = " << i << '\n';

    return 0;
}

The i parameter to func is local to func; this confuses new C++ programmers especially in the above scenario, it looks like you are passing i itself into func rather than passing the value of main::i.

As you get further into C/C++ you'll discover 'reference' and 'pointer' types which allow you to actually forward variables to functions rather than their values.

When your function is actually doing is fetching a user input value and then passing that back to the caller, or returning it.

int attempt()
{
    std::cout << "Take a guess!\n";

    int guess;
    std::cin >> guess;

    return guess;
}

We tell the compiler that "attempt" takes no arguments and will return an int value. Then at the end of the function we use return to do exactly that.

return can only pass back one value - again, later you will learn about pointers and references, which will allow you to return something larger than a single value.

To receive this value to your main:

int main() {

    int answer = (rand() % 10) + 1;

    srand(time(0));    

    std::cout << "I am thinking of a number between 1 and 10. Try to guess it in 3 attempts!";
    std::cout << endl;

    int guess = attempt();
    std::cout << "You guessed: " << guess << '\n';

    cin.get();
    return 0;
}

Or you can break the variable declaration and use into two lines:

int guess;
...
guess = attempt;

A key thing to take away from all this is that you can have different variables with the same name in different 'scopes':

int i = 1;  // global, visible to all functions in this compilation unit.

int main()
{
    srand(time());
    int i = 2;

    if ((rand() % 4) == 0) // 1 in four chance
    {
         // this is essentially a new scope.
         int i = 10;
    }

    // here, i = 2.
}

void foo()
{
    // here, i = 1 - we see the global since we didn't declare our own.
}

void foo2(int i)
{
    // here, i is whatever value we were called with.
    if (i == 1)
    {
        int i = 99;
    }
    // we're back to the i we were called with.
}
kfsone
  • 23,617
  • 2
  • 42
  • 74
0
  1. You function

    int attempt(int guess)
    ^^^
    

    need to return a int value.

  2. When you call this function in main body, call it properly with input parameters, so change

    attempt();
    

    to

    attempt(guess);
    
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • 1
    While certainly a good point, this isn't a guaranteed error. It's typically a warning. – chris Jan 29 '14 at 02:31
0

Your function is declared as

int attempt(int guess);

that is it has 1) to accept an argument of type int and 2) to return an object of type int.

And 1) it is called without an argument

attempt();

2) and does not return any object of type int.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335