I am returning a segmentation fault with this code. The argc portion of the code is function correctly if i enter too many arguments returning "1". If the correct number of arguments are entered I get a segmentation fault. I am pretty new to this and could use some help figuring out why.
Here is output from testing when i added a print f to the beginning.
./caesar your key is (null) segmentation fault
./caesar 2 your key is 2 segmentation fault
./caesar 2 3 your key is 2 segmentation fault
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main (int argc, string argv[])
{
int counter = 0;
printf("your key is: %s \n", argv[1]);
for (int i =0, n=strlen(argv[1]); i<n; i++)//tries to read argv[1] to determine if all of the elements are digits
{
if (argv[1][i]<=9)
{
counter ++;//adds to counter when decimal digits are found
}
else
{
counter--;//subtracts from counter if non decimals are found
}
}
if (argc==2 && counter == strlen(argv[1]))//if arguments are entered properly prompt user for message to encode.
{
string k = get_string("Plaintext: \n");
}
else
{
printf("1 \n");
}
}
I know argv is taking in the second argument stored in argv[1]. I am just not sure how to correct this outcome. Any help would be greatly appreciated.
Thanks