I am currently taking cs50 introduction into computer science.
I'm trying to create a type bool function with argument string named only_digits that will check to see if the command line argument that the user inputs is in fact a digit. I don't think I'm using the isdigit
correctly. What I am trying to accomplish is not only checking to see if it is a decimal digit but also returning it back to main to continue with the program and furthermore convert the key from a string to an int inside main.
I have tried so many different ways. I refuse to go look at other solutions to the whole program because I really want to understand this and be able to learn and progress. It is the "Caesar" cipher "Problem Set 2" in "Week 2 Arrays" of the "Introduction to computer science course."
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
bool only_digits(string s);
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("USAGE: ./caesar KEY\n");
return 1;
}
return 0;
}
bool only_digits(string s)
if (! isdigit, (argv[1]))
{
printf("INVAILD: USAGE: ./caesar KEY\n");
return 1;
}
return 0;
Now, I have tried implementing that function multiple different ways. I have tried...
if ((isdigit, s) == 0)...if (!isdigit, argv[1])
but I know that I haven't declared s
yet for argv[1] which I am having problems with that also. Every time I try to declare it I get an error. I would list more examples of what I have tried and the errors but to be honest I have tried so many different ways that I can't even remember all of them now.
Also, please tell me if I'm wrong on this assumption. The command line argument is stored as a string so it's for example stored in this fashion: if the user typed "./caesar 3" argv["3"] and not argv[3]. So meaning it is stored as a string and not the decimal number 3. So I'm actually trying to check if it is a string and if it is to be able to change it into an int. I don't know if I'm fully understanding that or not and that could be a huge reason to why I'm not able to implement this function due to not grasping the concept.
I checked all the suggested questions and answers that they wanted me to review before submitting this and to be honest I just skimmed through them because there was solutions to other sections of the program and I didn't want to accidentally come across that and lose the ability to try on my own at first. This specific part of implementing the function is the part I'm stuck on for now.
isdigit() only checks for one 'character' (of the string).
//do not have this
//#include <cs50.h>
//
#include <stdbool.h> //included for bool
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h> // for exit()
#define string char* // emulate string as keyword
bool only_digits(string s);
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("USAGE: ./caesar KEY\n");
return 1;
}
only_digits(argv[1]);
return 0;
}
bool only_digits(string s) {
int len = strlen(s);
for (int x=0;x<len; x++)
{
if (!isdigit( (unsigned char) s[x] ))
{
printf("INVAILD: USAGE: ./caesar KEY\n");
exit(1); // hard exit, or change it to return(1) to 'fail'
}
}
return 0;
}
Good luck