简体   繁体   English

For 循环未在 c 语言 cs50 ide 中运行

[英]For loop not running in c language cs50 ide

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

string crypt(string msg, int key);

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Sorry\n");
        return 1;
    }
    else
    {
        string h = get_string("ipt: ");
        h = crypt(h, atoi(argv[2]));
    }
}

string crypt(string msg, int key)
{
    string c = "";
    printf("%s\n", msg);
    int len = strlen(msg);
    for(int i = 0; i > len; i++)
    {
        char t = (msg[i] + key) % 26;
        printf("%c", t);
    }
    return c;
}

In this code it says Segmentation Fault在这段代码中,它说分段错误

I Don't Understand the Problem我不明白问题

I'm Trying a lot But Don't get it I'm not trying to access Memory "That Doesn't belong to me" as the wiki says:-我尝试了很多但不明白我不想访问 Memory “那不属于我” ,正如维基所说: -

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”核心转储/分段故障是由访问“不属于您”的 memory 引起的一种特定错误。

I'm Using the cs50 ide on "Windows Microsoft Edge" My Computers Ram is 4 GB .我在“Windows Microsoft Edge”上使用cs50 ide我的电脑内存是4 GB

This code is supposed To Output the Coded version of the input: Text.此代码应为 Output input:文本。

Plz help I Don't Understand The problem.请帮助我不明白这个问题。

The Error Appears After The text Is inputted.输入文本后出现错误。

For starters if argc is equal to 2 then argv[2] is equal to NULL .对于初学者,如果argc等于2那么argv[2]等于NULL That is according to the C Standard argv[argc] is equal to NULL .也就是说,根据 C 标准argv[argc]等于NULL

So you need to change this statement所以你需要改变这个语句

h = crypt(h, atoi(argv[2]));
                  ^^^^^^^

to

h = crypt(h, atoi(argv[1]));
                  ^^^^^^^

This is the reason of the segmentation fault.这就是分段错误的原因。

From the C Standard (5.1.2.2.1 Program startup)从 C 标准(5.1.2.2.1 程序启动)

2 If they are declared, the parameters to the main function shall obey the following constraints: 2 如果已声明,则主 function 的参数应遵守以下约束:

— The value of argc shall be nonnegative. — argc 的值应为非负数。

— argv[argc] shall be a null pointer. — argv[argc] 应为 null 指针。

Also it seems you mean the following condition in the for loop此外,您的意思似乎是 for 循环中的以下条件

for(int i = 0; i < len; i++)
               ^^^^^^^

instead of代替

for(int i = 0; i > len; i++)
               ^^^^^^^

Also it is better to use the type size_t instead of the type int .此外,最好使用size_t类型而不是int类型。 It is the type of the value returned by the function strlen .它是 function strlen返回值的类型。

size_t len = strlen(msg);
for( size_t i = 0; i < len; i++)
//...

Pay attention to that within the function you are not changing the original string.请注意,在 function 中,您没有更改原始字符串。 You are just outputting its converted characters.您只是在输出其转换后的字符。

In this case the function does not make a great sense because it seems it should return the converted string to the caller.在这种情况下,function 没有多大意义,因为它似乎应该将转换后的字符串返回给调用者。

And the returned by the function a pointer to an empty string function 返回一个指向空字符串的指针

string c = "";
//...
return c;

does not make a sense.没有意义。

The function should be defined at least the following way. function 至少应按以下方式定义。

string crypt( string msg, int key )
{
    printf("%s\n", msg);
    
    size_t len = strlen( msg );
    for ( size_t i = 0; i < len; i++ )
    {
        msg[i] = (msg[i] + key) % 26;
        printf("%c", msg[i] );
    }

    return msg;
}

for(int i = 0; i > len; i++)

This says that the loop starts with i at 0 , and will continue only while i is bigger than len .这表示循环从i0开始,并且仅在i大于len才会继续。

Think about that for a minute.想一想。
If i starts at 0 , it is never bigger than len .如果i0开始,它永远不会大于len

The loop will not run.循环不会运行。

You intend for the loop to run while i is less than len .您打算i小于len时运行循环。

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

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