使用功能

时间:2014-03-09 14:15:01

标签: c++

我正在编写这个方法,它接受3个参数,然后将结果输出到有序行中。我明白了这一点,但由于某种原因,我在每次迭代结束时得到一个重复的数字。如:

enter image description here

这是整个代码。希望它有所帮助:

#include <iostream>
//function prototype:
int payoff(int x, int y, int z);    

//global variables:
int R1;
int R2;
int R3;
int total;

using namespace std;
int main(void) {
    cout << "R1\t R2\t R3\t\n" << endl;

    ////////////////////
    //first loop
    ////////////////////
    R1 = 1;
    R2 = 1;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //second loop
    ////////////////////
    R1 = 1;
    R2 = 2;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //third loop
    ////////////////////
    R1 = 1;
    R2 = 3;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //fourth loop
    ////////////////////
    R1 = 2;
    R2 = 1;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //fifth loop
    ////////////////////
    R1 = 2;
    R2 = 2;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //sixth loop
    ////////////////////
    R1 = 2;
    R2 = 3;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //seventh loop
    ////////////////////
    R1 = 3;
    R2 = 1;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //eight loop
    ////////////////////
    R1 = 3;
    R2 = 2;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //ninth loop
    ////////////////////
    R1 = 3;
    R2 = 3;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    system("PAUSE");
    return 0;
}
/////////////////////////////////////////////////////////
//FUNCTIONS:
/////////////////////////////////////////////////////////
int payoff(int R1, int R2, int R3) {
    do {
        //calculate payoff:
        total = R1;
        if (R2 < R1) {
            total = total + R2;
        } //end if
        else {
            if (R3 < R1) {
                total = total + R3;
            } //end if
            else {
                total = R1;
            } // end else
        } //end else

        if (R3 < R2) {
            total = total + (2 * R3);
        } //end if
        else {
            if (R3 < R1) {
                total = total + R3;
            } //end if
            else {
                total = R1;
            } //end else
        } //end else

        //display payoff:
        printf("%d\t %d\t %d\t payoff is %d\n", R1, R2, R3, total);

        R3++;
    } while (R3 < 4); //end do-while

    return total;
} //end function payoff

任何人都可以帮我弄清楚如何摆脱那个讨厌的额外号码吗?非常感谢你提前!

3 个答案:

答案 0 :(得分:5)

来自payoff()的返回代码正在printf语句中打印,这是您不想要的。您只需在没有payoff()的情况下致电printf

printf("%d", payoff(R1, R2, R3));调用payoff函数,然后输出payoff()函数返回的值。

所以替换以下行:

 printf("%d", payoff(R1, R2, R3));

 payoff(R1, R2, R3);

答案 1 :(得分:2)

当您在函数中插入1,1,1时会发生这种情况:

printf(first loop with \n)
printf(second loop with \n)
printf(last loop with \n) //because R3 will be 4 and therefore hte while-loop will end.

然后你将把总数返回到主函数中的printf函数并打印重复的行。

如果您不想要那些重复的行,您应该简单地调用您的函数并使其不返回任何内容:

而不是:

int payoff(int R1, int R2, int R3) {

do{
...
}while(r3<4);

return total;
}

改为:

void payoff(int R1, int R2, int R3) {

do{
...
}while(r3<4);

//return total;
}

然后调用函数不像你那样:

printf("%d", payoff(R1, R2, R3));

但是像这样:

payoff(R1, R2, R3);

答案 2 :(得分:1)

如果您不想显示该行,请在每个循环中删除此行:

printf("%d", payoff(R1, R2, R3));

将其更改为:

payoff(R1, R2, R3)

因为它正在打印函数结果的%d个数字,所以在付款时你已经打印了结果,所以你不需要它将它返回到主站点。

为什么不使用循环?