简体   繁体   English

无限循环,无法获取值

[英]Infinite loop, cannot get values

I've spent literally 3 hours trying to get this code to work, however whenever I try I end up in a loop and the console continuously loops. 我实际上花了3个小时来尝试使此代码正常工作,但是,每当尝试尝试时,我都会陷入循环,并且控制台不断循环。 I've tried everything - I've created a function that returns just i and reassigning values but it doesn't seem to work. 我已经尝试了所有方法-我创建了一个仅返回i并重新分配值的函数,但它似乎不起作用。

For some reason whenever I try use logic operators it doesn't want to work on me. 由于某种原因,每当我尝试使用逻辑运算符时,它都不想对我起作用。 The values of mes->hand[x] is something like "2D,4D,3C,5C,6H,7H" and so on whilst the suit is either "H", "C" , "D", "S" in reference to cards. mes-> hand [x]的值类似于“ 2D,4D,3C,5C,6H,7H”,依此类推,而西装的值为参考卡。

int newtrick_value(struct Message *mes, int suit) {
    int len = strlen(mes->hand);
    int x = 0, i = 50, y = 0;
    while (i < 63) {
        while(x < len) {
            if (((mes->hand[x] == i || mes->hand[x] == y) && mes->hand[x+1] == suit)){
                if(i>= 58){
                    return y;
                }   
                return i;
            }
            x++;
            continue;
        }
        if(i >= 57) {
            y = determine_letter(i);        
        }
        i ++;
        x = 0;
        continue;
    }
    return 0;
}

I have the following observations: 我有以下观察:

  1. Your continue statements are redundant. 您的continue语句是多余的。 You are already at the bottom of the loops. 您已经处于循环的底部。 (No harm though.) (不过没有害处。)

  2. Your numbers 50 etc all refer to ASCII characters. 您的数字50等都表示ASCII字符。 Add comments about this. 添加对此的评论。

  3. the only error I can find is in: 我唯一能找到的错误是:

     if(i >= 57) { y = determine_letter(i); } 

This should be if (i>=58) as 57 ASCII is 9 . if (i>=58)等于57 ASCII是9 Possibly determine_letter gets confused when receiving 57 (that function apparently maps cards higher than 9 onto your card encoding scheme, which you did not provide completely). 可能determine_letter接收57时迷糊(该功能显然映射高于9卡到您的卡编码方案,你并没有完全提供)。 As a result, the function can return an invalid value in return y; 结果,该函数可以在return y;return y;无效值return y; , which the calling function doesn't understand and which causes your console to loop. ,调用函数不了解,这会导致控制台循环。

Also this statement should be placed before while (x < len) , so you first map the character and then compare it and at the end of the loop increment i for the next character (card value) to compare. 此语句也应放在while (x < len) ,因此您首先映射字符,然后进行比较,并在循环增量i的末尾比较下一个字符(卡值)。

Applying these observations gives: 应用这些观察结果可得出:

int newtrick_value(struct Message *mes, int suit) {
    int len = strlen(mes->hand);
    int x = 0, i = 50, y = 0;
    while (i < 63) {
        if (i >= 58) {
            y = determine_letter(i);        
        }
        while (x < len) {
            if (((mes->hand[x] == i || mes->hand[x] == y) && mes->hand[x+1] == suit)){
                if (i >= 58) {
                    return y;
                }   
                return i;
            }
            x++;
        }
        i++;
        x = 0;
    }
    return 0;
}

Without all details of caller, sub-functions and card encoding scheme I cannot give more suggestions. 没有呼叫者,子功能和卡编码方案的所有详细信息,我无法给出更多建议。 I hope this helps. 我希望这有帮助。

Note: thinking about the functionality, the function just returns the value of the first card of the given suit. 注意:考虑功能性,该功能仅返回给定西装的第一张牌的值。 This means that the outer loop to compare the card value seems pretty useless: first find the first suit, then get its value is more efficient: 这意味着比较卡片价值的外循环似乎没有用:首先找到第一套衣服,然后更有效地获取它的价值:

int newtrick_value(struct Message *mes, int suit) {
    int len = strlen(mes->hand);
    int x = 0;

    while (x < len) {
        if (mes->hand[x] == suit)){
            return(mes->hand[x-1]);
        }
        x++;
    }
    return 0;
}

As a MWE, this prints "calling determine letter" out to the console 6 times: 作为MWE,这会向控制台打印6次“呼叫确定字母”:

#include <stdio.h>
#include <string.h>

int newtrick_value(char* hand, int suit) {
    int len = strlen(hand);
    int x = 0, i = 50, y = 0;

    while (i < 63) 
    {
        x = 0;
        while(x < len) 
        {
            printf("i%d, y%d, suit %d, hand[x] %d, hand[x+1] %d\n", i, y, suit, hand[x], hand[x+1]);
            if (((hand[x] == i || hand[x] == y) && hand[x+1] == suit))
            {
                if(i>= 58)
                {
                    return y;
                }   
                return i;
            }
            x++;
        }
        if(i >= 57) 
        {
            printf("Calling determine letter\n");
            //y = determine_letter(i);        
        }

        i++;
    }
    return 0;
}

int main(void) 
{
    newtrick_value("2H,3H", (int)"H");
    return 0;
}

As far as I can tell, I didn't change any of the logic (just some of the formatting). 据我所知,我没有更改任何逻辑(只是某些格式)。 It shouldn't be giving this behaviour, as the suit and value should match. 它不应该给出这种行为,因为西装和价值应该匹配。

Oddly, the prints gave the following: 奇怪的是,这些照片给出了以下内容:

i50, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i50, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i50, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i50, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i50, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i51, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i51, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i51, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i51, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i51, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i52, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i52, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i52, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i52, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i52, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i53, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i53, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i53, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i53, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i53, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i54, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i54, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i54, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i54, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i54, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i55, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i55, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i55, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i55, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i55, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i56, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i56, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i56, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i56, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i56, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i57, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i57, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i57, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i57, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i57, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i58, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i58, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i58, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i58, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i58, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i59, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i59, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i59, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i59, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i59, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i60, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i60, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i60, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i60, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i60, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i61, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i61, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i61, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i61, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i61, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i62, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i62, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i62, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i62, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i62, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter

So the suit value that's given by converting "H" to an integer seems particularly screwy. 因此,将“ H”转换为整数所得到的西服值显得特别棘手。

I've made an assumption about how you've represented mes->hand in your structure, but hopefully this still holds. 我已经对您如何在结构中表示mes->hand做出了假设,但希望这仍然成立。 One thing I did notice though, is why are you passing in the suit as an int , not a char ? 不过,我确实注意到的一件事是,为什么将西装作为int而不是char传递? That seems like a much more logical thing to do. 这似乎是更合乎逻辑的事情。

As such, if you're getting into an infinite loop, my suspicion is that mes->hand may not be null-terminated, meaning that strlen(mes->hand) will return some unknown value, which would mean that while(x < len) may seem like it never terminates. 因此,如果您陷入无限循环,我的怀疑是mes->hand可能不会以空值结尾,这意味着strlen(mes->hand)将返回一些未知值,这意味着while(x < len)似乎永远不会终止。 Could you inspect mes->hand to ensure that it's a valid (and expected) string? 您可以检查mes->hand来确保它是有效的(并且是预期的)字符串吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM