简体   繁体   English

初学者级。 用C编码。遇到“如果不是”的麻烦。

[英]Beginner level. Coding in C. Having trouble with “if else”.

Can someone please help. 有人可以帮忙吗? Im new to programming and doing the cs50 course. 我是编程和CS50课程的新手。 The task is to scramble plain text based on a code word. 任务是根据代码字对纯文本进行加扰。 The language is C and on linux. 语言是C,在Linux上。

I seem to have it all working but I am banging my head for the last 2 hours trying to fix the last error when compiling the program. 我似乎一切正常,但是在过去的2个小时中我一直在努力工作,试图修复编译程序时的最后一个错误。 The problem I am having is the bottom half of the program ( after the //Cipher funtion bit ) 我遇到的问题是程序的下半部分( //密码功能位之后

This is the error: 这是错误:

viginere.c:39:5: error: expected expression
else 
^
viginere.c:44:5: error: expected expression
else
^
2 errors generated.

I cant see what I have done wrong with this. 我看不到我做错了什么。

I have messed around with the {} in several different places but I dont think that is the problem as this program is a modified version of a program i made before and that one works, with the same layout (just different with a slightly different printf) What am I missing? 我在几个不同的地方弄错了{},但我不认为这是问题所在,因为该程序是我之前制作的程序的修改版本,并且该程序可以使用相同的布局(只是略有不同,而printf稍有不同) )我想念什么?

Heres my code: 这是我的代码:

int main (int argc, string argv[])  
{
//change command line string to a number
int k = atoi(argv[1]);
string v = argv[1];

//check program command line was typed correctly
if (argc != 2 || k != 0)
    { 
    printf("Restart program with keyword in command line (alphabetical characters only)\n");
    return 1;
    }

//Get Plain Text
printf("The keyword is set to %s.\nType in your plain text: ", argv[1]);
string s = GetString();

//Print Cipher
printf("Your cipher text = ");

//Set variables for mudulo on keyword
int codecount = strlen(argv[1]);
int c = 0;

//Cipher function (Errors are in this part)
for (int i = 0; i < strlen(s); i++)
    {
    //Cipher uppercase
    if (isupper(s[i]))
    printf("%c", (s[i] + v[c%codecount] - 65)%26 + 65);
    c++;   
    //Else Cipher lowercase
    else 
    if (islower(s[i]))
    printf("%c", (s[i] + v[c%codecount] - 97)%26 + 97);
    c++;
    //Print all else as normal
    else
    printf("%c", s[i]);
    }
printf("\n");    
}    

If an if or else block has more than one statement you need to put curly braces around them. 如果ifelse块包含多个语句,则需要在其周围放置花括号。 To be safe, many programmers will include braces all the time. 为了安全起见,许多程序员都会一直使用花括号。

//Cipher uppercase
if (isupper(s[i])) {
    printf("%c", (s[i] + v[c%codecount] - 65)%26 + 65);
    c++;   
}
//Else Cipher lowercase
else if (islower(s[i])) {
    printf("%c", (s[i] + v[c%codecount] - 97)%26 + 97);
    c++;
}
//Print all else as normal
else {
    printf("%c", s[i]);
}

You're missing the { } around the body of your if . 您在if主体周围缺少{ } If you don't put braces around the body, it's just the next statement. 如果您没有在身体周围套上牙套,那就只是下一个陈述。 Since your else is not immediately after that statement, it's not recognized as matching up with the if . 由于您的else不在该语句之后,因此不会将其识别为与if匹配。

if (isupper(s[i])) {
    printf("%c", (s[i] + v[c%codecount] - 65)%26 + 65);
    c++;
}
//Else Cipher lowercase
else if (islower(s[i])) {
    printf("%c", (s[i] + v[c%codecount] - 97)%26 + 97);
    c++;
}
//Print all else as normal
else {
    printf("%c", s[i]);
}

Even if you only have one statement in the body, I recommend you always put braces around it. 即使您体内只有一种陈述,我还是建议您始终将括号括起来。 See Why is it considered a bad practice to omit curly braces? 请参阅为什么省略花括号被认为是不好的做法?

You need braces around multiple statements after an if for the else to be valid: 您需要在if之后加上多个花括号,以使else有效:

