【问题标题】:Forbids comparing pointer with integer?禁止将指针与整数进行比较?
【发布时间】:2015-03-24 11:21:45
【问题描述】:

我正在编写一个 farkle 游戏,即使我的 if 语句除了两个以外的所有语句都是布尔值,我也遇到了这个问题。

另外,我将如何让用户掷特定的骰子? BloodshedDev/其他 IDE 似乎还不能在 Windows 8 中运行,所以我正在使用 Ideone 并且在这方面的运气为零。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//global vars
int bubblehold;
int roll[6];
int score;
int bank;
int dicecatch;
int tempscore = 0;
int dicenum = 6;

//function prototype
void bubblesort(int[], int);
bool straightsix();
bool sixkind();
bool fivekind();
bool fourkind();
bool threekind();
int onefive();

int main() {
    srand(time(0));
        for(int i=0; i=dicenum-1; i++){
        roll[i] = rand()%6+1;
    }
    bubblesort(roll,dicenum);
    for(int i=0; i=dicenum-1; i++){
        cout<< roll[i] <<", ";
    }

    if(straightsix == true){
        tempscore = 1500;

    }
    else if(sixkind == true){
        tempscore = 3000;

    }
    else if(fivekind == true){
        tempscore = 2000;

    }
    else if(fourkind == true){
        tempscore = 1000;
    }
    else if(threekind == true){
        cout<< tempscore;
    }
    else if(onefive == 1){
        tempscore = dicecatch * tempscore;
        cout<< tempscore;
    }
    else if(onefive == 5){
        tempscore = dicecatch * tempscore;
        cout<< tempscore;
    }

    return 0;
}
void bubblesort(int a[], int x){
    for (int i = 0; i < x-1; i++){
        bubblehold = a[i+1];
        a[i+1] = a[i];
        a[i] = bubblehold;
    }
}
bool straightsix(){
    for(int i=0; i<5; i++){
        if(roll[i]+1 != roll[i+5]){
            return false;
        }
    }
}
bool sixkind(){
    for(int i=0; i=5; i++){
        if (roll[i] != roll[i+5]){
            return false;
        }
    }
}
bool fivekind(){
    for(int i=0; i=4; i++){
        if (roll[i] != roll[i+4]){
            return false;
        }
    }
}
bool fourkind(){
    for(int i=0; i=3; i++){
        if (roll[i] != roll[i+3]){
            return false;
        }
    }
}
bool threekind(){
    for(int i=0; i=2; i++){
        if (roll[i] != roll[i+2]){
            return false;
        }
        else if(i=1){
            dicecatch = 3;
            tempscore = dicecatch * 100;
            return true;
        }
        else{
            dicecatch = i;
            tempscore = dicecatch * 100;
            return true;
        };
    }
}

int onefive(){
    for(int i=0; i=dicenum; i++){
        if(roll[i] == 1){
            dicecatch = dicecatch + 1;
            tempscore = tempscore + 100;
            return 1;
        }
        else if(roll[i] == 5){
            dicecatch = dicecatch + 1;
            tempscore = tempscore + 50;
            return 5;
        }
    }
}

【问题讨论】:

  • 我想你的意思是if(straightsix() == true)等等等等。
  • “我遇到了这个问题” 什么问题?在哪里?
  • 你不应该在没有测试的情况下编写这么多代码。
  • @user657267:或者,好多更好,if (straightsix())。它已经是一个布尔值;为什么要添加无用的比较?
  • 您可能已经注意到您的很多代码都受到了负面关注。由于您是 C++ 新手,我认为 @Beta 在这里给出了最好的建议。编写一个小得多的程序,然后看看它是否有效。然后加一点代码,看看能不能用。

标签: c++ dice


【解决方案1】:

扩展一点answer of R Sahu,区别实际上只是添加了括号,但我想添加原因。

在 C++(以及我所知道的所有其他 C 风格的语言)中,当您指的是带括号的函数时,这是一个很大的不同。将它们放在上面意味着代码将调用函数,执行它的所有代码,并且它返回的任何内容都将用于之后的比较。括号也是传递参数的基本语法,即使在调用时不传递任何参数也是强制性的,这与基本风格的语言和许多其他语言不同。

省略括号具有完全不同的含义。它不会指示函数的执行,而是将pointer 返回给函数,并且永远不会运行它的任何代码。该指针显然不是布尔值,也不是 int,这很可能会导致编译器错误。 main 中的所有 if 都有这个问题。

函数调用的正确用法是对timesrand的初始调用,包括其中的括号和参数(零)。

【讨论】:

    【解决方案2】:

    在这一行,

    if(straightsix == true){
    

    您正在将函数指针与true 进行比较。您有类似的陈述涉及其他功能。

    也许你打算使用:

    if(straightsix() == true){
    

    在这种情况下,可以简化为:

    if(straightsix()){
    

    【讨论】:

    • 删除== true。没用。
    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多