I have the following code:
//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开发者_StackOverflow社区 << line << endl;
}
return 0;
}
I'm trying to convert a string to an integer using Int32.TryParse(), but when I compile I get the error that Int32 was not declared in this scope. Not sure why this is coming up, am I missing a namespace declaration?? Any help is appreciated, thank you
UPDATE: Thanks for all the responses, as in the comment I posted, I'm not trying to use C++/CLI, how to I convert a string, declared from the string class, to an integer?
Try using atoi() instead. States is a std::string so you will need to say:
numStates = atoi( states.c_str() );
Int32::TryParse Method is not a native C++ API. Its C++ \CLI
method.
You will have to use .NET Framework and include the namespace System
to get it working.
If you just want to convert the string to integer, you can use: atoi() or refer FAQ : How do I convert a std::string to a number?
It looks like you are compiling a straight-C++ application using the .NET Int32
class to parse a value.
You'll either need to reference the System
namespace and CLR support if you are indeed compiling a .NET application, or use a function like atoi()
to parse your string value.
精彩评论