在do while循环中出现“预期表达式”错误

时间:2020-05-20 03:32:14

标签: c cs50

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    //Get the plain text
    do
    {
        string pt = get_string("plaintext: ");
    }
    while (argc == 1);
    else 
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }


}

任何原因在“ else”行上出现“期望表达式”错误?它说预期的表达是 “ else {”,但是即使我将括号移到其他地方,它仍然会给我错误。也是我第一次在堆栈上发布文章,也是我的第一个程序,因此,如果需要发布有关此问题的其他详细信息,请告诉我。

2 个答案:

答案 0 :(得分:1)

else不能自立。您需要在if之前输入else。显然你想要的是

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    //Get the plain text
    if(argc == 1)
    {
        string pt = get_string("plaintext: ");
    }
   else 
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

即使您的程序已编译,如果argc为1,它也将永远循环,因为argc的值永远不会改变,条件while(argc == 1)永远是正确的。

答案 1 :(得分:1)

“ else”在代码中某处之前需要“ if”语句... 例: 如果{ ... } 其他{ ... }

相关问题