Ganondalf
Ganondalf

Reputation: 73

How can I run multiple functions after an if statement

How can I have a the return and cout both take place as a result of being true or false?

gameState computerMove(gameState state) {
    if (sizeOfPile(state,1) > 0)
         return removeCoinsFromPile(state,1,1);
         cout << "I take 1 coin from Pile 1." << endl;
    else
          return removeCoinsFromPile(state,1,2);
          cout << "I take 1 coin from Pile 2" << endl;

Upvotes: 0

Views: 754

Answers (3)

kfsone
kfsone

Reputation: 24249

C++ uses the concept of a "compound statement" - a statement made up of multiple sub-statements - denoted by brackets, { and }, just as you used for your function.

gameState computerMove(gameState state) {
}

You can use a compound statement pretty much anywhere you can use a regular statement:

int sum = 0;
for (int i = 0; i < 100; ++i)
    sum += i;

or

int sum = 0;
for (int i = 0; i < 100; ++i) {
    sum += i;
}

These do the same thing, but if we need to add more instructions inside the for, we add them between the '{'s.

One important thing about this: a compound statement is considered to have "scope"

int i = 1;
{
    int i = 2;
    std::cout << "inside, i = " << i << '\n';
}
std::cout << "outside, i = " << i << '\n';

Programmers sometimes use this to maintain lifetime of advanced objects. The following code opens a file and releases it as soon as we're done with it - because that's what happens when the 'istream' object goes out of scope.

std::string instructions;
{
    std::istream file("instructions.txt");
    file >> instructions;
} // <-- 'file' goes away, which closes the file.

However: the return keyword causes a function to end and return a value. So you're going to have to do that after your cout.

if (sizeOfPile(state,1) > 0) {
     cout << "I take 1 coin from Pile 1." << endl;
     return removeCoinsFromPile(state,1,1);
}

Upvotes: 2

Matt
Matt

Reputation: 437

You could move the return statement to after the cout statement.

gameState computerMove(gameState state) {
    int res;
    if (sizeOfPile(state,1) > 0) {
        cout << "I take 1 coin from Pile 1." << endl;
        res = removeCoinsFromPile(state,1,1);
    } else {
        cout << "I take 1 coin from Pile 2" << endl;          
        res = removeCoinsFromPile(state,1,2);
    }
    return res;
}

This website might be helpful when learning about functions in c++.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Your function ends after the return statement, so you should prepare what to return in a variable, print it, and then do the return, like this:

int res;
if (sizeOfPile(state,1) > 0) {
     res = removeCoinsFromPile(state,1,1);
     cout << "I take 1 coin from Pile 1." << endl;
} else {
      res = removeCoinsFromPile(state,1,2);
      cout << "I take 1 coin from Pile 2" << endl;
}
return res;

However, in your specific case you can rewrite this without an if by using a conditional operator:

int pile = sizeOfPile(state,1) > 0 ? 1 : 2;
res = removeCoinsFromPile(state, 1, pile);
cout << "I take 1 coin from Pile " << pile << endl;
return res;

Upvotes: 1

Related Questions