I'm testing code remotely on a Solaris machine through SSH Secure Shell using c++. Not sure of what version anything is; Solaris, the c++/compiler, etc. (and don't know how to find out through SSH Secure Shell)...
This code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <errno.h>
using std::cout;
using std::cin;
using std::string;
enum STR_TO_INT_STATUS { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };
STR_TO_INT_STATUS str_to_int( int&, char const*, int );
int main()
{
int num;
string str;
STR_TO_INT_STATUS code;
cout << "
Enter a string containing numbers: ";
cin >> str;
code = str_to_int( num, str.c_str(), 0 );
if( code == OVERFLOW || code == UNDERFLOW )
cout << "
The number was out of int's range
";
else if( code == INCONVERTIBLE )
cout << "
The string contained non-number characters
";
else if( code == SUCCESS )
cout << "
The int version of the string is: " << num << "
";
}
STR_TO_INT_STATUS str_to_int( int &i, char const* s, int base )
{
char *end;
long l;
errno = 0;
l = strtol( s, &end, base );
if( ( errno == ERANGE && l == LONG_MAX ) || l > INT_MAX )
return OVERFLOW;
if( ( errno == ERANGE && l == LONG_MIN ) || l < INT_MIN )
return UNDERFLOW;
if( *s == '' || *end != '' )
return INCONVERTIBLE;
i = l;
return SUCCESS;
}
compiles and works fine... as you can see, it converts a string entered by the user into an int...
But when modified like so:
#include <iostream>
#include <string>
#include <cstdlib>
#include <errno.h>
#include <limits>
#include <cmath>
using std::cout;
using std::cin;
using std::string;
using std::numeric_limits;
int size_of( int ); //------------------------------------------------- :62
enum STR_TO_INT_STATUS { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };
STR_TO_INT_STATUS str_to_int( int&, char const*, int ); //------------- :84
int main()
{
int num;
string str;
string dummy;
STR_TO_INT_STATUS code;
system( "clear" );
cout << "
int's max limit: "
<< numeric_limits<int>::max()
<< "
int's min limit: "
<< numeric_limits<int>::min()
<< "
number of digits in the largest int: "
<< size_of( numeric_limits<int>::max() )
<< "
number of digits in the smallest int: "
<< size_of( numeric_limits<int>::min() );
cout << "
Enter a string containing numbers: ";
cin >> str;
code = str_to_int( num, str.c_str(), 0 );
if( code == OVERFLOW || code == UNDERFLOW )
cout << "
The number was out of int's range
";
else if( code == INCONVERTIBLE )
cout << "
The string contained non-number characters
";
else if( code == SUCCESS )
cout << "
The int version of the string is: " << num << "
";
cout << "Press enter key to continue...";
getline( cin, dummy );
system( "clear" );
return( 0 );
}
int size_of( int num )
{
int length = 0;
num = ( int )fabs( num );
if( num == 0 )
length = 1;
else
while( num > 0 )
{
length++;
num /= 10;
}
return( length );
}
STR_TO_INT_STATUS str_to_int( int &i, char const* s, int base )
{
char *end;
long l;
errno = 0;
l = strtol( s, &end, base );
if( ( errno == ERANGE && l == LONG_MAX ) || l > INT_MAX )
return OVERFLOW;
if( ( errno == ERANGE && l == LONG_MIN ) || l < INT_MIN )
return UNDERFLOW;
if( *s == '' || *end != '' )
return INCONVERTIBLE;
i = l;
return SUCCESS;
}
I get the following compile errors:
int_limits.cpp:16: error: expected identifier before numeric constant
int_limits.cpp:16: error: expected `}' before numeric constant
int_limits.cpp:16: error: expected unqualified-id before numeric constant
int_limits.cpp:16: error: expected `,' or `;' before numeric constant
int_limits.cpp:16: error: expected declaration before '}' token
I've looked for spelling errors, tried moving the location of the enum line around... I dunno what in the world is going on.
Any help with this issue would be GREATLY appreciated!
See Question&Answers more detail:
os