2009-11-24 3 views
0

나는 다음과 같은 코드가 있습니다INT32은이 범위에서 선언하지

//Comp454 program 2 
#include <iostream> 
#include <string> 
#include <fstream> // file I/O support 
#include <cstdlib> // support for exit() 
const int SIZE = 60; 

int main() 
{ 
using namespace std; 
string states; 
int numStates = 0, i = 0, stateVar = 0; 
string line; 
char filename[SIZE]; 
ifstream inFSM, inString; 

//Open FSM definition 
cout << "Enter name of FSM definition: "; 
cin.getline(filename, SIZE); 
inFSM.open(filename); //Associate inFile with a file 
if (!inFSM.is_open()) // failed to open file 
{ 
cout << "Could not open the file " << filename << endl; 
cout << "Program terminating.\n"; 
exit(EXIT_FAILURE); 
} 

//Process FSM definition line by line until EOF 
getline(inFSM, states); 
numStates = Int32.TryParse(states); 

//Check for num of states 
if(numStates > 10) 
{ 
    cout << "There can be no more than 10 states in the FSM definition, exiting now." << endl; 
    return 0; 
} 

while (!inFSM.eof()) // while input good and not at EOF 
{ 
    getline(inFSM, line); 
    cout << line << endl; 
} 

return 0; 
} 

내가 Int32.TryParse()를 사용하여 문자열을 정수로 변환하기 위해 노력하고있어,하지만 난 컴파일 할 때 오류가 얻을를 INT32 이 범위에서 선언되지 않았습니다. 이것이 왜 나올지 모르겠다. 네임 스페이스 선언을 놓쳤는가 ?? 어떤 도움을 주셔서 감사합니다,

업데이트 : 내가 게시 한 의견에서와 마찬가지로 모든 응답 주셔서 감사합니다, 나는 C++/CLI, 문자열 클래스에서 선언 된 문자열을 변환하는 방법을 사용하려고하지 않아요 , 정수로?

답변

3

대신 atoi()를 사용해보십시오. 당신이 해석 한 경우 atoi` 더 오류보고 메커니즘이 없습니다`로 (당신이 모르는,

numStates = atoi(states.c_str()); 
+1

내가 오히려'strtol' 조언을 것입니다 : 당신이 말할 필요가 있도록 미국은 표준 : : 문자열 숫자가 올바르지 않은 경우 또는 숫자가 아닌 문자열을 파싱하지 못한 경우 - 두 경우 모두'atoi '에서'0'을 얻음). –

1

.NET Int32 클래스를 사용하여 값을 구문 분석하는 스트레이트 -C++ 응용 프로그램을 컴파일하는 것처럼 보입니다.

실제로 .NET 응용 프로그램을 컴파일하는 경우 System 네임 스페이스 및 CLR 지원을 참조하거나 atoi()과 같은 함수를 사용하여 문자열 값을 구문 분석해야합니다.

관련 문제