Not a bad program, one thing I would do is have it read the value of N from the command line.
Here is some code I found (with comment saying where I got it) that does a string to integer conversion:
Code:
#include <string>
// STRING CONVERSION FROM CODEGURU
// http://www.codeguru.com/forum/showthread.php?t=231054
template <class T>
bool from_string(T& t,const std::string& s,std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
// Converts a string to an integer
int convStrInt(std::string inStr)
{
// Variable Declaration
int iVal;
// The third parameter of from_string() should be
// one of std::hex, std::dec or std::oct
if(from_string<int>(iVal, inStr, std::dec))
{
return iVal;
}
else
{
std::cout << "from_string failed" << std::endl;
std::exit(1);
}
} // End convStrInt
I don't fully know how the first function works, but it does and does it very well.
Bookmarks