第二个 scanf 识别关键字以退出整数迭代? C

second scanf to recognize keyword to exit iteration of integers? C

尝试接受由空格或行分隔的整数,直到用户键入关键字 "end",此时程序将查找运算符 (+ - * /) 以对输入的整数执行操作.

我在编译时不断收到 "comparison between pointer and integer" 警告,这是有道理的,但我不确定修复它的正确方法。代码中有问题的地方有注释。

有人可以帮助我了解第二次 scanf 的正确语法以退出迭代并继续进行操作员输入。

感谢您的帮助! PS:是的,我知道有更简单的方法来编写程序,但我是 C 语言的新手,我将此作为另一个练习机会。

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

int main (void) {
   int iarray[100];
   char stop[100];
   int c = 0;
   char action[100];
   while (scanf("%d", &iarray[c]) == 1) {
         if (scanf("%s", stop) == "end")      // the issue is here
            break;
         else
             continue;
      c++;
   }
   if (c == 0) {
      printf("error");
      return 1;
   }
   scanf("%s", action);
return 0;
}

第一个问题:如果不等于"end",尝试读取字符串,然后使用sscanf将字符串转换为整数。 (比较 strcmp 而不是 ==)。

第二个问题:读数字的时候,你永远不会接触到c++。继续让它跳过它。

例如,您可以这样做:

char temp[100]
while (scanf("%s", temp) == 1) {
     if (0 == strcmp(temp,"end")) // return 0 when both equal... 
        break;
     else{
        if( 1 == sscanf(temp,"%d",&iarray[c]))
           c++; //number successfully read, increment counter. 
        else
           break; //not end nor number...  
     }
}