|
Programming Help
|
| Caesar |
Posted on 11/18/2013 15:17:54
|
Member

Posts: 179
Joined: 22.08.13
|
Okay, so for my Programming Class, we're doing C++ Caesar Cipher. I already have the whole thing written (With a lot of Else if Statements), but now we're trying to add functions to the program. I doubt any of you (Unless you're in college) have code blocks so I have decided to post the program here (Warning it's long) and I ask that someone help me :(
Code//***********************************************~********************
//Caesar Cipher. This program out puts a simple letter substitution
//cipher used by Julius Caesar. ROT13 replaces a letter with 13
//letters that come after in the alphabet.
//*********************************************************************
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
char caesar( char );
int main()
{
string letter;
ofstream outData;
ifstream inData;
char character;
cout << "Enter file name" << endl;
cin >> letter;
//open file
inData.open (letter.c_str());
outData.open ("note.out");
inData >> character;
character= toupper(character);
while(inData)
{
if (character == 'A')
{
outData << "N";
}
else if (character == 'B')
{
outData << "O";
}
else if (character == 'C')
{
outData << "P";
}
else if (character == 'D')
{
outData << "Q";
}
else if (character == 'E')
{
outData << "R";
}
else if (character == 'F')
{
outData << "S";
}
else if (character == 'G')
{
outData << "T";
}
else if (character == 'H')
{
outData << "U";
}
else if (character == 'I')
{
outData << "V";
}
else if (character == 'J')
{
outData << "W";
}
else if (character == 'K')
{
outData << "X";
}
else if (character == 'L')
{
outData << "Y";
}
else if (character == 'M')
{
outData << "Z";
}
else if (character == 'N')
{
outData << "A";
}
else if (character == 'O')
{
outData << "B";
}
else if (character == 'P')
{
outData << "C";
}
else if (character == 'Q')
{
outData << "D";
}
else if (character == 'R')
{
outData << "E";
}
else if (character == 'S')
{
outData << "F";
}
else if (character == 'T')
{
outData << "G";
}
else if (character == 'U')
{
outData << "H";
}
else if (character == 'V')
{
outData << "I";
}
else if (character == 'W')
{
outData << "J";
}
else if (character == 'X')
{
outData << "K";
}
else if (character == 'Y')
{
outData << "L";
}
else if (character == 'Z')
{
outData << "M";
}
else
{
cout << "Incorrect input" << endl;
}
inData >> character;
}
return 0;
}
Edited by ninja on 11/18/2013 18:24:02
My name is Julius Augustus Octavianus Joseph Vissarionovich Stalin Brutus
I am DrkInferno in case you didn't know. booooooo (didn't change that) Actually the other way around. |
| |
|
|
| Caesar |
Posted on 11/18/2013 15:18:26
|
Member

Posts: 179
Joined: 22.08.13
|
WTF?! Those smiley faces should be semi colons!
My name is Julius Augustus Octavianus Joseph Vissarionovich Stalin Brutus
I am DrkInferno in case you didn't know. booooooo (didn't change that) Actually the other way around. |
| |
|
|
| Vlexis XII |
Posted on 11/18/2013 15:57:15
|

Admin

Posts: 1199
Joined: 16.07.13
|
I am of no help here because I know nothing about programming, but I lol'd at the winking smiley faces. xD
To get rid of them, edit the reply and check the box that says "Disable Smileys." :P
Edited by Vlexis XII on 11/18/2013 15:58:05
I like owls. Do you like owls? (@v@)

 |
| |
|
|
| Target |
Posted on 11/18/2013 16:13:06
|

Numpty

Posts: 1425
Joined: 19.07.12
|
I have to start a refresher course on C++; Just a thought..wouldn't it be easier to have conditional statements so there's not 500 lines? Also, wuts with the outdata? I thought it was cout to print the data..
/admittedly my memory is a bit hazy on the topic..
Edited by Target on 11/18/2013 16:13:56
Lasso Lassie's Legendary Legless Lego-Legolas!


 |
| |
|
|
| ZA Bvigil |
Posted on 11/18/2013 16:21:55
|

Admin

Posts: 2161
Joined: 15.11.10
|
There are multiple methods for outputting data I personally use cout and ya i'd suggest using a conditional statement if you'd like I can find my version of this program but with conditional statements that make it like only 3 lines long
also i'm a bit confused by your question that you need help with I can help you as i'm a comp science major but again not quite sure what you're asking

 |
