簡體   English   中英

不斷獲取未聲明的標識符,我會出錯

[英]Keep getting undeclared identifier I error caesar

我正在通過CS50課程來研究pset2,但是我不明白為什么我一直收到我沒有聲明我的錯誤,因為我認為我確實這樣做了。首先,我要求提供一個數字作為鍵加密,然后我要求輸入純文本,該文本應按給定的數字進行加密,然后打印出來。

這是我的代碼:

int main(int argc, string argv[])
{
    // get key from command line argument, return 1 if wrong
    if (argc < 2)
    {
        printf("No value entered!\n");
        return 1;
    }

    //store key in integer
    int k = atoi(argv[1]);

    if (k < 0)
    {
        printf("No right variable detected\n");
        return 1;
    }
    else
    {
        printf("Plain text: \n");
        string s = get_string();

        // iterate over strings in argv
        for (int i = 0; n = strlen(s); i < n; i++);
        {
            if (isalpha(s[i]))
            {
                // for capitalized letters
                if (isupper(s[i]))
                {
                    int a = s[i] - 65;
                    int b = (a + k) % 26;
                    int c = b + 65;
                    printf("%c", c);
                }

                //for lowercase
                else
                {
                    int d = s[i] - 97;
                    int e = (d + k) % 26;
                    int f = e + 97;
                    printf("%c", f);
                }
            }
            else
            {
                //for non alphabetical characters
                printf("%c", s[i]);
            }
        }
    }
    // print new line 
    printf("\n");
    return 0;
}

對於循環錯誤,它接受3個參數,將其設置為4。

另外,在for循環后注意分號。

這行:

for (int i = 0; n = strlen(s); i < n; i++);

應該:

for (int i = 0, n = strlen(s); i < n; i++)

注意逗號,最后不要使用分號

你有一個; 在for循環的末尾

for (int i = 0; n = strlen(s); i < n; i++);
                                          ^

更改為

for (int i = 0, n = strlen(s); i < n; i++)

此外,如可以看到的init必須在第一分號之前被放置, (逗號)隔開。

旁注

  1. 您應該避免在代碼中使用“幻數”。 就您而言,您可以簡單地使用
  2. 您應該使用可以使代碼更具可讀性的變量名稱
  3. 您可以使用單個變量而不是6灰化

     if (isalpha(s[i])) { int ashed; // for capitalized letters if (isupper(s[i])) { ashed = s[i] - 'A'; ashed = (ashed + k) % 26; ashed += 'A'; } //for lowercase else { ashed = s[i] - 'a'; ashed = (ashed + k) % 26; ashed += 'a'; } printf("%c", ashed); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM