[英]CS50 How to fix error: use of undeclared identifier 'i'?
I'm working on my PSet2 Caesar Problem.我正在处理我的 PSet2 凯撒问题。 After finishing my code here is the mistakes I got.
完成我的代码后,这里是我得到的错误。 Any advice how to fix them?
任何建议如何解决它们? Really appreciated.
非常感谢。
caesar.c:46:10: error: use of undeclared identifier 'i'
for (i = 0; i < strlen(plaintext); i++)
^
caesar.c:46:17: error: use of undeclared identifier 'i'
for (i = 0; i < strlen(plaintext); i++)
^
caesar.c:46:40: error: use of undeclared identifier 'i'
for (i = 0; i < strlen(plaintext); i++)
^
caesar.c:48:31: error: use of undeclared identifier 'i'
if (isupper(plaintext[i]))
^
caesar.c:50:39: error: use of undeclared identifier 'i'
printf("%c", (((plaintext[i] - 65) + k) %26) + 65);
^
caesar.c:52:36: error: use of undeclared identifier 'i'
else if (islower(plaintext[i]))
^
caesar.c:54:39: error: use of undeclared identifier 'i'
printf("%c", (((plaintext[i] - 97) + k) %26) + 97);
^
caesar.c:58:36: error: use of undeclared identifier 'i'
printf("%c", plaintext[i]);
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Check that there is one command-line argument
if (argc != 2)
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n");
//Define the key
string key = argv[1];
//Check if input is a digit
for (int i = 0; i < strlen(argv[1]); i++)
{
if (!isdigit(argv[1][i]))
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n%s\n", key);
}
//Get plain text from user
string plaintext = get_string("Plaintext: ");
//Define key
int k = atoi(key);
printf("ciphertext: ");
//Obtain ciphertext
for (i = 0; i < strlen(plaintext); i++)
{
if (isupper(plaintext[i]))
{
printf("%c", (((plaintext[i] - 65) + k) %26) + 65);
}
else if (islower(plaintext[i]))
{
printf("%c", (((plaintext[i] - 97) + k) %26) + 97);
}
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
}
"use of undeclared identifier 'i'" means that you have not declared the identifier i
before using it - the compiler doesn't recognize the name. “使用未声明的标识符'i'”意味着您在使用之前没有声明标识符
i
- 编译器无法识别该名称。
In this case the code should be for (int i = 0; ...
在这种情况下,代码应该是
for (int i = 0; ...
Good to see you're doing cs50.很高兴看到你在做 cs50。 You have missed a little, yet important thing.
你错过了一点,但很重要的事情。 Since you have not declared the variable
i
outside any loop, thus, it is only available to that loop.由于您没有在任何循环之外声明变量
i
,因此,它仅适用于该循环。 So, when you are using it anywhere outside the loop, it's actually undeclared variable, and that's what is happening here.所以,当你在循环之外的任何地方使用它时,它实际上是未声明的变量,这就是这里发生的事情。 So, you need to declare
i
in the last for
loop too.因此,您也需要在最后一个
for
循环中声明i
。 I've attached the modified code.我附上了修改后的代码。
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Check that there is one command-line argument
if (argc != 2)
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n");
//Define the key
string key = argv[1];
//Check if input is a digit
for (int i = 0; i < strlen(argv[1]); i++)
{
if (!isdigit(argv[1][i]))
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n%s\n", key);
}
//Get plain text from user
string plaintext = get_string("Plaintext: ");
//Define key
int k = atoi(key);
printf("ciphertext: ");
//Obtain ciphertext
for (int i = 0; i < strlen(plaintext); i++) // declared i
{
if (isupper(plaintext[i]))
{
printf("%c", (((plaintext[i] - 65) + k) %26) + 65);
}
else if (islower(plaintext[i]))
{
printf("%c", (((plaintext[i] - 97) + k) %26) + 97);
}
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.