| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 428 人关注过本帖
标题:我写了一个小程序,自己在单步debug时,程序进不了for()循环,大伙看看。
只看楼主 加入收藏
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
结帖率:90.91%
收藏
已结贴  问题点数:20 回复次数:4 
我写了一个小程序,自己在单步debug时,程序进不了for()循环,大伙看看。
程序代码:
#include <stdio.h>
#include <ctype.h>
#define _STDC_WANT_LIB_EXT1_ 1
#define max_len 4
long input_num(void);
int main (void)
{
    long num = 0L;
    char j = '0';
    printf("This sheet for \"printable character\"\n");
    for(int i =0; i<=127; ++i)
    {        
        j = (char)i;
        if(isgraph(j))
      {
          printf(" number %d is symbol \'%c\'\t", i,j);
           if(i%2==0)
               printf("\n\n");        
      }
        else
            continue;
    }
    printf("This sheet for \"printable character\"\n");
    printf("Plese input number to match the character! \nthis program will output the character.\n");
    //i = 0;
    num = input_num();
    if(num>=0 && num<=32)
        printf("\n\nthe number %ld was not a printable character, but also can print \'%c\' \n\n", num, num);
    else if (num == 127)
        printf("\n\nthe number %ld was not a printable character, but also can print \'%c\' \n\n", num, num);
    else
        printf("\n\nthe number %ld was a printable character, the character is \'%c\' \n\n", num, num);
return 0;
}
long input_num (void)
{
    char xy[max_len] = {'0'};
    int test = 0;
    size_t str_len = 0;
    long int xx = 0;
    //printf("\nThis progarm is build a Mult-sheet.");    
    do
    {
        test = 0;
        printf("\nPlase input a number(0 to 127) : ");
        fflush(stdin);
        int ret_scan = scanf_s("%s", xy, sizeof(xy));
        if(ret_scan == EOF)
        {
            printf("Error reading,input overflow!!\n");
            return 1;
        }
        str_len = strnlen_s(xy,sizeof(xy));

        //printf("str_len is %zd\n", str_len);

        if(str_len == 0)
        {
            printf("strnlen_s() function error!!");
            return 2;
        }

        //fflush(stdin);

        for(int i = 0; i<str_len; ++i)   //问题就在这!程序再运行到这里时无法进入for()循环,why?
        {
            if((xy[i]>='0') && (xy[i]<='9'))    //按道理程序要进入for()循环修改标志位 test.           
               test = 0;
            else
               test = 1;                      
        }
        xx = atol(xy);
        if((xx>=0) && (xx<=127))
            test = 0;
        else
            test = 1;
    }
    while(test == 1);
    //xx = atol(xy);
    return xx;
}



这个程序目的先是输出0~127所对应的可打印字符(printable character),然后提示用户输入0~127的数字,然后输出对应的可打印字符。我的子函数input_num()的for()循环是检查每一位输入的字符是否为数字,然后修改标志位test。若test==1则表示用户输入存在错误。我的程序怎么无法进入for()循环?在debug时发现在进入for循环之前i就满足跳出循环条件了。。。
2015-03-20 22:37
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:20 
進入不了循環,就是循環條件不成立,檢查str_len的値!

授人以渔,不授人以鱼。
2015-03-20 22:47
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
收藏
得分:0 
以下是引用TonyDeng在2015-3-20 22:47:00的发言:

進入不了循環,就是循環條件不成立,檢查str_len的値!

我前面printf了str_len的值,是我说需要的值。但是在编译器的窗口看到的str_len的值则是一长串垃圾值。
2015-03-20 23:07
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
以實際運行結果爲準,不要迷信調試器。你運行print()出來的正確,那麽它進去了沒有?在循環内部寫一句helloworld,看有沒有出現,即知流程有沒有進入,這比用調試器更可靠,也才是正宗的調試方法。調試器會干擾程序真實執行環境,這一點我說過很多次的。

授人以渔,不授人以鱼。
2015-03-20 23:10
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
掃了一眼,你sizeof(xy)在邏輯上是錯的,這程序肯定運行不正確。

授人以渔,不授人以鱼。
2015-03-20 23:13
快速回复:我写了一个小程序,自己在单步debug时,程序进不了for()循环,大伙看看 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018617 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved