Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 7th, 2005, 5:30 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,075
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
My First C++ Program

I need to learn C++ for school soon, so I thought I'd get started sooner with a quick script. This demonstrates the Vigenere Cypher, nothing too deep in the code. It's only heavily commented because I had to sell it.

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;

string Qualify(string text)                     // this will get rid of all non A-Z letters
{
    string result ("");                         // the result of the function
    int lp, let_val;                            // the loop indicator, and ascii value var
                    
    strupr((char *) text.c_str());              // convert all characters to upper case
     
    for(lp = 0; lp < text.length(); lp++)       // loop through every character in 'text'
    {   
        let_val = int(text[lp]);                // get the ascii value of the letter
        if (let_val > 64 && let_val < 91)       // if the letter is between 'A' and 'Z'
        {
            result += text[lp];                 // keep this letter               
        }
    }                                            
    return (result);                            // give back the result
}


string VigenereCypher(string text, string key)  // this encrypts a string into a Vigenere Cypher
{
    string result ("");                         // the result of our function
    text = Qualify(text);                       // remove the spaces and upper-case 'text'
    key = Qualify(key);                         // same to the 'key'
    
    int key_len(key.length()), lp, let_val, key_val; 
                                                // declared our variables
    for(lp = 0; lp < text.length(); lp++)       // loop through each character in 'text'
    {
        let_val = int(text[lp]);                // get the ascii value of the letter
        key_val = int(key[lp % key_len]);       // get the ascii value of our current key letter

/*  
basically, the Vigenere Cypher changes the value of the current letter by 
an amount equal to the current letter of the key, so to do this we must: 
  - add the two values together and subtract by 130 to get a value from 0-50,
  - confine the result to 0-25 by finding the remainder (%) of dividing by 26,
  - add the number to 65, because 65 = A, 66 = B, etc,
  - convert to ascii, then append to result */
        result += char((let_val + key_val - 130) % 26 + 65);
    } 
    return (result);                            // give back the result
}


int main ()
{
    string cypher, key, text;                   // declare variables

    cout << "Enter the text (no spaces): ";     // no spaces allowed in cin
    cin >> text;                                // get text from user

    cout << "Enter the key: ";
    cin >> key;                                 // get key from user
    
    cypher = VigenereCypher(text, key);         // using the text and key, get cypher
    
    ofstream ofile;
    ofile.open ("VigenereCyphers.txt", ofstream::out | ofstream::app);
    ofile << "Text:\n - " << text << "\nKey:\n - " << key << "\nCypher:\n - " << cypher << "\n\n";
    ofile.close();                              // appended all data to a text file
    
    cout << cypher << endl;                     // display the cypher
    cout << "\nAppended to: VigenereCyphers.txt" << endl;

    system("PAUSE");                            // wait for the user to press a key
    return 0;
}

And I realise there's some "bad form", like system("PAUSE"). But I couldn't do cin.get(), because after the user hits enter from the last cin, it seems to carry through over to cin.get().

Oh well, wee.
Sane is offline   Reply With Quote
Old Dec 7th, 2005, 5:36 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Noice.

BTW, try:
cin.sync(); // clears the input buffer (apparently)
cin.get();
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 7th, 2005, 5:57 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
And just for future reference, a C++ program is not a script
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is online now   Reply With Quote
Old Dec 7th, 2005, 6:56 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Not bad for a first try. You should use <cstdlib> and <string> instead of <stdlib.h> and <string.h>. Using namespace std is also (often) not good practice. Instead try: using std::cout; etc.
You also know that you are relying on the ascii character set here, and that other character sets might not have A at 65.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 7th, 2005, 6:57 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,075
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
What's the difference between those two sets of includes?

And in what context are there different character sets? =O
Sane is offline   Reply With Quote
Old Dec 7th, 2005, 6:59 PM   #6
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
There's the EBCDIC set used by some computers

I don't really know a more efficient way for performing character checks than to write a function that takes a character as an argument and returns c == 'A' || c == 'B' || ... || c == 'Z' where c is that character.

Unless of course you use the isalpha function from the standard string library.
UnKnown X is offline   Reply With Quote
Old Dec 7th, 2005, 7:01 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
There is also EBCDIC. But I think you don't have to worry about that in *nix and windows worlds, since they use ascii and unicode.

Edit: Need to type faster
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 7th, 2005, 7:03 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,075
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Ah I see.

And by the way, this is why I love Python.

return''.join(chr((ord(k[a%len(k)])+ord(t[a]))%26+65)for a in range(len(t)))
So much shorter then that C++ code. XD

EDIT: Oh, and thanks Ooble! I'll try that. Nice Avatar too.
Sane is offline   Reply With Quote
Old Dec 7th, 2005, 8:39 PM   #9
Munsta
Newbie
 
Join Date: Nov 2005
Posts: 9
Rep Power: 0 Munsta is on a distinguished road
EBCDIC charset is used on IBM mainframes but you shouldn't worry about that because they don't run Windows or *nix.
Munsta is offline   Reply With Quote
Old Dec 7th, 2005, 8:41 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
So much shorter then that C++ code. XD
Of course, you're enjoying riding on someone else's shoulders (nothing wrong with that, either). There's a hell of a lot of C/C++ code behind that, helping you out.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC