【问题标题】:command gets stuck for some reason命令由于某种原因卡住
【发布时间】:2017-01-25 22:15:01
【问题描述】:

我正在尝试输入用户的基本 IP 地址,但我的命令卡在了 scanf 中,之后什么也没有执行。

   int ip1,ip2,ip3,ip4;
   scanf("%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
   printf("Here");

那么,基本上“这里”永远不会被打印出来,并且命令 scanf 永远不会结束?

#include <stdio.h>
#include<math.h>
int main(void) {
char input;
char rep = 'r';
char quit = 'q';
char first = '1';
char second = '2';
input = rep;
while( input != quit) {
    printf("What type of conversion do you want? \n");
    printf("Enter 1 for 32-bit number to dot-decimal conversion, 2 for the inverse of operation: ");
        char val;
    scanf(" %c", &val);
    if( val == first) {
    } else if( val == second) {
        printf("\nEnter dot-decimal IP address:");

        int ip1,ip2,ip3,ip4;
        scanf(" %d.%d.%d.%d", &ip1,&ip2,&ip3,&ip4);
        printf("Here");
        unsigned int ip = 0,c,k,counter = 31;
        for(c = 7; c >= 0; c--) {
            k = ip1 >> c;
            if(k & 1) {
                int temp = 2,i;
                for(i = 0; i < counter;i++) {
                    temp *= 2;
                }
                ip += temp;
                counter--;
            }

        }

        for(c = 7; c >= 0; c--) {
            k = ip2 >> c;
            if(k & 1) {
                int temp = 2,i;
                for(i = 0; i < counter;i++) {
                    temp *= 2;
                }
                ip += temp;
                counter--;
            }
        }


        for(c = 7; c >= 0; c--) {
            k = ip3 >> c;
            if(k & 1) {
                int temp = 2,i;
                for(i = 0; i < counter;i++) {
                    temp *= 2;
                }
                ip += temp;
                counter--;              
            }
        }   

        for(c = 7; c >= 0; c--) {
            k = ip4 >> c;
            if(k & 1) {
                int temp = 2,i;
                for(i = 0; i < counter;i++) {
                    temp *= 2;
                }
                ip += temp;
                counter--;              
            }
        }


        printf("%u is the IP Address",ip);

    }
    printf("\n \n Enter r to repeat, q to quit:");
    scanf(" %c",&input);
}
return 0;

}

这正是我正在做的。当我尝试以十进制表示法获取 IP 地址时,它卡住了。

【问题讨论】:

  • 你是如何传递输入的?
  • 192.162.2.3(仅一例)
  • 无法复制ideone.com/Lif8Wv
  • @dave:你输入后是否输入了 Enter 键?这对我来说可以。数字之间没有空格吧?
  • @dave - 这不是 gcc 问题。我提供的链接使用 gcc 来编译代码。问题出在你没有展示的东西上。

标签: c ip bits


【解决方案1】:

我在更新后分析了你的代码(完整代码),发现问题不在scanf的输入中,而是在获取数据后执行的for循环中。

看看那个循环:

    unsigned int ip = 0,c,k,counter = 31;
    for(c = 7; c >= 0; c--) {
        k = ip1 >> c;
        if(k & 1) {
            int temp = 2,i;
            for(i = 0; i < counter;i++) {
                temp *= 2;
            }
            ip += temp;
            counter--;
        }
    }

特别是在for(c = 7; c &gt;= 0; c--) 考虑到c 是unsigned int 类型...我看到这个循环是无限的,因为从0 递减新的正值UINT_MAX(见limits.h )。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2021-08-05
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多