| |
|
|
| SpartanJaz |
Posted on 11/18/2013 16:38:11
|

Junior Member

Posts: 34
Joined: 13.08.12
|
I might be wrong but I seem to remember functions being initialized with:
int FunctionName()
So for example, initializing the OpenFile() function would be done as follows:
int OpenFile()
{
inData.open (letter.c_str());
outData.open ("note.out"Wink;
inData >> character;
character= toupper(character);
}
Initialize the function in this way before int main.
You can then call the function within int main by simply typing
OpenFile();
Haven't got Visual installed atm though so can't test it. That's the method I believe I've used in the past though.(Though I haven't even looked at C++ for at least a couple months now)
Oh, and you might have to change int appropriately for the content of the function.(So if you're returning a value which an int is not suitable for, substitute int for something else. (So my example may be incorrect, so don't copy and paste it ) |
| |
|
|
| Vulture |
Posted on 11/18/2013 17:11:54
|

Newbie

Posts: 0
Joined: 25.08.12
|
I would help if I was an expert at program language, but I'm afraid I can't help as I'm weak at it. Sorry. :/

Proud to be in the Jajaja group! |
| |
|
|
| Mist |
Posted on 11/18/2013 17:59:52
|

Numpty

Posts: 1242
Joined: 22.07.13
|
I would recommend pm-ing or talking with Vulture. Vulture is extremely smart and is very familiar with that stuff and i spam him all the time about my pc problems/questions. Also, i didn't read Vult's post above so if he said something like "i have no idea" well then i have NO idea what to do LOL maybe try like a carnage or someone smart like that idk |
| |
|
|
| ninja |
Posted on 11/18/2013 18:15:38
|

Super Admin

Posts: 4174
Joined: 15.11.10
|
I have Codeblocks and know some basic C++, but it's hard to help if you don't format your code properly, and you don't ask any questions.
E: I re-formatted your code so that it's readable.
Have you thought about generalising your code? At the moment, it will only shift 13 places.
Did you know that you can treat characters as integers (using ascii), so that you don't have to use loads of if statements?
Edited by ninja on 11/18/2013 18:51:08


My brain is open.
- Paul Erdős |
| |
|
|
| ZA Bvigil |
Posted on 11/18/2013 19:12:49
|

Admin

Posts: 2161
Joined: 15.11.10
|
what ninja said is the method i'd suggest treating characters as integers is the best way to do a function like this

 |
| |
|
|
| ninja |
Posted on 11/18/2013 20:25:35
|

Super Admin

Posts: 4174
Joined: 15.11.10
|
ZA Bvigil wrote:
what ninja said is the method i'd suggest treating characters as integers is the best way to do a function like this
Yes, it works very neatly.
I hadn't written a Caesar shift program in C++ before (I mostly use Python), so I just had a go, and got it down to about 20 lines of code using that technique. Most of that is set-up and text I/O, too - the actual encoding is about 3/4 lines (although it uses loops and modular arithmetic, which you may not have met).


My brain is open.
- Paul Erdős |
| |
|
|
| Vulture |
Posted on 11/18/2013 23:58:50
|

Newbie

Posts: 0
Joined: 25.08.12
|
Mist wrote:
I would recommend pm-ing or talking with Vulture. Vulture is extremely smart and is very familiar with that stuff and i spam him all the time about my pc problems/questions. Also, i didn't read Vult's post above so if he said something like "i have no idea" well then i have NO idea what to do LOL maybe try like a carnage or someone smart like that idk
Did you not read post #7? lol

Proud to be in the Jajaja group! |
| |
|
|
| Mist |
Posted on 11/19/2013 00:10:15
|

Numpty

Posts: 1242
Joined: 22.07.13
|
Vulture wrote:
Mist wrote:
I would recommend pm-ing or talking with Vulture. Vulture is extremely smart and is very familiar with that stuff and i spam him all the time about my pc problems/questions. Also, i didn't read Vult's post above so if he said something like "i have no idea" well then i have NO idea what to do LOL maybe try like a carnage or someone smart like that idk
Did you not read post #7? lol
LMFAO! nope lol |
| |
|
|
| ninja |
Posted on 11/19/2013 22:12:58
|

Super Admin

Posts: 4174
Joined: 15.11.10
|
Did you sort it out, or do you not actually want help?


My brain is open.
- Paul Erdős |
| |
|