Simple calculator using C ++ command line argument

I am new to C ++. I am writing a simple calculator using the command line. The command line should be in the following format: program name firstNumber operator secondNumber This is what I got so far:

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        cerr << "Usage: " << argv[0] << endl;
        exit(0);
    }
    else
    {
        int firstNumber = atoi(argv[1]);
        char theOperator = atoi(argv[2]);
        int secondNumber = atoi(argv[3]);
        switch (theOperator)
        {
        case'+':
            {
                cout << "The answer is " << firstNumber + secondNumber << endl;
                break;
            }
        case '-':
            {
                cout << "The answer is " << firstNumber - secondNumber << endl;
                break;
            }
        case '*':
            {
                cout << "The answer is " << firstNumber * secondNumber << endl;
                break;
            }
        case '/':
            {
                if (secondNumber == 0)
                {
                    cout << "Can not devide by a ZERO" << endl;
                    break;
                }
                else
                {
                    cout << "The answer is " << firstNumber / secondNumber << endl;
                    break;
                }
            }
        }
    }
}

      

The program does not start. When I run it, it displays an appropriate usage message and exits the program. Can anyone help me?

+3


source to share


3 answers


Others have already given you the answer, but you could very easily figure it out yourself. Just print what argc

's at the point where you know the code goes:

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        cout << "argc is: " << argc << endl; // Debug output that you delete later
        cerr << "Usage: " << argv[0] << endl;
        exit(0);
    }
    else

      

And then let's go back with that argc

. When you find that argc

there is actually 4 and you want to know what is inside argc

, you have to write some code to print it so you can figure it out. For example:

int main(int argc, char* argv[])
{
    cout << "argc is: " << argc << endl; // Debug output that you delete later
    for (int i = 0; i < argc; ++i)
    {
        // Print out all of the arguments since it not working as you expect...
        cout << "argv[" << i << "] = " << argv[i] << endl;
    }

    if (argc != 3)
    {
        cerr << "Usage: " << argv[0] << endl;
        exit(0);
    }
    else

      

and you would have figured out very quickly what is wrong ...

Please find out how to do this, because it will save you, but in the future and you won't have to wait for an answer here.

Also, there is another error in your code.



Why are you converting character +

from string to int?

else
{
    int firstNumber = atoi(argv[1]);
    char theOperator = atoi(argv[2]); // <<< WTF? Why?
    int secondNumber = atoi(argv[3]);
    switch (theOperator)

      

You probably want to get rid of atoi

and just:

char theOperator = argv[2][0]; // First character of the string

      

Assuming there will always be only one letter in the second argument ... Which may need to be forced / checked. See strlen () and std :: string and note that the type argv[2]

is char*

(pointer to char

).

I also recommend that you read How to Debug Small Programs which are linked from the SO Howto-Ask Help Page . It might help a little. And no, I don't think your question is bad. Debugging small programs is a skill that you will need in the future if you intend to program, so it will be useful for you to learn it now.

Welcome to programming and C ++ :)

+4


source


If you enter

programname firstNumber operator secondNumber

      

You have 4 arguments, not 3.



argv[0] = programname 
argv[1] = firstNumber
argv[2] = operator
argv[3] = secondNumber

      

It looks like your program is working correctly ... at least as far as the usage message prints.

See also other comments regarding the use of the operator argument.

+1


source


The parameter argc

also takes into account the name of the program.

Try the following:

if (argc != 4) // We expect 4 arguments: programname number operator number
{
    cerr << "Usage: " << argv[0] << " <number> <operator> <number>" << endl;
    exit(0);
}

      

In your code, running the program correctly (with all 3 parameters) displays an error message as argc

equals 4

.

+1


source







All Articles