-1

I'm taking the course Harvard CS50x and this is the caesar problem set.

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

 int main(int argc, string argv[])
 {
    if (argc != 2)
    {
        printf("Nope\n");
        return 1;
    }

    int k = atoi(argv[1]);

    if (k < 0)
    {
        printf("Nope\n");
        return 1;
    }
    else
    {
        string code = GetString();

        for (int i = 0, n = strlen(code); i < n; i++)
            {
                if islower(code[i])
                    printf("%c", (((code[i] + k) - 97) % 26) + 97);
                else if isupper(code[i])
                    printf("%c", (((code[i] + k) - 65) % 26) + 65);
                else
                    printf("%c", code[i]);
            }
            printf("\n");
            return 0;
    }
 }

I don't understand what the problem is with this code but when I try to compile I get this: The IDE is AWS Cloud9

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Vish G
  • 25
  • 1
  • 6

1 Answers1

0

To use the function atoi you need to include the header <stdlib.h>

#include <stdlib.h>

It seems the function GetString is not declared in the header <cs50.h>. Instead try to use the function get_string. For example

string code = get_string( "Enter a text: " );

These if statements

            if islower(code[i])
                printf("%c", (((code[i] + k) - 97) % 26) + 97);
            else if isupper(code[i])

are incorrect. Expressions used in if statements shall be enclosed in pafrentheses. For example

            if ( islower( ( unsigned char )code[i] ) )
                printf("%c", (((code[i] + k) - 97) % 26) + 97);
            else if ( isupper( ( unsigned char )code[i] ) )

Also it is a bad idea to use magic numbers like for example 97 or 65. Instead use 'a' and 'A'.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335