简体   繁体   English

当我在while循环中的else语句之后使用增量运算符时,它破坏了整个程序并且它不起作用

[英]when i use Increment operator after else statment in a while loop its broke the whole program and it won't work

when i run this code in the terminal i get a blank line that just wait for nothing when i move the "i++;"当我在终端中运行此代码时,我得到一个空白行,当我移动“i++;”时它什么也不等。 under the "while(str[i]!='\0')" it works but thats not what i wanted , any help?在“while(str[i]!='\0')”下它可以工作,但这不是我想要的,有什么帮助吗?

int main(){
    printf("hello kiddo");
    char str[20]="hel a awdaw dwa";
    int i=0;
    char *cleanstring;

    while(str[i]!='\0'){
        if(str[i]==' '){
            continue;   
        }
        else{
            printf("%c",str[i]);    
        }
        i++;
    }

    return 0;
}

i dont know what wrong with this code我不知道这段代码有什么问题

In addition to Johnny Mopp's comment, notice the difference when adding the \n to your printf your program needs to flush the character and will not until the end of line, which as he says never happens since the loop gets stuck.除了 Johnny Mopp 的评论之外,请注意将 \n 添加到您的 printf 时的区别,您的程序需要刷新字符并且直到行尾才会刷新,正如他所说,自从循环卡住以来,这种情况永远不会发生。

int main() {
  printf("hello kiddo");
  char str[20] = "hel a awdaw dwa";
  int i = 0;
  char * cleanstring;

  while (str[i] != '\0') {
    if (str[i] != ' ') {
      printf("%c\n", str[i]);
    }
    i++;
  }

  return 0;
}

i solved this problem editing the "if condition" like that我解决了这个问题,像这样编辑“如果条件”

while(str[i]!='\0'){
        //if(str[i]==' '){
        //    continue;   
        //}
        //else{
        //   printf("%c",str[i]);    
        //}
        //i++;

        if(str[i]!=' '){
            strncat(cleanstring,&str[i],1);
        }           
        i++;

    }

the code was long for no reason and its better right now but , still wanna know why it didn't work in the first time.代码无缘无故地长了,现在更好了,但是,仍然想知道为什么它在第一次不起作用。

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

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