Question

In: Computer Science

In C programming using C11 library, how can we validate command line arguments? meaning for example...

In C programming using C11 library, how can we validate command line arguments?

meaning for example if we input "./math 65 45 2.3 1 0 68" into the terminal, how can we validate that the numbers "65 45 2.3 1 0 68" are floats and not chars? In other terms the program should display and error message if the command line arguments are not floats and if they are floats, the program carries on as usual.

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
int allOk = 1; // flag to check whether argument is float
int index = 0; // index of the not-a-float input
  
// display all the command line arguments
// loop index starts from 1 as, 0th element is the program name
printf("Arguments: ");
for(int i = 1; i < argc; i++)
{
printf("%s ", argv[i]);
}
printf("\n");
  
// check each argument
for(int i = 1; i < argc; i++)
{
// this loop iterates over the length of the ith argument
for(int j = 0; j < strlen(argv[i]); j++)
{
// get the ascii value of each character of the ith argument
int ascii = (int)argv[i][j];
  
// if ascii is within the range: 48 to 57 (digits: 0 to 9)
if(ascii >= 48 && ascii <= 57)
allOk = 1; // set flag
else
{
// if single argument is not float, set flag to 0 and break the inner loop
allOk = 0;
break;
}
}
// if non-float input is encountered, get the index and breeak the outer loop
if(allOk == 0)
{
index = i;
break;
}
}
  
// finally, check the flag for intended input
if(allOk == 1)
printf("All are floats!\n");
else
printf("Invalid input: %s\n", argv[index]);
  
return 0;
}'

****************************************************************** SCREENSHOT *********************************************************


Related Solutions

How do I add additional command line arguments in C++? I am working on a programming...
How do I add additional command line arguments in C++? I am working on a programming assignment that has the user input a file into the command line and then they have the option to also add a series of other arguments to the command line. I know how to accept the text file from the command line by using: int main(int argc, char *argv[]) { /.../ } Then filename(argv[1]) would be the text file that they put into the...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
C programming What is an array? Explain by taking an example that how can we take...
C programming What is an array? Explain by taking an example that how can we take input and output in 1D array?
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line...
C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line arguments to be passed to the programs. You only need to support one pipe command at a time. For example, when you type ls | wc the shell should write the output of ls to a temporary file by redirecting standard output when running ls, and run wc, redirecting standard input so it reads from the temporary file written to by ls.
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
Systems Programming in Linux using C Remake the version of the ls -ialR command in C.....
Systems Programming in Linux using C Remake the version of the ls -ialR command in C.. In this version of ls, you must also consider symbolic file types. Make sure you have file header comments and function header comments. Also, write inline comments whenever necessary.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT