简体   繁体   English

CS50 如何修复错误:使用未声明的标识符“i”?

[英]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.

相关问题 CS50 恢复 - 我如何使用 sprintf? 错误:使用未声明的标识符“文件名”; 您指的是 'rename' 吗? - CS50 Recover - How do I use sprintf? error: use of undeclared identifier 'filename'; did you mean 'rename'? C - 在函数中使用未声明的标识符。 CS50 - C - use of undeclared identifier in a function. CS50 尝试从 cs50 编译 pset2 的可读性 .. 给出相同的错误“使用未声明的标识符”将不胜感激任何建议 - Trying to compile pset2 readability from cs50 .. Gives same errors “use of undeclared identifier” would appreciate any suggestions 如何修复 C 中的错误“使用未声明的标识符 n”? - How do I fix error "use of undeclared identifier n" in C? CS50 Pset3 错误:预期标识符或“(” - CS50 Pset3 error: expected identifier or '(' CS50 Pset1 Cash 错误“预期标识符或‘(’”是什么意思? - CS50 Pset1 Cash error "expected identifier or '('" meaning? "<i>CS50 Assignment Issue;<\/i> CS50 作业问题;<\/b> <i>expected identifier or &#39;(&#39;<\/i>预期标识符或“(”<\/b>" - CS50 Assignment Issue; expected identifier or '(' =CS50 PSET 2 CAESAR= 如何将密钥转换为数字? 如果我使用 atoi() 它会给我这个错误 - =CS50 PSET 2 CAESAR= How do I convert the key to digit? It gives me this error if I use atoi() 如何修复这些错误? CS50 Pset1 学分 - How do I fix these errors? CS50 Pset1 Credit 如何修复这个特定的“未声明的标识符”错误? - How do i fix this specific "undeclared identifier" error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM