this is just a preliminary part of code that i tried running for checking the condition for the command line argument having an input which was numeric with digits ranging from 0-9. if empty, it forces the user for an input. the program is successfully printing the error message for when there is non numeric input in command line, or if there is more than one input in command line. but whenever i execute with no input at command line, it shows “Segmentation fault (core dumped)” even after i have added my own error message in my code for the situation if there is no input at command line. my problem is the fact that it won’t show my error message instead
the code i tried
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
bool only_int(string inputkey);
int check(string inputkey);
int main(int argc , string argv[])
{
bool check = only_int(argv[1]);
if(argc == 2)
{
if(check == true)
{
string plaintext = get_string("plaintext : ");
return 0;
}
else
{
printf("Usage: ./caesar keyn");
return 1;
}
}
else if((argc != 2) || (argv[1] == NULL))
{
printf("Usage: ./caesar keyn");
return 1;
}
}
//functions to check for only integral input
bool only_int(string inputkey)
{
int y = check(inputkey);
if(y > 0)
{
return true;
}
else
{
return false;
}
}
int check(string inputkey)
{
int result = 0;
for(int i = 0 , l = strlen(inputkey) ; i < l ; i++)
{
int x = isdigit(inputkey[i]);
if (x > 0)
{
result++;
}
}
return result;
}
i expected it to be something like this
expected
but what i got
actual result
Manasvi Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.