Results 1 to 3 of 3

Thread: Generic Caesarian Cipher (rotN)

  1. #1
    Join Date
    Jan 2006
    Location
    Surrey; UK
    Beans
    362
    Distro
    Kubuntu Intrepid Ibex (testing)

    Generic Caesarian Cipher (rotN)

    I've just finished a quick and dirty rotN program in C++:

    www.randomskk.net/uploads/rotN.html

    It's not exactly.. elegant, though. What do you guys think?

    Also, as an aside: any ideas on how to get the current date (ie, 04, 28, etc) of the day of the month from C++?
    "Rest not, time is sweeping by. Go and dare before you die. Something mighty and sublime, leave behind to conquer time."

  2. #2
    Join Date
    Sep 2005
    Location
    Cedar Rapids, IA, USA
    Beans
    545
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Generic Caesarian Cipher (rotN)

    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.
    #399784 | Ubuntu User #287
    *** If you're going to program, install the damn build-essential package ***
    There is no such thing as Ubuntu specific programming
    Save the electrons - if you quote, trim!

  3. #3
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: Generic Caesarian Cipher (rotN)

    If you want dynamic-esque objects in C++ string streams are the way to go...

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    class dynamic {
    	public:
    		stringstream strValue;
    		dynamic(){}
    
    		template <typename T>
    		void operator=(T value){strValue.str(""); strValue << value;}
    
    		template <typename T>
    		void operator<<(T value){strValue << value;}
    
    		template <typename T>
    		void operator>>(T& value){strValue >> value;}
    
    		string c_str(){return strValue.str();}
    		int    c_int(){return atoi(strValue.str().c_str());}
    		float  c_flt(){return atof(strValue.str().c_str());}
    };
    
    int main(){
    	dynamic myVariable;
    
    	myVariable = 10.7;
    	cout << myVariable.c_int()+ 3  << endl;
    	cout << myVariable.c_flt()+ 3  << endl;
    	cout << myVariable.c_str()+'3' << endl;
    
    	myVariable = "";
    	myVariable << "1";
    	myVariable << 2;
    	myVariable << 3.5;
    	myVariable << 6;
    	myVariable << "7";
    	cout << myVariable.c_str() << endl;
    
    	myVariable = myVariable.c_flt() * 2;
    	cout << myVariable.c_str() << endl;
    
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •