4

I'm writing a very basic command line C++ application that takes arguments on execution.

I just started C++ today, and it seems apparent that you can only take char** as the data type for arguments. I want to take two floats as parameters (to add them together later), but I can't seem to cast the character arrays as floats.

I have tried static_cast<float>(argv[0]) and stof(argv[0]) to cast the values, and both give compiler errors (unable to cast and not defined in scope, respectively).

I'm using the Code::Blocks IDE if that answers any questions.

My code:

#include <iostream>
#include <string>

/**
 * author: 2mac
 *
 */

using namespace std;

void derp();
float getSum(float num1, float num2);

int main(int argc, char** argv)
{
    float num1 = static_cast<float>(argv[0]);
    float num2 = static_cast<float>(argv[1]);

    if (argc != 0 || argc != 2)
        cout << "Usage: hello-world [args]\nargs: none OR <num1> <num2> to be added together" << endl;
    else
    {
        switch (argc)
        {
        case 0:
            derp();
            break;
        case 2:
            derp();
            cout << num1 << " + " << num2 << " equals " << getSum(num1,num2) << endl;
            break;
        }
    }
    return 0;
}

void derp()
{
    cout << "Hello, world!\n";
    cout << "It's time to lern some C++!" << endl;
}

float getSum(float num1, float num2)
{
    return num1 + num2;
}
1
  • argv[0] will be the name of your program. Use argv[1] for the first argument, argv[2] for the second, etc. But, anytime you are using argv[1...n] you should make a check that argc is large enough. It always has a value of at least 1 (program name is always in argv[0]) and is incremented by 1 for each argument on the command line. So your check should be if with regard to argc == 1 or 3, not 0 and 2. Your check also appears to be incorrect. != with an or. Commented Dec 16, 2013 at 3:29

3 Answers 3

4

Using this to convert your input to float number,

double f1, f2;
if (argc == 2)
{
    f1 = atof (argv[0]);
    f2 = atof (argv[1]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

The compiler gives me grief for this. Similar to before, atof isn't defined in the scope.
You need to #include <stdlib.h>
If this is C++, don't use stdlib.h. Use cstdlib instead.
1

A char** in C++ is just that, an pointer to a pointer to a character, thus not convertable to a float (Or any numeric value for that matter) directly Thus your first example will fail. You can use the intermediary functions atof or strtof (note strtof, not stof, why your second example fails) to convert these values, assuming you are sure they are actually in float form.

1 Comment

Okay. I altered the code to use strtof instead, but I still get a error: 'strtof' was not declared in this scope message when compiling.
1

You are just converting a char * variable to float by typecasting. It will typecast value of the pointer to float, not the string to float.

You need to use functions such as atof, strtof to convert string to float. Or you can write your own function to to convert string to float.

Comments

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.