I am on Problem Set 2: Caesar. Right now I'm trying to write code that first checks if the second command line argument is a int and if it does prints the integer. I got my program to compile after some issues but the bug im having is that when it prints regardless of if it is the error message or the int it prints too many times for example if start the program with "./caesar 235" it prints "235235235" and if i start with "./caesar jklm" it prints "Usage ./caesar key" 4 times when it should only be printing it once. My Code is down below.
One other small question if you have the time:
if (isdigit(argv[1][x]) == 0)
This is supposed to be saying if a character is NOT a digit. Did I write that part of the code right? I googled it alot but I dunno i feel like its wrong for some reason. Anyways Thanks for any and all help!
#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;
for (int x = 0; argv[1][x] != '\0'; x++)
{
if (isdigit(argv[1][x]) == 0)
{
y++;
}
if (y > 0)
{
printf("Usage ./caesar key\n");
}
else
{
int i = atoi(argv[1]);
printf("%i", i);
}
}
}