Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a command-line argument to int?

I need to get an argument and convert it to an int. Here is my code so far:

#include <iostream>   using namespace std; int main(int argc,int argvx[]) {     int i=1;     int answer = 23;     int temp;      // decode arguments     if(argc < 2) {         printf("You must provide at least one argument\n");         exit(0);     }      // Convert it to an int here  } 
like image 742
nosedive25 Avatar asked May 09 '10 13:05

nosedive25


People also ask

How do you change a command line argument to an integer?

For example, to convert args[0] to its integer value, you can write this: public static void main(String[] args) { int num = 0; ... try { // Parse the string argument into an integer value.

Can command line arguments be converted into int?

Can command line arguments be converted into int automatically if required? D. Only ASCII characters can be converted.

Can command line arguments be converted?

Can command line arguments be converted into int automatically if required? Explanation: All command Line arguments are passed as a string. We must convert numerical value to their internal forms manually.

How do you convert command line arguments to integers in Java?

Example: Numeric Command-Line Argumentsint argument = Intege. parseInt(str); Here, the parseInt() method of the Integer class converts the string argument into an integer. Similarly, we can use the parseDouble() and parseFloat() method to convert the string into double and float respectively.


2 Answers

Since this answer was somehow accepted and thus will appear at the top, although it's not the best, I've improved it based on the other answers and the comments.

The C way; simplest, but will treat any invalid number as 0:

#include <cstdlib>  int x = atoi(argv[1]); 

The C way with input checking:

#include <cstdlib>  errno = 0; char *endptr; long int x = strtol(argv[1], &endptr, 10); if (endptr == argv[1]) {   std::cerr << "Invalid number: " << argv[1] << '\n'; } else if (*endptr) {   std::cerr << "Trailing characters after number: " << argv[1] << '\n'; } else if (errno == ERANGE) {   std::cerr << "Number out of range: " << argv[1] << '\n'; } 

The C++ iostreams way with input checking:

#include <sstream>  std::istringstream ss(argv[1]); int x; if (!(ss >> x)) {   std::cerr << "Invalid number: " << argv[1] << '\n'; } else if (!ss.eof()) {   std::cerr << "Trailing characters after number: " << argv[1] << '\n'; } 

Alternative C++ way since C++11:

#include <stdexcept> #include <string>  std::string arg = argv[1]; try {   std::size_t pos;   int x = std::stoi(arg, &pos);   if (pos < arg.size()) {     std::cerr << "Trailing characters after number: " << arg << '\n';   } } catch (std::invalid_argument const &ex) {   std::cerr << "Invalid number: " << arg << '\n'; } catch (std::out_of_range const &ex) {   std::cerr << "Number out of range: " << arg << '\n'; } 

All four variants assume that argc >= 2. All accept leading whitespace; check isspace(argv[1][0]) if you don't want that. All except atoi reject trailing whitespace.

like image 97
Thomas Avatar answered Sep 17 '22 11:09

Thomas


Note that your main arguments are not correct. The standard form should be:

int main(int argc, char *argv[]) 

or equivalently:

int main(int argc, char **argv) 

There are many ways to achieve the conversion. This is one approach:

#include <sstream>  int main(int argc, char *argv[]) {     if (argc >= 2)     {         std::istringstream iss( argv[1] );         int val;          if (iss >> val)         {             // Conversion successful         }     }      return 0; } 
like image 30
CB Bailey Avatar answered Sep 18 '22 11:09

CB Bailey