Search code examples
cfor-loopcommand-line-argumentscomputer-sciencecs50

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:-

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”

I'm Using the cs50 ide on MS Edge.

This code is supposed To Output the Coded version of the input: Text.

Plz help I Don't Understand The problem.

The Error Appears After The text Is entered.


Solution

  • For starters if argc is equal to 2 then argv[2] is equal to NULL. That is according to the C Standard argv[argc] is equal to 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)

    2 If they are declared, the parameters to the main function shall obey the following constraints:

    — The value of argc shall be nonnegative.

    — argv[argc] shall be a null pointer.

    Also it seems you mean the following condition in the for loop

    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. It is the type of the value returned by the 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. 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.

    And the returned by the function a pointer to an empty string

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

    does not make a sense.

    The function should be defined at least the following way.

    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;
    }