First of all, I am talking about old-fashioned ANSI-C (I mean the ANSI standard and no C99 or newer) compiled with gcc. I am only allowed to use the libraries that can be seen below in the code.
My problem is I have a program that is called in the following way on the Terminal:
program < integer_1 integer_2
While I have been able to figure out how to check for the number of arguments, I'm stuck on checking if those are integers.
If the program is called like this:
program < 1 -13
it should run without complaining but if it is run like this:
program < s 7
it should throw out an error.
Whatever I have tried so far has been utter rubbish. The best thing I have managed so far has been an error message if the second number has been a character. None of my tries has been able to deal with more than one digit but I have figured out why that is.
The problem is that I haven't used command line / terminal arguments with any programming language i now (C++, Java). I would really appreciate it if someone could show me how check for correct input as frankly I am out of ideas.
Am I correct that if I want to deal with numbers bigger than 9, I have to iterate through argv starting from index 2 until I find a space?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int arc, char *argv[])
{
if(arc != 3)
{
printf("Error: You have entered %d arguments, but two were expected!", arc - 1);
return -1;
}
return 0;
}
The easiest way out is to iterate over the argv[n]s and pass one by one to them to strtol() or similar. Then, check for the error and make the decision. To quote the man page, (emphasis mine)
long int strtol(const char *nptr, char **endptr, int base);
[...]
If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.
That said, program < integer_1 integer_2 is not exactly the way to pass the command-line arguments. If you want to pass the values arguments as command-line arguments, you shall lose the redirection operator and work with argc and argv[n]s directly..
Best way is to create a function for checking whether it is number or not.if the below function returns true then use atoi(argv[]) to convert them to integers to use it further.
bool isNumber(char number[])
{
int i = 0;
//checking for negative numbers
if (number[0] == '-')
i = 1;
for (; number[i] != 0; i++)
{
//if (number[i] > '9' || number[i] < '0')
if (!isdigit(number[i]))
return false;
}
return true;
}
Just a comment: not an answer
If you are going to use
program < arg1 arg2
You will not see arg1 or arg2 in the main parameters. arg1 is typically a filename or device which contain data which will be read by the program. I don't know if the program will even be able to access arg2. If you wish to pick up arg1 arg2 etc, lose the <
program arg1 arg2
You can try something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int check_cmd_args(char const *str[], int numargs);
int
main(int argc, char const *argv[]) {
if (argc < 2) {
printf("Not enough command line arguements entered\n");
exit(EXIT_FAILURE);
}
if (check_cmd_args(argv, argc)) {
printf("All Command line arguements are integers\n");
} else {
printf("Error, non-integer command line arguement entered\n");
}
return 0;
}
int
check_cmd_args(char const *str[], int numargs) {
int n, i = 0;
for (n = 1; n < numargs; n++) {
if (str[n][0] == '-') {
i = 1;
}
for (; str[n][i]; i++) {
if (!isdigit(str[n][i])) {
return 0;
}
}
}
return 1;
}
Related
Hi folks thanks in advance for any help, I'm doing the CS50 course i'm at the very beginning of programming.
I'm trying to check if the string from the main function parameter string argv[] is indeed a number, I searched multiple ways.
I found in another topic How can I check if a string has special characters in C++ effectively?, on the solution posted by the user Jerry Coffin:
char junk;
if (sscanf(str, "%*[A-Za-z0-9_]%c", &junk))
/* it has at least one "special" character
else
/* no special characters */
if seems to me it may work for what I'm trying to do, I'm not familiar with the sscanf function, I'm having a hard time, to integrate and adapt to my code, I came this far I can't understand the logic of my mistake:
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int numCheck(string[]);
int main(int argc, string argv[]) {
//Function to check for user "cooperation"
int key = numCheck(argv);
}
int numCheck(string input[]) {
int i = 0;
char junk;
bool usrCooperation = true;
//check for user "cooperation" check that key isn't a letter or special sign
while (input[i] != NULL) {
if (sscanf(*input, "%*[A-Za-z_]%c", &junk)) {
printf("test fail");
usrCooperation = false;
} else {
printf("test pass");
}
i++;
}
return 0;
}
check if the string from the main function parameter string argv[] is indeed a number
A direct way to test if the string converts to an int is to use strtol(). This nicely handles "123", "-123", "+123", "1234567890123", "x", "123x", "".
int numCheck(const char *s) {
char *endptr;
errno = 0; // Clear error indicator
long num = strtol(s, &endptr, 0);
if (s == endptr) return 0; // no conversion
if (*endptr) return 0; // Junk after the number
if (errno) return 0; // Overflow
if (num > INT_MAX || num < INT_MIN) return 0; // int Overflow
return 1; // Success
}
int main(int argc, string argv[]) {
// Call each arg[] starting with `argv[1]`
for (int a = 1; a < argc; a++) {
int success = numCheck(argv[a]);
printf("test %s\n", success ? "pass" : "fail");
}
}
sscanf(*input, "%*[A-Za-z_]%c", &junk) is the wrong approach for testing numerical conversion.
You pass argv to numcheck and test all strings in it: this is incorrect as argv[0] is the name of the running executable, so you should skip this argument. Note also that you should pass input[i] to sscanf(), not *input.
Furthermore, lets analyze the return value of sscanf(input[i], "%*[A-Za-z_]%c", &junk):
it returns EOF if the input string is empty,
it returns 0 if %*[A-Za-z_] fails,
it also returns 0 if the conversion %c fails after the %*[A-Za-z_] succeeds,
it returns 1 is both conversions succeed.
This test is insufficient to check for non digits in the string, it does not actually give useful information: the return value will be 0 for the string "1" and also for the string "a"...
sscanf() is very tricky, full of quirks and traps. Definitely not the right tool for pattern matching.
If the goal is to check that the strings contain only digits (at least one), use this instead, using the often overlooked standard function strspn():
#include <stdio.h>
#include <string.h>
int numCheck(char *input[]) {
int i;
int usrCooperation = 1;
//check for user "cooperation" check that key isn't a letter or special sign
for (i = 1; input[i] != NULL; i++) {
// count the number of matching character at the beginning of the string
int ndigits = strspn(input[i], "0123456789");
// check for at least 1 digit and no characters after the digits
if (ndigits > 0 && input[i][ndigits] == '\0') {
printf("test passes: %d digits\n", ndigits);
} else {
printf("test fails\n");
usrCooperation = 0;
}
}
return usrCooperation;
}
Let's try this again:
This is still your problem:
if (sscanf(*input, "%*[A-Za-z_]%c", &junk))
but not for the reason I originally said - *input is equal to input[0]. What you want to have there is
if ( sscanf( input[i], "%*[A-Za-z_]%c", &junk ) )
what you're doing is cycling through all your command line arguments in the while loop:
while( input[i] != NULL )
but you're only actually testing input[0].
So, quick primer on sscanf:
The first argument (input) is the string you're scanning. The type of this argument needs to be char * (pointer to char). The string typedef name is an alias for char *. CS50 tries to paper over the grosser parts of C string handling and I/O and the string typedef is part of that, but it's unique to the CS50 course and not a part of the language. Beware.
The second argument is the format string. %[ and %c are format specifiers and tell sscanf what you're looking for in the string. %[ specifies a set of characters called a scanset - %[A-Za-z_] means "match any sequence of upper- and lowercase letters and underscores". The * in %*[A-Za-z_] means don't assign the result of the scan to an argument. %c matches any character.
Remaining arguments are the input items you want to store, and their type must match up with the format specifier. %[ expects its corresponding argument to have type char * and be the address of an array into which the input will be stored. %c expects its corresponding argument (in this case junk) to also have type char *, but it's expecting the address of a single char object.
sscanf returns the number of items successfully read and assigned - in this case, you're expecting the return value to be either 0 or 1 (because only junk gets assigned to).
Putting it all together,
sscanf( input, "%*[A-Za-z_]%c", &junk )
will read and discard characters from input up until it either sees the string terminator or a character that is not part of the scanset. If it sees a character that is not part of the scanset (such as a digit), that character gets written to junk and sscanf returns 1, which in this context is treated as "true". If it doesn't see any characters outside of the scanset, then nothing gets written to junk and sscanf returns 0, which is treated as "false".
EDIT
So, chqrlie pointed out a big error of mine - this test won't work as intended.
If there are no non-letter and non-underscore characters in input[i], then nothing gets assigned to junk and sscanf returns 0 (nothing assigned). If input[i] starts with a letter or underscore but contains a non-letter or non-underscore character later on, that bad character will be converted and assigned to junk and sscanf will return 1.
So far so good, that's what you want to happen. But...
If input[i] starts with a non-letter or non-underscore character, then you have a matching failure and sscanf bails out, returning 0. So it will erroneously match a bad input.
Frankly, this is not a very good way to test for the presence of "bad" characters.
A potentially better way would be to use something like this:
while ( input[i] )
{
bool good = true;
/**
* Cycle through each character in input[i] and
* check to see if it's a letter or an underscore;
* if it isn't, we set good to false and break out of
* the loop.
*/
for ( char *c = input[i]; *c; c++ )
{
if ( !isalpha( *c ) && *c != '_' )
{
good = false;
break;
}
}
if ( !good )
{
puts( "test fails" );
usrCooperation = 0;
}
else
{
puts( "test passes" );
}
}
I followed the solution by the user "chux - Reinstate Monica". thaks everybody for helping me solve this problem. Here is my final program, maybe it can help another learner in the future. I decided to avoid using the non standard library "cs50.h".
//#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
void keyCheck(int);
int numCheck(char*);
int main(int argc, char* argv[])
{
//Error code == 1;
int key = 0;
keyCheck(argc); //check that two parameters where sent to main.
key = numCheck(argv[1]); //Check for user "cooperation".
return 0;
}
//check for that main received two parameters.
void keyCheck(int key)
{
if (key != 2) //check that main argc only has two parameter. if not terminate program.
{
exit(1);
}
}
//check that the key (main parameter (argv [])) is a valid number.
int numCheck(char* input)
{
char* endptr;
errno = 0;
long num = strtol(input, &endptr, 0);
if (input == endptr) //no conversion is possible.
{
printf("Error: No conversion possible");
return 1;
}
else if (errno == ERANGE) //Input out of range
{
printf("Error: Input out of range");
return 1;
}
else if (*endptr) //Junk after numeric text
{
printf("Error: data after main parameter");
return 1;
}
else //conversion succesfull
{
//verify that the long int is in the integer limits.
if (num >= INT_MIN && num <= INT_MAX)
{
return num;
}
//if the main parameter is bigger than an int, terminate program
else
{
printf("Error key out of integer limits");
exit(1);
}
}
/* else
{
printf("Success: %ld", num);
return num;
} */
}
I just started learning computer science.
I'm studying through CS50 taught at Harvard online.
Well, I'm working on this one problem where I need to get the key from the user in command line,
then a plaintext, and then shift that text for key amount of numbers in ASCII to make a ciphertext.
here is what I've got so far.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
}
{
string plaintext = get_string("plaintext: ");
int key = atoi(argv[1]);
int n = strlen(plaintext);
char chr[n];
printf("ciphertext: ");
for (int i = 0; i < n; i++)
{
chr[i] = plaintext[i];
if (isalpha(chr[i]))
{
if (isupper(chr[i]))
{
chr[i] = (chr[i] - 65 + key) % 26 + 65;
printf("%c",chr[i]);
}
else if (islower(chr[i]))
{
chr[i] = (chr[i] - 97 + key) % 26 + 97;
printf("%c",chr[i]);
}
}
else
{
printf("%c",chr[i]);
}
}
printf("\n");
}
}
well I know this seems very floppy but man it's my second week into programming while working full time.
anyways, I'm trying to have the user to run this program by using
./caesar "any number for the key".
if the user puts in any other things, then I'd like to print
"Usage: ./caesar key\n"
so far the only way i can think of is making the if statement with argc != 2
so that I can at least make sure the user puts in just one command on top of the program's name.
but the problem is that if the user puts in other things such as
./caesar HELLO
./caesar YolO
the program still runs.
I'm trying to figure out what I can do in order to prevent that from happening.
Really appreciate your time for reading this and help.
That is the reason way the old atoi function has been superseded by strtol. The former only try to convert an initial numeric part from the given string, while the latter also tells what remains after the converted part.
So to make sure that the user has given a number as sole argument, you could do:
int main(int argc, char *argv[]) {
long key;
int reject = 0;
if (argc != 2) { // ensure one argument
reject = 1;
}
else {
char *end;
key = strtol(argv[1], &end, 10); // ensure not empty and numeric
if ((*end != 0) || (end == argv[1])) reject = 1;
}
if (reject) {
printf("Usage: %s key\n", argv[0]);
return 1;
}
sscanf could be used too. It even allows blank characters after the number, which should not happen as the argv array is blank separated:
int main(int argc, char *argv[]) {
int key;
char empty[2];
if ((argc != 2) || (sscanf(argv[1], "%d%1s", &key, empty) != 1)) {
printf("Usage: %s key\n", argv[0]);
return 1;
}
...
To rephrase your question: You want to perform validation on user input, where the only legal input values are numeric strings: 123 and 4534 and 000 could all be considered valid, but hello world and f00 and one hundred thirty two are not valid.
In most programming languages, including C, strings are more or less just arrays of individual char elements (in other words, char is a primitive data type, but string is not.... C doesn't have a string data type at all. hint: take a look at the way you've declared main). So how can you use that to your advantage to validate the input to this program? Easy enough: every character in the input string must come from the domain [0123456789] (plus the null terminator \0 at the end...).
So to solve this problem, you just need to implement a check that verifies that the inputted value has that property. There are a number of ways to do that, maybe the easiest of which is:
int num;
int y = scanf("%d", &num);
then check the value of y. It will be set to the number of valid inputs that scanf read (so it will be 1 if a number was given).
Note: as mentioned in the comments, this will also accept things like 123abc (it will strip off the numbers and ignore the characters at the end). If you absolutely must have only numbers for input, scanf might not be the right function. In that case, using fgets to read the input as a string and then looping over each input to check that it is from the valid input domain might be a better approach.
I'm having problems with this simple loop exercise. I think the code is correct and I don't get any errors however when I run the program, I just get "^C" over and over again. Please help.
#import <readline/readline.h>
#import <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
printf("Where should I start counting? ");
const char *numInput = readline(NULL);
int countDownStart = atoi(numInput);
for (int i = countDownStart; i >= 0; i--) {
if (i % 3 == 0){
printf("%d\n", i);
if (i % 5 == 0) {
printf("Found one!\n");
}
}
}
return 0;
}
UPDATE 8/3/14
The code works when I enter the starting number using the numbers above my keyboard. However when I enter the starting number with my 10 key, I get "^C" every time I hit enter. Now I'm completely flummoxed but at least my code works.
Thank you for the help everyone. I understand atoi is not the best function to use but am trying to work through the Big Nerd Objective-C book.
Use strtol inplace of atoi as it returns error value in case of conversion failures.
You have to check the output received from function readline
const char *numInput = readline(NULL);
printf ("Input %s", numInput); //debugging
int countDownStart = atoi(numInput);
If the function is always returning the same string means, then the result also will remain the same.
I need to convert an array of number characters to integer values, in order to perform math operations in C. When I use atoi(argv[2][count]), it only converts the first digit to an integer. So if argv[2]=123, it only converts the '1' to an integer 1. How can I get '123' to be one integer value, 123? Thanks!
atoi is a good function to use, however you can write your own atoi function in C.
int xatoi(char *s)
{
int result=0;
while(*s)
{
result=result*10+(*s-48);
s++;
}
return result;
}
the logic behind this is every character '1','2',......'s ascii value is stored for example '1' has ASCII value '49'. please compile above program and check for errors i haven't tested it but i am sure it will work.
please see the link below for your reference
http://www.newebgroup.com/rod/newillusions/ascii.htm
argv is an array of string arrays and so argv[N] gives you an array. But argv[N][J] gives you element at position J of the array stored in argv[N]. However, it is strange that compiler did not complain about your code because atoi() function, in fact, expects a pointer (to the null-terminated string) and not a character value. The compiler should have said something like warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast. I recommend you not to ignore warnings. Personally I tend to compile the code with the highest warning level and not have a single warning, though our codebase at work is many millions of lines of code.
Also, prefer to use strtol over atoi. The problem with atoi is that it ignores invalid input (i.e. just returns you 0 if there is no problem, without affecting errno).
Anyway, here is an example of how to grab an array of base 10 integers from a command line:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
int n;
char *e;
int *v;
if (argc < 2) {
fprintf(stderr, "Please specify some numbers\n");
return EXIT_FAILURE;
}
n = argc - 1;
v = alloca(sizeof(int) * n);
for (i = 1; i < argc; ++i) {
v[i-1] = strtol(argv[i][i], &e, 10);
if (!v && errno) {
fprintf(stderr, "Cannot convert '%s' into number: %s\n",
argv[i], strerror(errno));
return EXIT_FAILURE;
} else if (*e != '\0') {
fprintf(stderr, "%s is not a number\n", argv[i]);
return EXIT_FAILURE;
}
}
printf("You have entered the following numbers: %d", v[0]);
for (i = 1; i < n; ++i)
printf(", %d", v[i]);
printf("\n");
return EXIT_SUCCESS;
}
Hope it is useful.
I would like a user to pass either two parameters or leave it blank. For instance:
./program 50 50
or
./program
When I tried to use int main(int argc, char *argv[]), first thing I have done was to change char *argv[] to int *argv[] but it did not work. What I want is from the user is just to enter two integers between 0 and 100. So if it is not two integers then it should give an error.
I was sort of thinking to give out an error with types (as I used to program on C#) but whatever I enter, argv[1] would be 'char' type all the time.
So what I have done is
for (int i = 0; i <= 100; i++) {
//printf("%d", i);
if (argv[1] == i) {
argcheck++;
printf("1st one %d\n", i);
}
else if (argv[2] == i) {
argcheck++;
printf("2nd one %d\n", i);
}
This does not work as well. Also it gives warning when compiling, but if I change argv with atoi(argv[1]) for instance, then it gives a Segmentation fault (core dumped) error.
I need a simple way to solve this problem.
EDIT:
So I fixed with atoi(), the reason why it was giving segmentation fault was because I was trying it with null value when I have no parameter. So I fixed it up by adding an extra cond. But now the problem is if the value is let's say
./program asd asd
Then the output of atoi(argv[1]) would be 0. Is there a way to change this value?
Don't use atoi() and don't use strtol(). atoi() has no error checking (as you found out!) and strtol() has to be error-checked using the global errno variable, which means you have to set errno to 0, then call strtol(), then check errno again for errors. A better way is to use sscanf(), which also lets you parse any primitive type from a string, not just an integer, and it lets you read fancy formats (like hex).
For example, to parse integer "1435" from a string:
if (sscanf (argv[1], "%i", &intvar) != 1) {
fprintf(stderr, "error - not an integer");
}
To parse a single character 'Z' from a string
if (sscanf (argv[1], "%c", &charvar)!=1) {
fprintf(stderr, "error - not a char");
}
To parse a float "3.1459" from a string
if (sscanf (argv[1], "%f", &floatvar)!=1) {
fprintf(stderr, "error - not a float");
}
To parse a large unsigned hexadecimal integer "0x332561" from a string
if (sscanf (argv[1], "%xu", &uintvar)!=1) {
fprintf(stderr, "error - not a hex integer");
}
If you need more error-handling than that, use a regex library.
This will do:
int main(int argc, char*argv[])
{
long a,b;
if (argc > 2)
{
a = strtol(argv[1], NULL, 0);
b = strtol(argv[2], NULL, 0);
printf("%ld %ld", a,b);
}
return 0;
}
The arguments are always passed as strings, you can't change the prototype of main(). The operating system and surrounding machinery always pass strings, and are not able to figure out that you've changed it.
You need to use e.g. strtol() to convert the strings to integers.
You want to check whether
0 or 2 argument is received
two values received are between 0 and 100
received argument is not a string. If string comes sscanf will return
0.
Below logic will helps you
int main(int argc, char *argv[])
{
int no1 = 0, no2 = 0, ret = 0;
if ((argc != 0) && (argc != 2))
{
return 0;
}
if (2 == argc)
{
ret = sscanf(argv[1], "%d", &no1);
if (ret != 1)return 0;
ret = sscanf(argv[2], "%d", &no2);
if (ret != 1)return 0;
if ((no1 < 0) || (no1 >100)) return 0;
if ((no2 < 0) || (no2 >100)) return 0;
}
//now do your stuff
}
The things you have done so innocently are blunder in C. No matter what, you have to take strings or chars as your arguments and then later you can do whatever you like with them. Either change them to integer using strtol or let them be the strings and check each char of the string in the range of 48 to 57 as these are the decimal values of char 0 to 9. Personally, this is not a good idea and not so good coding practice. Better convert them and check for the integer range you want.
You can still use atoi, just check whether the argument is a number first ;)
btw ppl will tell you goto is evil, but they're usually brainwashed by non-sensible "goto is bad", "goto is evil", "goto is the devil" statements that were just tossed out without elaboration on the internet.
Yes, spaggethi code is less managable. And goto in that context is from an age where it replaced functions...
int r, n, I;
for (I = 0; I < argc; ++I)
{
n = 0;
do
if ( !((argv + I) [n] >= '0' && (argv + I) [n] <= '9') )
{
err:
fprintf(stderr,"ONLY INTEGERS 0-100 ALLOWED AS ARGUMENTS!\n");
return 1;
}
while ((argv + I) [++n] != '\0')
r = atoi(argv[I]);
if (r > 100)
goto err;
}
You can not change the arguments of main() function. So you should just use atoi() function to convert arguments from string to integer.
atoi() has its drawbacks. Same is true for strtol() or strtoul() functions. These functions will return a 0 value if the input to the function is non-numeric i.e user enters asdf or anything other than a valid number. To overcome this you will need to parse the string before passing it to atoi() and call isdigit() on each character to make sure that the user has input a valid number. After that you can use atoi() to convert to integer.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main(int argc,char *args[])
{
int i,sum=0;
for(i=0;i<=argc;i++)
{
printf("\n The %d argument is: %s",i,args[i]);
}
printf("\nTHe sum of given argumnets are:");
for(i=1;i<argc;i++)
{
int n;
n=atoi(args[i]);
printf("\nN=%d",n);
sum += n;
}
printf("\nThe sum of given numbers are %d",sum);
return(0);
}
check this program i added another library stdlib.h and so i can convert the character array to string using function atoi.