1

I'm building a Toothpick Game of 23 in C++ as an homework assignment in my Programming course. I'm almost finished with the code and the output looks exactly like the one that I am suppose to follow.

I am supposed to use a function in my code but I don't know how to use the function. Everything in the program works like it should except the function returns 0 and that 0 is the last line of the output and that's the line that is not identical to the output I am supposed to follow. So maybe someone can help my find out how I can get this right.

#include <iostream>
using namespace std;

int computerMove(int numPicksLeft, int humanNumber);

int main()
{
    int a, z=0, y=0;

    a = computerMove(z, y);
    cout << a;


    return 0;
}

int computerMove(int numPicksLeft, int humanNumber) {

    int number_left=23, n, cpu_turn;

    do{
        cout << "There are " << number_left << " toothpicks left. Pick 1, 2 or 3 toothpicks: ";
        cin >> n;


        if (n <= 3)
        {
            number_left -= n;
            if (number_left > 4)
            {
                cpu_turn = (4 - n); // þar sem n er fjöldi tannstöngla dregnir af notanda.
                cout << "I pick " << cpu_turn << " toothpicks" << endl;
                number_left -= cpu_turn;
            }
            else if (number_left == 2)
            {
                cpu_turn = 1;
                cout << "I pick " << cpu_turn << " toothpicks" << endl;
                number_left -= cpu_turn;
            }
            else if (number_left == 3)
            {
                cpu_turn = 2;
                cout << "I pick " << cpu_turn << " toothpicks" << endl;
                number_left -= cpu_turn;
            }
            else if (number_left == 4)
            {
                cpu_turn = 3;
                cout << "I pick " << cpu_turn << " toothpicks" << endl;
                number_left -= cpu_turn;
            }
            else if (number_left == 1)
            {
                cpu_turn = 1;
                cout << "I pick " << cpu_turn << " toothpicks" << endl;
                cout << "You won!" << endl;
                number_left -= cpu_turn;
            }
            else if (number_left == 0)
            {
                cpu_turn = 0;
                cout << "I pick " << cpu_turn << " toothpicks" << endl;
                cout << "I won!" << endl;
            }
        }
        else
            cout << "Invalid input. Try again." << endl;

    } while (number_left > 0);

    return 0;
}

I always get this 0 in the last line and I don't want that. So my question is. How can I use this function so it won't be like this?

Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
drleifz
  • 189
  • 3
  • 12

1 Answers1

1

The function signature indicates the return type of the function is an integer (int). The function itself will finally return 0 if it reaches the end which you assign and print in your main function. If you are not interested in the result of the function why have it return something at all?

You could change the return type to void and not return/assign anything and the 0 would be left out.

void computerMove(int numPicksLeft, int humanNumber) {
    // Your code
    // No return statement!
}

Something like this for example.

As a side note avoid using namespace std;.

Community
  • 1
  • 1
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
  • There is no reason to avoid `using namesace std;` in a short, self-contained program like this one. Why give yourself more work? – TonyK Sep 13 '14 at 22:46
  • Because he might have to extend on this program depending on the content of the course. – Floris Velleman Sep 13 '14 at 22:48
  • 2
    @TonyK awesome attitude. Clearly, you haven't really discovered the nice ways in which using namespaces can make your life miserable. – sehe Sep 13 '14 at 22:52
  • 3
    @TonyK Here it's just _proper instruction_. People come to SO and just take code... rather indiscriminately. It's better to have the code up to certain standards. I _can_ make a judgement call on my own code, but it's not about that here – sehe Sep 13 '14 at 23:05
  • 2
    @TonyK You should not forget that you are _teaching_, and therefore have a burden of responsibility. Advising the avoidance of `using namespace std` by default, and only adding it back in under controlled conditions, is clearely desirable. – Lightness Races in Orbit Sep 13 '14 at 23:09
  • @TonyK there's no reason to *use* `using namespace std;` in a short, self-contained program like this one. Typing 20 more characters doesn't take more than a couple of seconds, and it'll save you much more than that when you transfer the habit to other programs you write. Do you really, seriously, honestly, think that the time and effort spent writing that one line *is a problem*? – jalf Sep 13 '14 at 23:14