Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integer arguments for c++

Tags:

c++

arguments

I have a sort of calculator in C++ that should accept arguments when executed. However, when I enter 7 as an argument, it might come out to be 10354 when put into a variable. Here is my code:

#include "stdafx.h"
#include <iostream>

int main(int argc, int argv[])
{
    using namespace std;
    int a;
    int b;
    if(argc==3){
        a=argv[1];
        b=argv[2];
    }
    else{
        cout << "Please enter a number:";
        cin >> a;
        cout << "Please enter another number:";
        cin >> b;
    }
    cout << "Addition:" << a+b << endl;
    cout << "Subtaction:" << a-b << endl;
    cout << "Multiplycation:" << a*b << endl;
    cout << "Division:" << static_cast<long double>(a)/b << endl;
    system("pause");
    return 0;
}
like image 436
Rob Avatar asked Feb 09 '11 23:02

Rob


People also ask

What is integer argument in C?

int main(int argc, char *argv[]); argc is the number of arguments passed to your program, including the program name its self. argv is an array containing each argument as a string of characters.

What are the arguments in C?

The values that are declared within a function when the function is called are known as an argument. These values are considered as the root of the function that needs the arguments while execution, and it is also known as Actual arguments or Actual Parameters.

What is an int argument?

The INT function returns the integer portion of the argument (truncates the decimal portion). If the argument's value is within 1E-12 of an integer, the function results in that integer. If the value of argument is positive, the INT function has the same result as the FLOOR function.


2 Answers

Wherever did you get int argv[]? The second argument to main is char* argv[].

You can convert these command line arguments from string to integer using strtol or to floating-point using strtod.

For example:

    a=strtol(argv[1], nullptr, 0);
    b=strtol(argv[2], nullptr, 0);

But you can't just change the parameter type, because the operating system is going to give you your command-line arguments in string form whether you like it or not.

NOTE: You must #include <stdlib.h> (or #include <cstdlib> and using std::strtol;) to use the strtol function.


If you want error-checking, use strtol instead of atoi. Using it is almost as easy, and it also gives you a pointer to the location in the string where parsing terminated. If that points to the terminating NUL, parsing was successful. And of course it is good that you verify argc to make sure the user provided enough parameters, and avoid trying to read missing parameters from argv.

Example of error checking:

char* endp;
a = strtol(argv[1], &endp, 0);
if (endp == argv[1] || *endp) { /* failed, handle error */ }
like image 62
Ben Voigt Avatar answered Oct 02 '22 23:10

Ben Voigt


The function signature is int main(int argc, char *argv[]). argv is an array of string pointers.

If the argument is 7, it will be in the form of a string ("7"). Use atoi() to convert it to the number 7.

like image 32
Jonathan Wood Avatar answered Oct 03 '22 00:10

Jonathan Wood