Question

Write a C++ program that takes two numbers from the command line and perform and arithmetic...

Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces (i.e. use the argv and argc input parameters).

Example1: Exercise1_1234567.exe    10   2    a

which should return 10 + 2 = 12, i.e. the last (and only) line on the console will be:

12

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ code for command line arguments computation

#include<string.h>
#include<iostream>
using namespace std;
int main(int argc, char **argv)
{
int n1,n2,sum,difference;
//convert command line input strings to integers
n1=atoi(argv[1]);
n2=atoi(argv[2]);
if(argc==3)
{
sum=n1+n2;
cout<<"sum="<<sum;
}
else if(argc==4)
{
  
if(strcmp(argv[3],"a")==0)
{
sum=n1+n2;
cout<<"sum="<<sum;
}
else if(strcmp(argv[3],"s")==0)
{
difference=n1-n2;
cout<<"Difference="<<difference;
}
else
cout<<"invalid operation, enter a or s:";
return 0;
}
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that takes two numbers from the command line and perform and arithmetic...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • In C please Create a program that takes two integers as command line arguments (num, length)...

    In C please Create a program that takes two integers as command line arguments (num, length) and outputs a list of the first length multiples of num. num should be included in the returned array. For example: ./a.out 7 5 -> [7, 14, 21, 28, 35] ./a.out 12, 10 -> [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] ./a.out 17, 6 -> [17, 34, 51, 68, 85, 102] Ma Word starts with a vowel add "yay" to the...

  • In C, Write a program that can accept an arbitrary number of command line arguments, e.g....

    In C, Write a program that can accept an arbitrary number of command line arguments, e.g. program must do the following: program needs to split each of the arguments into two halves based on the length of a string. Specifically, the first half will be [0, n/2) and the second half will be [n/2] where n is the length of the string and n > 1. For instances, hello is split into two halves: he and llo. Similarly, world! is...

  • Background: For this assignment, you will write a small encryption utility that implements a simple encryption...

    Background: For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce. There is an important catch, however: your program is...

  • java) write a program that takes command line argument and compute the average of their maximum...

    java) write a program that takes command line argument and compute the average of their maximum and minimum. make sure there are command line arguments being passed before you attempt to compute anything

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT