I have this encryption program that I spend a lot of time writing I am a beginner but I consider it a nice program except that is is way to slow, can you see anyway to speed this up?
Thanks.
I know you don't like doing stuff for us but I am really stressed about this program.
kernel>panic 3 Light Poster
kernel>panic 3 Light Poster
Oops forgot the code here is the .cpp file.
Thanks.
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#include <process.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <string.h>
#include <windows.h>
using namespace std;
string str;
void doCrypt(char *einput) {
system("CLS");
int pow;
int c=0;
ifstream in;
ofstream out;
const int MAX_FILE_SIZE = 900000000;
char* buf = new char[MAX_FILE_SIZE + 1];
int actualFileSize = 0;
char a;
in.open(einput);
if(in.fail()) {
in.close();
cout <<"Could not find file: "<<einput<<endl;
cout <<"Please type a command.\n"
<<"Type m for command menu,\n"
<<"Or type any other key to go back.\n";
return; }
if(in.is_open()) {
while(!in.eof()) {
in.get(buf[actualFileSize]);
if (!in.eof ()) {
actualFileSize++; }}
}
in.close();
buf[actualFileSize] = 0;
cout <<"File was found. Program ready to encrypt!\n";
system("PAUSE");
out.open(einput, ios::trunc);
for (int i = 0; i < actualFileSize; i++)
{
cout <<"Encrypting...\n";
a = buf[i] - 0x3;
out << a;
}
out.close();
cout <<"\nFILE SAVED!\n";
cout <<"Please type a command.\n"
<<"Type m for command menu,\n"
<<"Or type any other key to go back.\n";
return;
}
void doDcrypt(char *einput) {
system("CLS");
ifstream in;
ofstream out;
const int MAX_FILE_SIZE = 900000000;
char* buf = new char[MAX_FILE_SIZE + 1];
int actualFileSize = 0;
char a;
in.open(einput);
if(in.fail()) {
in.close();
cout <<"Could not find file: "<<einput<<endl;
cout <<"Please type a command.\n"
<<"Type m for command menu,\n"
<<"Or type any other key to go back.\n";
return; }
if(in.is_open()) {
while(!in.eof()) {
in.get(buf[actualFileSize]);
if (!in.eof ()) {
actualFileSize++; }
}
}
in.close();
buf[actualFileSize] = 0;
cout <<"File was found. Program ready to decrypt!\n";
system("PAUSE");
out.open(einput, ios::trunc);
for (int i = 0; i < actualFileSize; i++)
{
cout <<"Decrypting...\n";
a = buf[i] + 0x3;
out << a;
}
out.close();
cout <<"\nFILE SAVED!\n";
cout <<"Please type a command.\n"
<<"Type m for command menu,\n"
<<"Or type any other key to go back.\n";
return; }
int main(void) {
char comd;
char *einput=new char[20];
char *input=new char[81];
goto over;
over:
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type m for the command menu.\n";
over2:
cout <<"COMMAND: ";
while(true) {
cin >> comd;
switch(comd) {
case 'e' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doCrypt(einput);
break;
break;
case 'd' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doDcrypt(einput);
break;
break;
case 'm' :
cout <<"\n<COMMAND MENU>\n"
<<"*--------------------*\n"
<<"| e = 'Encrypt' |\n"
<<"| d = 'Decrypt' |\n"
<<"| m = 'Command Menu' |\n"
<<"| q = 'Quit' |\n"
<<"*--------------------*\n";
goto over2;
break;
break;
case 'q' :
cout <<"\nThanks For Using 'Crypto'\n";
system("PAUSE");
return 0;
break;
break;
default :
goto over;
break;
break; }
}}
thelamb 163 Posting Pro in Training
And how exactly do you expect us to help you?
I doubt anyone is going through your source(I could be blind but I don't even see any) to pinpoint for you where your bottlenecks are.
There exist 'code profilers' that can show you what section of your code is taking the most time, that is where you want to start looking to optimize your code. If you have found it and you are not sure what you can do to make it run faster you can post that and other relevant sections and we will have a look with you. But mostly in the beginning you can gain some performance with pure logic.
mrnutty 761 Senior Poster
I'm sorry but I don't know where to begin.
Your program needs indentation, and spaces. Its unclear. It
uses system commands. Its uses goto commands all over the
place. you allocate 900000000+1 memory twice and never delete it.
Your encrypt and decrypt program is way out of hand. Your program
is pretty bad.
I don't mean to be rude but its the truth.
When you are reading in a file all you have to do is this to encrypt:
ifstream iFile(filename);
char c;
string fileContent;
while(iFile.get(c))
{
fileContent += c;//now all the data in file is in fileContent.
};
for(int i = 0; i < fileContent.size(); i++)
fileContent[i] += 0x4f;
//open up another file to put the encrypted information
kernel>panic 3 Light Poster
Hey I can take the truth!
And I know my program sucks, but I have only been doing c++ about 2 years and I am only 14.
But thanks for the code this helps.
mrnutty 761 Senior Poster
I would advise you to read a good c++ book, maybe C++ primer plus
5th edition. Keep at it. Some part of your program was good.
And loose the goto and system commands.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.