0

I am writing a code for a Caeser cipher program in C. the code is working as intended however I was trying to edit the code to make it accept multiple arguments instead of just one like it does currently. To incorporate this I added a new array in the main() named argvalue1 to hold the second argument. Furthermore I added a second interating variable and another loop to read the characters from the second array. I am still really new to using C, just not sure why the program will not read the second argument and how I can fix that.

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

// Compile this program with:
//    cc -std=c11 -Wall -Werror -o rotate rotate.c

#define ROT 13

//  The rotate function returns the character ROT positions further along the
//  alphabetic character sequence from c, or c if c is not lower-case

char rotate(char c)
{
    // Check if c is lower-case or not
    if(islower(c)) {
        // The ciphered character is ROT positions beyond c,
        // allowing for wrap-around
        return ('a' + (c - 'a' + ROT) % 26);
    }
    else {
        return c;
    }
}

//  Execution of the whole program begins at the main function

int main(int argcount, char *argvalue[], char *argvalue1[])
{
    // Exit with an error if the number of arguments (including
    // the name of the executable) is not precisely 2
    if(argcount != 3) {
        fprintf(stderr, "%s: program expected 1 argument, but instead received %d\n",
                    argvalue[0], argcount-1);
        exit(EXIT_FAILURE);
    }
    else {
        // Define a variable for a later loop
        int i;
        int j;
        // Calculate the length of the first argument
        int length = strlen(argvalue[1]);
        int length2 = strlen(argvalue1[1]);
        // Loop for every character in the text
        for(i = 0; i < length; i++) {
            // Determine and print the ciphered character
            printf("%c", rotate(argvalue[1][i]));
            printf("%c", rotate(argvalue1[1][j]));
        }

        // Print one final new-line character
        printf("\n");

        // Exit indicating success
        exit(EXIT_SUCCESS);
    }
    return 0;
}
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • 1
    All values you give are in this variable `char *argvalue[]`. [This](https://www.geeksforgeeks.org/command-line-arguments-in-c-cpp/) might help. (char* argv[], consider it as a array of strings. So all strings that you gave as input, are stored in this array of string.) [This](https://pastebin.ubuntu.com/p/HhYzDqNtxn/) is a fix for your code. – Daoist Paul Aug 04 '21 at 08:32
  • What do you pass to your program? Please show the command line how you call it. What is the output? – Gerhardh Aug 04 '21 at 09:38

1 Answers1

0

You don't need to add char *argvalue1[] at all here. Notice that the the second argument char *argvalue[] is actually an array. The second argument that you run your code with will be part of this array and that is how you should access it within your code.

To reiterate :

argvalue[0] --> first argument

argvalue[1] -- second argument

njari
  • 198
  • 1
  • 12
  • i have tried this however it is still not recognizing that there is a second argument – ActuallyMosses Aug 04 '21 at 09:11
  • @ActuallyMosses did you remove the erronous third parameter from `main`? Depending on CPU architecture and calling conventions you will get wrong results if you have an extra parameter that is not provided by the caller of the function. – Gerhardh Aug 04 '21 at 09:40
  • can you share exactly what commands you are running it with? Are you aware that argcount should be 3 in your case? 2 arguments in the array + the argcount itself. – njari Aug 04 '21 at 11:17
  • @Gerhardh I have removed the third parameter from ```main``` – ActuallyMosses Aug 05 '21 at 02:58
  • @njari I am running the code like ```./rotate dog cat```, and yes I have changes the argcount to 3 – ActuallyMosses Aug 05 '21 at 03:00