简体   繁体   English

C语言中我的while循环条件错误的原型函数

[英]Prototype function with my while loop condition error in C

How could I run my own prototype function with my while loop condition? 我如何在while循环条件下运行自己的原型函数?

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

msghere(char *text){
    printf("%s",text);
    return 0;
}
void main(){
    char inp[256]={0};
    clrscr();
    while( strcmp(inp,"pass") && msghere("Error!")){
        memset(inp,0,strlen(inp));
        printf("Type \"pass\": ");
        gets(inp);
    }
    msghere("Right Answer!");
    getch();
}

This code prints an output of: 该代码输出以下内容:

Error!Right Answer!

What you want is a do-while loop and something like an if condition. 您想要的是一个do-while循环和类似if条件的东西。

int msghere(char *text){
    printf("%s",text);
    return 1;
}
int main(void)
{    
    do
    {
    //your code
    }while( (strcmp(inp, "pass") == 0 ? 0 : msghere("error!")) );
}

Why the do-while? 为什么要同时做?
Because you want your user to make an input before you check it the first time. 因为您希望用户在第一次检查之前进行输入。 Logical right? 符合逻辑吗?

WTF is " while( (strcmp(inp, "pass") == 0 ? 0 : msghere("error!")) ) "? WTF是“ while( (strcmp(inp, "pass") == 0 ? 0 : msghere("error!")) ) ”?
First of all: bad coding style. 首先:不良的编码风格。 It is a short version if/else. 如果/否则为简短版本。 if the first condition is true return the value after the ? 如果第一个条件为true,则返回?之后的值。 otherwise return the value after : 否则返回以下值:

Why return 1; 为什么返回1; in msghere()? 在msghere()中?
Because your do while loop will evaluate if there was an error. 因为您的do while循环将评估是否存在错误。 Error == True -> do it again. 错误== True->再做一次。

What you should do: 您应该做什么:
is something like the following: 如下所示:

// your original msghere
int main(void)
{
  int passed = 0;  //false
  // some code
  while(!passed) //while not passed
  {
    //read input with fgets like said in the comments
    if(strcmp(inp, "pass") == 0)
    {
       passed = 1; // true
    }
    else
    {
      msghere("error");
    }
  }
}

it uses a status variable and is much more easyier to read. 它使用状态变量,并且更易于阅读。

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

int msghere(const char *text)
{
    printf("%s",text);
    return 1; /* you want 1 instead of 0, because (strcmp != 0) && 1 == true */
}

int main(void)
{
    char inp[256];

    clrscr();
    do {
        printf("Type \"pass\": ");
        fgets(inp, sizeof inp, stdin); /* gets is deprecated, use fgets */
    } while (strcmp(inp, "pass\n") && msghere("Error!"));
    msghere("Right Answer!");
    getch();
    return 0;
}

EDIT: 编辑:

Why is there \\n after pass in while((strcmp(inp, "pass\\n") && msghere("Error!")) 为什么在传递while((strcmp(inp, "pass\\n") && msghere("Error!"))之后有\\n while((strcmp(inp, "pass\\n") && msghere("Error!"))

Because fgets puts an extra \\n at the end of the string, you can skip this new line using: 因为fgets在字符串的末尾加上了\\n ,所以您可以使用以下命令跳过这一行:

if (fgets(inp, sizeof inp, stdin) != 0)
{
    size_t len = strlen(inp);

    if (len > 0 && inp[len - 1] == '\n')
        inp[len - 1] = '\0';
    /* ... */
}

Seems that you are using Turbo C or an old compiler, use a modern compiler (eg. MinGW), furthermore: 似乎您使用的是Turbo C或旧的编译器,请使用现代的编译器(例如MinGW),此外:

  • There is no need to initialize inp 无需初始化inp
  • There is no need to memset on each iteration 无需在每次迭代中进行memset设置
  • Functions must return some type (in this case int ) or void 函数必须返回某种类型(在这种情况下为int )或void
  • The const qualifier explicitly declares a data object as something that cannot be changed, use it in order to help the compiler build better code. const限定符将数据对象显式声明为无法更改的对象,请使用它来帮助编译器构建更好的代码。
  • Use int main(void) instead of void main() 使用int main(void)代替void main()

The strcmp() function return's an integer less than, equal to, or greater than zero if string1 (or the first n bytes thereof) is found,respectively, to be less than, to match, or be greater than string2. 如果发现string1(或其前n个字节)分别小于,匹配或大于string2,则strcmp()函数返回的整数小于,等于或大于零。

Therefore your call to strcmp in while loop retuns non-zero value.so, the next condition is evaluated ie, calling the function(msghere) . 因此,您在while循环中对strcmp的调用返回非零值。因此,将评估下一个条件,即调用function(msghere) The function executes, prints the result and returns zero, which makes the condition to be false in while loop. 该函数执行,打印结果并返回零,这使得在while循环中条件为假。

Now, you know what to do. 现在,您知道该怎么办了。

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

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