0

Hello I'm still fairly raw at this coding thing so i'm sure im making rookie mistakes. I'm doing Problem Set 2 caesar and I've hit a wall and haven't been able to move forward. What I have been trying to do is write code that figures out if the the 2nd Command Line Argument is a non decimal place integer and then I'm going to convert a copy of it from a string to an int to use to complete the program. First I tried just using the command line argument "argv[1]" straight up in my code but when I printed it out it was printing the completely wrong thing so I figured the problem was I had to make a copy of it first. So I guess my main question is what am I doing wrong to use the 2nd command line article with stringcpy so I can use it in my program. I understand that stringcpy takes in the data type "char" and I think it has something to do with me putting [][] somewhere but i've tried so many different combinations of that and changing the data types around but I just can't get it to compile. Please Help. I've changed my code around quite a few times and I know the "atoi" isn't going to work right now but I cant fix that on my own once I know how to make these copies properly.

Edit: So now I understand that I dont need to declare argv[1] and got it to compile but now It prints the wrong thing like if the CLA is 24 it prints 2424 so I know that isn't right. Help?

Here's my code:

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


int main(int argc, string argv[])


{
    


// If there are no CLA or if more than 1 CLA print an error message and return from main a value of 1
    if (argc == 2)
        { printf("Success\n"); }
    if (argc != 2)
        { 
            printf("Usage ./caesar key\n"); 
            return 1; 
            
        }
// If CLA is not a decimal digit print Usage: ./caesarkey and return from main a value of 1
    int y = 0;
    int u = strlen(argv[1]);
    char copy[u+1]; 
    strcpy(copy, argv[1]);
    for (int x = 0; copy[x] != '\0'; x++)
        {
            if (isdigit(copy[x]) == 0)
                {
                    y++;
                }
    if (y > 0)
        {
            printf("Usage ./caesar key\n"); 
        }
    else 
        {
            char c2[u+1];
            strcpy(c2, argv[1]);
            int i = atoi(c2);
            printf("%i", i);
        } 
        }
        

//Get string from user by asking for plaintext:

//print ciphertext

}


Like I said I'm pretty new to this so feel free to give me any other pointers as well.

1 Answer 1

0

There are several things going on in your code that need to be addressed. First, remember that arrays start at 0, not 1, so if an array has 3 elements, they are numbered 0, 1 and 2.

Second, argc and argv are special var names. They are already declared in int main(int argc, string argv[]), so they should not be redeclared inside of main.

Just in case you didn't remember, a string should always be declared with one extra space to hold the end of string marker, \0. So, a string that would hold the word "cat" would need to be declared with length 4, as in char myString[4];

The isdigit() function and all of it's cousins take a single char as input. If you attempt to use a string as input to isdigit, it will cause a seg fault. You need to select a single char from a string, such as argv[1][i] in a for loop. (That's a big hint.)

To convert a string to an int, you use the atoi() function, using the entire string as an input.

Most of the pieces are there, but there's a lot of extraneous code too. Try simplifying.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • hey sorry to bug you but i wanted to ask for more advice. I edited my some of my code trying to use some of what you said and I did get it to compile but It prints the wrong thing like when its a multiple digit number like 24 it prints 2424 you mind pointing me toward the problem? I know im supposed to use argv[1][i] somewhere but Im stumped. Jan 29, 2021 at 4:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .