0

I am trying to write an application that accepts command line argument like 1 + 2, calculates and prints the output. I am very new to c++, so i wrote some code - that's does not work. I think my main problem is lack of knowledge, i cannot figure out how to parse and convert this expression

1 has to go into int/double + has to remain a string to go through the switch statement 2 has to go into int/double

I already know that if there are spaces then each argument will contain the piece of string accordingly having argv[2] = "1" argv[3] = "+" argv = "2" so all i need now is to convert them to the right datatype so i can perform calculations, i have tried to convert using strtod also but every one of them throws exception, plus apparently i cannot write switch statement the way it it so its needs to be a string. I am very confused with all the conversions. Please let me know if i even think in the right way?

 // Calculate.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <tchar.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    double oper1 = atof(argv[2]);
    double oper2 = atof (argv[4]);

    switch(char[3]){
    case "+":
        cout<< oper1 + oper2;
        break;
    }


    return 0;
}
6
  • There are a number of things wrong with the code. For example, in C/C++, array indexes start from 0, not 1. Double quotes indicate a string, not a character and C++ switch statements do not work on strings. The syntax "char[3]" does not convert the fourth element of the argv array into a char type, either. Commented Oct 28, 2014 at 2:06
  • @akton At least something that i learned today is that, when you pass command line arguments the actual argument starts at location 2 of an array. It says there is always 1 reserved for the actual command, not sure what 0 is used for. I know the single and double quotes difference, i dont understand why char* acts like string, but if i switch char and change to single quotes it won't work anyway Commented Oct 28, 2014 at 2:09
  • Actually, argv is the parsed command line, so the first element in argv (argv[0]) is usually the executable name. See stackoverflow.com/questions/2050961/… for more info. Commented Oct 28, 2014 at 2:12
  • @Jenny The arguments DO start at location 2 in an array. In C++ however this is index 1 (location 1 is index 0 and typically the program name). Commented Oct 28, 2014 at 2:14
  • argv[0] = command, argv[1] = first argument Commented Oct 28, 2014 at 2:19

2 Answers 2

2

Try this

// Calculate.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <tchar.h>
#include <stdlib.h>
#include <iostream>
using namespace std;



 int main(int argc, char* argv[])
{
    double coeff1 = atof(argv[1]);
    double coeff2 = atof (argv[3]);
    char op = argv[2][0];

    switch(op){
    case '+':
        cout << coeff1 + coeff2 << endl;
        break;
    default:
        cout << "unhandled operation " << argv[2] << endl;
        break;
    }

    return 0;
}

Note in your code.

switch(char[3]){

Is incorrect, you have no array called char (and you couldn't its a reserved word).

You cannot switch on a string value, so you will have to make it a char.

Your command line arguments are strings. So you read the first character from your argument.

char op = argv[2][0];

and in your switch you can now use

switch(op){

in your case statement

 case "+":

Is wrong. Anything in "" is a string, you must use '' for a char

case '+':

Others have already explained this but your arguments start from argv[1], arg[0] is the name of your program (try printing it to cout). So if you invoke your executable like.

Calculate 1 + 2

The arguments look like

argv[0] = "Calculate"
argv[1] = "1"
argv[2] = "+"
argv[3] = "2"

Note: That this program has zero error checking it will crash if there are not at least 3 command line arguments. You can use argc to determine the number of command line arguments provided.

Sign up to request clarification or add additional context in comments.

2 Comments

yeah the error checking totally failed me, my native is java, so i am use to java exception handling...and had no idea its that important to do returns in c++
The return value is not significant here (notice my example always returns 0). You have to ensure you are interpreting the operator (+, - etc) correctly. This was where your initial code was falling down. You can compare chars (my example) or strings (other answer).
1

I suggest something like that: (all relevant includes)

using namespace std;

int main(int argc, char* argv[])
{
    if(argc != 4) {
        cout << "usage: calculate <op1> <operation> <op2>" << endl;
        return 1;
    }
    double oper1( atof(argv[1]) );
    double oper2( atof(argv[3]) );
    string operation( argv[2] );
    if(operation == "+") {
        cout << oper1 + oper2;
    } else if (operation == "-") {
        cout << oper1 - oper2;
    } else {
        cout << "illegal operation '" << operation <<"'" << endl;
        return 1;
    }
    return 0;
}

it does not check for a valid floating point number specified

2 Comments

now i see, my total misunderstanding was in exception handling. only because i didn't do returns my program would not compile. You are awesome!
returns have nothing to do with it. Your program always returned 0. Your program was not compilable, as @Paul Roony pointed out because array with the name of "char" does not exist. "char[3]" does not do what you wanted. You could have used argv[2][0] to extract first character of the 3-rd argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.