In this problem set, we are supposed to obtain one argument ( which omprises of digits), failing which, the user are prompted with a message, "Usage: ./caesar key". I have attached the problem statement below.
Here is the problem statement from the problem set for context.
Modify caesar.c at right such that instead of printing out the command-line argument provided, your program instead checks to make sure that each character of that command line argument is a decimal digit (i.e., 0, 1, 2, etc.) and, if any of them are not, terminates (with a return code of 1) after printing the message Usage: ./caesar key.
But if the argument consists solely of digit characters, you should convert that string (recall that argv is an array of strings, even if those strings happen to look like numbers) to an actual integer. As luck would have it, a function, atoi, exists for exactly that purpose. Here’s how you might use it:
int k = atoi(argv[1]); Once saved, print out the integer, as via %i with printf. So, for example, the behavior might look like this:
$ ./caesar 20
Success
20
or
$ ./caesar 20x
Usage: ./caesar key
Three issues:
Problem 1
1) When I input an integer(say 20, for convenience), I am prompted with the following outputs in the command line twice- not once as is desired:
./caesar
Success
20
Success
20
instead of
./caesar
Success
20
Problem 2
2) When I input a single argument, 20x, the user does not receive an error message. Instead, the code runs as if everything is allright(i.e)
Success
20x
Success
20x
Usage: ./caesar key
Admittedly, the programme returns a Usage:./caesar key. But why does it return success? Havent I already accounted for the fact in my code that it should not?
Problem 3:
When I input 0 or more than two arguments ( instead of strictly one argument), instead of reprompting the code, I am greeted with a blinking $ symbol in the terminal, instead of receiving the error mesasge " Usage:./caesar key"
int main(int argc, string argv[])
{
string s = argv[1];
if (argc == 2)
{
for (int i=0, n = strlen(s); i < n ; i++)
{
if(!isdigit(s[i]))
{
printf("Usage: ./caesar key \n");
}
else if(isdigit(s[i]))
{
printf("Success\n%s\n", argv[1]);
}
}
if( argc==1 || argc > 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
}
}
c