![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() ![]() |
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. ![]() |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
|
|
|
|
|
|
#3 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
And just for future reference, a C++ program is not a script
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
What's the difference between those two sets of includes?
And in what context are there different character sets? =O |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() |
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))) EDIT: Oh, and thanks Ooble! I'll try that. Nice Avatar too. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Nov 2005
Posts: 9
Rep Power: 0
![]() |
EBCDIC charset is used on IBM mainframes but you shouldn't worry about that because they don't run Windows or *nix.
|
|
|
|
|
|
#10 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|