[英]Error in c when iterating: use of undeclared identifier i
我正在創建一個程序,它將向用戶詢問命令行參數,而用戶只需要輸入整數作為 argv[1]。 它應該拒絕除整數以外的任何輸入。 我的代碼如下:
#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;
}
}
運行上面的代碼會拋出一個錯誤:error: use of undeclared identifier 'i' for (int i = 0; n = strlen(s); i< n; i++)
我在這里做錯了什么,我該如何解決這個問題? 謝謝。
對不起,如果我的問題看起來很愚蠢,我仍然是一個在這里學習的新手,之前對 C 一無所知。 謝謝你的幫助。
如果您有 C89 編譯器,則需要將 delarations 放在作用域塊的頂部,因此必須在 for 循環之前聲明變量 i。
如果您使用 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.