首页 \ 问答 \ 做... while循环意外行为(Do… while loop unexpected behavior)

做... while循环意外行为(Do… while loop unexpected behavior)

快速解释:我希望将随机数分配给单词,以便每个字母都有不同的数字。 为方便起见,我使用了数组而不是单独的一个字母变量; 这样我就知道“abc [1]”是单词“abc”中的字母'b'。 一个单词的第一个数字使用不同的范围,以避免使用像“075”这样的数字

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

int abc[2];
int def[2];

void setRandom()
{
    abc[0] = rand() %9+1;
    do {abc[1] = rand() %10;} while (abc[1] == abc[0]);
    do {abc[2] = rand() %10;} while (abc[2] == abc[1] || abc[2] == abc[0]);

    printf("RANDOM abc %d %d %d", abc[0], abc[1], abc[2]); //printed just fine.

    do {def[0] = rand() % 9 +1; } while (def[0] == abc[2] || def[0] == abc[1] || def[0] == abc[0]); //code seems to be stuck here
    do {def[1] = rand() %10;} while (def[1] == def[0] || def[1] == abc[2] || def[1] == abc[1] || def[1] == abc[0]);
    do {def[2] = rand() %10;} while (def[2] == def[1] || def[2] == def[0] || def[2] == abc[2] || def[2] == abc[1] || def[2] == abc[0]);

    printf("RANDOM def %d %d %d", def[0], def[1], def[2]); //THIS CODE IS NEVER REACHED. WHY?
}

int main ()
{
    setRandom();
    printf("RANDOM SET");
}

我知道这些数字不是真正随机的。 我知道代码很乱。 我知道这对蛮力数字来说是无效的。 我知道这种设置数字的方式很糟糕,但我想不出更好的事情。 此代码将完成我的工作。

问题是上面的代码在生成def [0]的数字时卡住了; 它将无限生成新的随机数,它似乎完全忽略了“while”循环的一部分。

你可能会建议一个更好的方法来做我正在尝试做的事情,或者只是解决问题的方法,要么会做得很好,因为项目不是很大,它只是用作拼图求解器。


Quick explanation: I want to assign random numbers to words, in a way so every letter has different number. for convenience purposes I have used arrays instead of separate one letter variables; this way I know that "abc[1]" is letter 'b' from word "abc". The first number of a word is using different range to avoid having numbers like "075"

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

int abc[2];
int def[2];

void setRandom()
{
    abc[0] = rand() %9+1;
    do {abc[1] = rand() %10;} while (abc[1] == abc[0]);
    do {abc[2] = rand() %10;} while (abc[2] == abc[1] || abc[2] == abc[0]);

    printf("RANDOM abc %d %d %d", abc[0], abc[1], abc[2]); //printed just fine.

    do {def[0] = rand() % 9 +1; } while (def[0] == abc[2] || def[0] == abc[1] || def[0] == abc[0]); //code seems to be stuck here
    do {def[1] = rand() %10;} while (def[1] == def[0] || def[1] == abc[2] || def[1] == abc[1] || def[1] == abc[0]);
    do {def[2] = rand() %10;} while (def[2] == def[1] || def[2] == def[0] || def[2] == abc[2] || def[2] == abc[1] || def[2] == abc[0]);

    printf("RANDOM def %d %d %d", def[0], def[1], def[2]); //THIS CODE IS NEVER REACHED. WHY?
}

int main ()
{
    setRandom();
    printf("RANDOM SET");
}

I am aware that the numbers are not truly random. I am aware that the code is messy. I am aware it's innefficient to brute-force numbers. I am aware that this way of setting numbers is bad, but I couldn't think of anything better. This code will do the job for my purposes.

The problem is that the above code gets stuck while generating number for def[0]; it will infinitely generate new random numbers, it seems to be completely ignoring 'while' part of the loop.

You may either suggest a better method of doing what I'm trying to do, or simply a solution to the problem, either will do just fine since the project isn't anything big, it's just used as puzzle solver.


原文:https://stackoverflow.com/questions/38171138
更新时间:2020-03-26 12:02

最满意答案

C / C ++中的数组是从零开始的。 创建abc[2]只允许0和1合法访问。 超出数组末尾的任何东西都是未定义的行为 - 在这种情况下, abc[2]第三个 ,而不是第二个元素)很可能指向def[0]

修复您的代码,以便两个声明分配3个元素而不是2个。


Arrays in C/C++ are zero-based. creating abc[2] give you only 0 and 1 to legitimately access. Anything beyond the end of the array is undefined behaviour - in this case it's probable that abc[2] (the third, not second element) points to def[0].

Fix your code so that both declarations allocate 3 elements instead of 2.

2016-07-04

相关文章

更多

最新问答

更多
  • css在元素之前中断列而不破坏包装器(css break column before element without breaking the wrapper)
  • 如何在Xamarin共享项目中使用自定义渲染器(How to use Custom Renderer in Xamarin Shared Project)
  • 如何为特定表中的特定字段设置唯一?(How to set unique for specific field from specific table?)
  • Google SDK iOS - sign()方法完成处理程序(Google SDK iOS - sign() method completion handler)
  • 在具有接口{}值的地图上实现String()(Implement String() on a map with interface{} values)
  • 检查数据库中是否已存在用户名(Check if username already exist in DB)
  • 使用javascript进行ajax调用时阻止用户交互(Block user interaction while doing ajax call using javascript)
  • 什么'if(err)'在Javascript中精确测试?(What does 'if (err)' tests precisely in Javascript?)
  • jQuery mouseleave无法正常工作(jQuery mouseleave not working)
  • 寻求使用的一些说明(Seeking some clarification on use of )
  • 将数组传递给注释的语法(syntax for passing array to annotation)
  • 用于从两个日期范围之间的文件中提取数据的Shell脚本(Shell script to extract data from file between two date ranges)
  • 元素隐藏但父()没有(Element hides but parent() not)
  • 如何使用Google App Engine Java平台开发web ui(How to develop web ui with Google App Engine Java platform)
  • 对于OWL A级;(For an OWL class A; Getting all properties that A is their domain)
  • Excel VBA公式格式问题(Excel VBA Formula Format Issue)
  • ORA - 02287序列号不允许在这里(ORA - 02287 sequence number not allowed here)
  • Github拉忽略特定文件(Github Pull Ignore Specific File)
  • SQL CONVERT函数在SQL Server中工作但不在应用程序中(SQL CONVERT function working in SQL Server but not in application)
  • backbone.js适用于大型应用程序(backbone.js for large applications)
  • 防止程序关闭(Preventing program from closing)
  • 生成不带图像的heightMap(Generating a heightMap without an Image)
  • Bootstrap - 如何将包含文本的div居中?(Bootstrap - How to center div that has text inside it?)
  • Android - 片段findViewById()总是null?(Android - Fragment findViewById() always null?)
  • 确定CSS中的高度(Figuring out heights in CSS)
  • 使用__autoload包含类和使用命名空间(Use __autoload to include class and use namespace)
  • setTimeout()不允许我传递文本值[重复](setTimeout() doesn't allow me to pass text values [duplicate])
  • 在NSUserDefault中恢复值(Restoring value in NSUserDefault)
  • 知道如何将这种下沉的悬停效果添加到图像/链接吗?(Any idea how to add this sinking hover effect to an image/link?)
  • 在XIB中淡入/淡出UISegmentedControl(fade in/fade out UISegmentedControl in XIB)