int len = strlen(s);
for (int i = 0; i < len; i++)
{
    //Cipher uppercase
    if (isupper(s[i]))
    {
        printf("%c", (s[i] + v[c%codecount] - 65)%26 + 65);
        c++;
    }
    //Else Cipher lowercase
    else if (islower(s[i]))
    {
        printf("%c", (s[i] + v[c%codecount] - 97)%26 + 97);
        c++;
    }
    //Print all else as normal
    else
        printf("%c", s[i]);
    printf("\n");
}

There are those who like to use braces every time. 有些人喜欢每次都使用牙套。 I'm of the view it isn't necessary, but it is harmless and can help you, especially perhaps while you're learning. 我认为这不是必需的,但它无害,可以为您提供帮助,尤其是在您学习时。

Alternatively, in this variant of the code, you could avoid separate statements by using the increment in the printf() calls: 另外,在此代码变体中,可以通过使用printf()调用中的增量来避免使用单独的语句:

int len = strlen(s);
for (int i = 0; i < len; i++)
{
    if (isupper(s[i]))         // Cipher uppercase
        printf("%c", (s[i] + v[c++ % codecount] - 'A') % 26 + 'A');
    else if (islower(s[i]))    // Cipher lowercase
        printf("%c", (s[i] + v[c++ % codecount] - 'a') % 26 + 'a');
    else                       // Non-alphabetic
        printf("%c", s[i]);
}
printf("\n");

This version also uses 'A' in place of 65 and 'a' in place of 97 ; 此版本还使用'A'代替65并使用'a'代替97 it makes it easier for people to understand. 它使人们更容易理解。 This technique of compression won't always work, and should be used with caution even when it does work, but it can be used in code like this. 这种压缩技术并不总是有效,即使有效,也应谨慎使用,但可以在这样的代码中使用。 It also only prints a newline at the end of the string (or if there are newlines within the string), rather than placing each character on its own line. 它还仅在字符串末尾(或如果字符串中有换行符)打印换行符,而不是将每个字符放在其自己的行上。

Both variations shown also avoid using strlen(s) in the loop condition; 所示的两种变体还避免在循环条件下使用strlen(s) it is bad for performance, ultimately, though you would need to be encrypting long messages before it would become measurable. 最终,这对性能不利,尽管您需要先对长消息进行加密,然后才能进行测量。 One could also argue that the code should use putchar(char_value) instead of printf("%c", char_value) . 有人还可能认为代码应使用putchar(char_value)而不是printf("%c", char_value) However, this is unlikely to have a measurable benefit on the code for the data involved in a student exercise — but both changes could be significant if you were working on megabytes of data repeatedly. 但是,这不太可能对学生练习中涉及的数据的代码产生可观的收益-但是,如果您反复处理兆字节的数据,则这两个更改都可能是重大的。

The trouble is that you have too many statements between your if and your else ! 问题是您的ifelse之间的语句过多!

To use if...else you have to create a block or give a single statement. 要使用if...else您必须创建一个块或给出一个语句。 Really, that works out to be the same thing. 确实,这是一回事。 In other words: 换一种说法:

if(x==1) do_this();
else do_that();

This will work. 这将起作用。 But what is far more typical (and what you want) is to create a "Block": 但是,更典型的(以及您想要的)是创建一个“块”:

if(x==1) {
  do_this();
  and_that();
} else { do_some_other_things(); }

Hope that helps! 希望有帮助!

Use curly brackets... 使用大括号...

When there is more than one line to be executed as a result of an if(condition) , each must be encapsulated with curly brackets. 如果由于if(condition)而要执行多于一行,则每行都必须用大括号括起来。

if(condition)
{
    statement1();
    statement2();//without brackets this would not be executed
}

In the same way, if there are multiple lines that should be executed if and only if the if(condition) is false , they must also be surrounded with curly brackets starting after the else keyword. 同样,如果且仅当if(condition)为false时 ,应执行多行,它们也必须在else关键字之后用大括号括起来。

...
else
{
    statement3();
    statement4();//without brackets, this would not be executed
}

There are different opinions on if curlys should be used for only single lines after if and else , but readability, maintainability and reliability are reasons enough to use curly brackets religiously. 对于ifelse之后的大写是否只用于单行有不同的看法,但是可读性,可维护性和可靠性是足以虔诚地使用大括号的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM