View Full Version : Is there a way I can have integer arguments in c++?
WarMonkey
March 8th, 2008, 06:35 PM
I know that you can do:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
cout << argv[1] << "\n";
}
But how would I make it so the argument is an integer? I want to be able to add two arguments together. I tried changing char *argv[] to int *argv[], but contrary to my expectations, it did not work.
mike_g
March 8th, 2008, 06:46 PM
No, but the arguments can be converted by using strtol, or atoi.
Edit: heres an example:
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html
[h2o]
March 8th, 2008, 06:46 PM
You need to convert the textual representation of a number to an integer.
If my memory serves me right you should either be looking at "atoi()" or maybe some method of the stringstream classes.
WarMonkey
March 8th, 2008, 06:55 PM
Thanks! It works perfectly.
Jessehk
March 8th, 2008, 07:02 PM
There are several ways of doing this. If you want to do this in the bare-bones way, you can use a C function such as atoi() as suggested before. However, if you want to take advantage of C++ features and use an (arguably overkill) solution, you can do something like this:
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <string>
class ConvertError : public std::invalid_argument {
public:
ConvertError( const std::string &msg ) :
std::invalid_argument( msg ) {}
};
template <typename TO>
TO fromString( const std::string &src ) {
std::stringstream ss( src );
TO result;
if ( ! (ss >> result) )
throw ConvertError( "Can't convert" );
return result;
}
int main() {
try {
int x = fromString<int>( "32" );
float f = fromString<float>( "3.14159" );
int y = fromString<int>( "abc" );
std::cout << x << std::endl;
std::cout << f << std::endl;
std::cout << y << std::endl;
} catch ( ConvertError &e ) {
std::cout << e.what() << std::endl;
}
}
Or, if you don't mind using third party libraries, Boost.LexicalCast (http://boost.org/libs/conversion/lexical_cast.htm) makes this kind of thing very easy.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.