Я тестирую код удаленно на машине Solaris через SSH Secure Shell, используя c ++.Не уверен, что это за версия;Solaris, c ++ / компилятор и т. Д. (И не знаю, как узнать через SSH Secure Shell) ...
Этот код:
#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 << "\nEnter a string containing numbers: ";
cin >> str;
code = str_to_int( num, str.c_str(), 0 );
if( code == OVERFLOW || code == UNDERFLOW )
cout << "\nThe number was out of int's range\n\n";
else if( code == INCONVERTIBLE )
cout << "\nThe string contained non-number characters\n\n";
else if( code == SUCCESS )
cout << "\nThe int version of the string is: " << num << "\n\n";
}
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 == '\0' || *end != '\0' )
return INCONVERTIBLE;
i = l;
return SUCCESS;
}
компилируется и работает нормально... как вы можете видеть, он преобразует введенную пользователем строку в int ...
Но при изменении так:
#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 << "\nint's max limit: "
<< numeric_limits<int>::max()
<< "\nint's min limit: "
<< numeric_limits<int>::min()
<< "\n\nnumber of digits in the largest int: "
<< size_of( numeric_limits<int>::max() )
<< "\nnumber of digits in the smallest int: "
<< size_of( numeric_limits<int>::min() );
cout << "\nEnter a string containing numbers: ";
cin >> str;
code = str_to_int( num, str.c_str(), 0 );
if( code == OVERFLOW || code == UNDERFLOW )
cout << "\nThe number was out of int's range\n\n";
else if( code == INCONVERTIBLE )
cout << "\nThe string contained non-number characters\n\n";
else if( code == SUCCESS )
cout << "\nThe int version of the string is: " << num << "\n\n";
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 == '\0' || *end != '\0' )
return INCONVERTIBLE;
i = l;
return SUCCESS;
}
Я получаю следующие ошибки компиляции:
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
Я искал орфографические ошибки, пытался переместить местоположение строки перечисления вокруг ... Я не знаю, что происходит в мире.
Любая помощь в этом вопросе будетС благодарностью!