Exercise: Basic Math Program
In Fridayās lecture Fardad (OOP344 Professor) assigned a second exercise for us to work on related to one of the topicās of the lecture: command line arguments. The premise is simple: Write a program which receives two numbers and an operator from the command line in the formatĀ program_name number operator numberĀ and then output the result of the operation, also, error-checking must be included. An example run:
input ~> bm 2 x 2
output: 4
input ~> bm 4.6 + 5.7
output: 10.3
input ~> bm q w e
output: invalid input! use format: <bm num op num>
Ā Ā The first part of the problem: check the validity of the command line input. To do this I wrote a function calledĀ valid()Ā which takesĀ argcĀ andĀ argvĀ as parameters and returns true if the input is found to be valid and false otherwise. If the input is detected as invalid the program will display an error message with a description of the proper use of the program and then exit allowing the user to try again with new input.
Ā Ā The problem to solve is thatĀ the numbers provided in the command line are stored in the variableĀ char* argv[]Ā as strings. To perform any sort of mathematical operation on these numbers, they must first be converted to double values. To do this I used the functionĀ atof()Ā fromĀ cstdlibĀ which takes a null-terminated character array as an argument and returns a double value (if the string provided is convertible to a double, otherwise the function will return false).
Ā Ā Finally, once the validity of the input is ensured and the strings have been converted to numbers, weāre ready to perform the arithmetic. FourĀ ifĀ statements check for which operator was provided and the appropriate operation is performed and the answer displayed on screen.
//BasicMath.cpp
//Fadi Tawfig
//21/09/2013
#include <iostream>
#include <cstdlib>
bool valid(int, char*[]);
int main(int argc, char* argv[]){
//First ensure that the command line variables provided are valid usingĀ
//valid() function. If not, exit the program with a message instructing theĀ
//user on the proper use of the program.
if(!valid(argc, argv)){
std::cout « āInvalid Input!ā « std::endl « āPlease enter an equation in the following format:ā « std::endl;
std::cout « ābm num operator(+, -, x, /) Example: bm 4 x 4ā « std::endl;
return 0;
}
//The atof() function included in cstdlib is used to convert a char* (string)Ā
//to a double value
double num1 = atof(argv[1]);
double num2 = atof(argv[3]);
//Find out which operator was used and print the result of the operation
if(argv[2][0] == ā+ā)
std::cout « std::endl « num1 + num2 « std::endl « std::endl;
else if(argv[2][0] == ā-ā)
std::cout « std::endl « num1 - num2 « std::endl « std::endl;
else if(argv[2][0] == āxā)
std::cout « std::endl « num1 * num2 « std::endl « std::endl;
else if(argv[2][0] == ā/ā)
std::cout « std::endl « num1 / num2 « std::endl « std::endl;
return 0;
}
//Function valid() checks the validity of the input provided at the command line
bool valid(int argc, char* argv[]){
//If the amount of command line arguments provided is not exactly four,
//(program name, first number, operator and, second number) then the input
//is invalid.
if(argcĀ != 4)Ā
return false;
//If the operator provided isnāt one of the four valid operators, then input
//is invalid.
else if(argv[2][0]Ā != ā+ā && argv[2][0]Ā != ā-ā && argv[2][0]Ā != āxā && argv[2][0]Ā != ā/ā)
return false;
//If the 1st and 3rd arguments arenāt convertable to double values then the
//input is invalid.
else if(!atof(argv[1]) ||Ā !atof(argv[3]))
return false;
//If none of these conditions is true, the input valid() will return true.
return true;
}