Question

In: Computer Science

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 0 2.5 3" I want to add two flags. the first flag being "-f" which tells the code what kind of calculation you want it to do with those 6 numbers before it and a second flag which can either be "-r" for radians or "-d" for degrees. The question is how do I scan if those two flags using code? So while my code checks if the 6 numbers are floats, I do not know how to check for those two flags... an example of the command line argument is "./math 1 1 0 0 2.5 3 -f -d"

Solutions

Expert Solution

Hello, you need to take all arguments as string i.e char array, and first check for the flag which starts with '-' symbol.

Then for operands check if it not start with '-', then convert it according to math calculation type i.e float/ integer.

Here is the example how you do this:




Here is the text code:

#include <stdio.h>
#include <stdlib.h>

/* declared sum constant */
const int UNKNOWN_TYPE = -1;
const int DEGREE_TYPE = 0;
const int RADIAN_TYPE = 1;
const int CALC_FLOAT = 0;
const int CALC_INT = 1;

int main(int argc, char *argv[])
{
/* read and store from command line argument */
int degreeOrRadian = UNKNOWN_TYPE;
int calcType = UNKNOWN_TYPE;

/* Loop through all arguments */
for(int i=1; i<argc; i++)
{
/* Get the argument */
char *arg = argv[i];
/* Check if it is flag */
if(arg[0] == '-')
{
/* store the flag in variable */
switch(arg[1])
{
case 'f':
calcType = CALC_FLOAT;
break;

case 'i':
calcType = CALC_INT;
break;

case 'd':
degreeOrRadian = DEGREE_TYPE;
break;

case 'r':
degreeOrRadian = RADIAN_TYPE;
break;

default:
printf("Unknown flag: %s\n", arg);
}
}
}
/* If -d flag is passed, it is degree */
/* else if -r passed, it is radian */
if(degreeOrRadian == DEGREE_TYPE)
{
printf("Degree Calculation\n");
}
else if(degreeOrRadian == RADIAN_TYPE)
{
printf("Radian Calculation\n");
}

/* check for -f flag or any flag you need */
if(calcType == CALC_FLOAT)
{
printf("Calculation type: Float\n");
float sum = 0;
/*Loop through argument and check if it is not flag */
/* then it must be operands */
for(int i=0; i<argc; i++)
{
if(argv[i][0] != '-')
{
/* convert argument to float/double */
sum += atof(argv[i]);
}
}

printf("Calculate result: %.4f",sum);

}
else if(calcType == CALC_INT)
{
printf("Calculation type: Integer\n");

int sum = 0;
for(int i=0; i<argc; i++)
{
if(argv[i][0] != '-')
{
/* Convert argument to integer */
sum += atoi(argv[i]);
}
}

printf("Calculate result: %d",sum);

}
else
{
printf("Unknown calculation type\n");
}


return 0;
}

Output


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...
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.
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. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
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...
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...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
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).
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal...
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal integer literals. Each of these decimal integer literals will be expressed using decimal digits, only, and none of the decimal integer literals will be prefixed by a plus or minus sign. None of the decimal integer literals will be greater than ULONG_MAX. The program prints the integers in order, least-to-greates and also prints the sum of the integers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT