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.