简体   繁体   English

迭代时 c 中的错误:使用未声明的标识符 i

[英]Error in c when iterating: use of undeclared identifier i

I am creating a program that will ask a command line argument from the user, and the user need to input only integers as the argv[1].我正在创建一个程序,它将向用户询问命令行参数,而用户只需要输入整数作为 argv[1]。 It should reject any input other than integers.它应该拒绝除整数以外的任何输入。 My code is as below:我的代码如下:

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

int main(int argc, string argv[] )
string s = argv[1];
    for (int i = 0; n = strlen(s); i< n; i++)
    {
        if(!( isdigit(s[i])) )
        {
            printf("All numbers: correct input");
            return 1;
        }
    }

    //Else print a prompt asking for a plaintext to cipher
    else
    {
        string p = get_string("Your text here: ");
        return 0;
    }
    }

Running the above code throws me an error: error: use of undeclared identifier 'i' for (int i = 0; n = strlen(s); i< n; i++)运行上面的代码会抛出一个错误:error: use of undeclared identifier 'i' for (int i = 0; n = strlen(s); i< n; i++)

Where do I do wrong here and how do I fix this?我在这里做错了什么,我该如何解决这个问题? Thanks.谢谢。

Sorry if my questions seem stupid, I am still a newbie learning here and know absolutely nothing about C before.对不起,如果我的问题看起来很愚蠢,我仍然是一个在这里学习的新手,之前对 C 一无所知。 Thanks for the help though.谢谢你的帮助。

If you have a C89 compiler you will need to put the delarations at the top of the scope block, so the variable i has to be declared before the for loop.如果您有 C89 编译器,则需要将 delarations 放在作用域块的顶部,因此必须在 for 循环之前声明变量 i。

Try this if your using C89:如果您使用 C89,请尝试此操作:

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

int main(int argc, string argv[] ){
 string s = argv[1];
 int i = 0;
 int n = strlen(s);
 for (; i< n; i++)
    {
        if(!( isdigit(s[i])) )
        {
            printf("All numbers: correct input");
            return 1;
        }
    }

    //Else print a prompt asking for a plaintext to cipher
    else
    {
        string p = get_string("Your text here: ");
        return 0;
    }
  } //end of for loop?  TODO: fix your braces spacing the sample was broken
}//end of for main?  TODO: fix your braces spacing the sample was broken

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

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