| | |
Why does this not work?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
C++ Syntax (Toggle Plain Text)
int main(int check) { char comd;
Actually, the problem starts with the above two lines. main() must have either no parameters or two prameters
int main(int argc, char* argv[]) There are no other options, except possible adding a third parameter which is normally an array of environment strings. And you are NEVER allowed to call main() from within the program. That function is reserved by the compiler to be the entry point into your program.>> gets(pass1);
NEVER EVER use gets() in either C or C++ programs because it can corrupt your program's memory. It has no checks to see that you can type more characters into the array then the charcater array can hold. In C++ use either
cin >> pass1 or cin.getline(pass1, sizeof(pass1)); >>goto over;
Replace that with the break statement. goto in C and C++ is considered to be poor coding practice.
Last edited by Ancient Dragon; Sep 9th, 2009 at 12:07 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
It is not a real .dll file just a .txt file.
Oh and the part I need help with says:
//Problems start here
//<------------------------------------------->
//Problems end here
//<------------------------------------------->
Can you help me, thanks.
Call it a text file then. Anyone seeing a .dll extension expects it to be a DLL file. Anyone seeing .txt expects it to be a text file. Anyone seeing a .csv extension expects a comma separated text file. The same goes for labels.
C++ Syntax (Toggle Plain Text)
over: system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; over2: 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; } } }
over? over2? How about DisplayMenu or something?
Anyway, here's an old thread on the same topic where you put in some comments, but then took them out here.
http://www.daniweb.com/forums/thread213717.html
I think people are expecting functions like:
C++ Syntax (Toggle Plain Text)
string decrypt (string encryptedMessage, string key); string encrypt (string plainText, string key);
Sticking code like this:
C++ Syntax (Toggle Plain Text)
fileContent[i] -= 0x4f;
with no explanation isn't going to make sense. Some indentation would also make it more readable.
Basically, indentation, comments, good filenames, descriptive labels, and descriptive variables like "key", "plainText", "encryptedText" are necessary if you want people to follow your code.
1) Learn to format code in a readable style.
2) Remove all those unnecessary header files. You can always add some back when you need them.
2) Remove all those unnecessary header files. You can always add some back when you need them.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; string str; bool password(); void doCrypt(char *einput) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(einput); if(in.fail()) { in.close(); cout <<"Could not find file: "<<einput<<endl; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } while(in.get(c)) { fileContent += c; } in.close(); for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] += 0x4f; } out.open(einput, ios::trunc); out << fileContent; out.close(); cout <<"FILE ENCRYPTED!\n"; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } void doDcrypt(char *einput) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(einput); if(in.fail()) { in.close(); cout <<"Could not find file: "<<einput<<endl; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } while(in.get(c)) { fileContent += c; for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] -= 0x4f; } } in.close(); out.open(einput, ios::trunc); out << fileContent; out.close(); cout <<"FILE DECRYPTED!\n"; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } int main(int argc, char* argv[]) { char einput[20]; //Problems Start Here //<------------------------------------------------------------------------------> char input_line[81]; char pass1[50]; char pass2[50]; char comd; ifstream file_in("setpCS4.dll"); if (! file_in) { file_in.close(); cout <<"Please make a password: "; cin >> pass2; ofstream file_out("setpCS4.dll"); file_out <<pass2; file_out.close(); cout <<"Password made.\n"; system("PAUSE"); return 1; } cout <<"Please type your password: "; cin.getline(pass1, sizeof(pass1)); while( file_in.getline(input_line, 80) ) { if(strcmp(input_line, pass1)) { break; } } //Problems End Here //<------------------------------------------------------------------------------> system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; while(true) { cin >> comd; switch(comd) { case 'e' : system("CLS"); cout <<"Please type name of input file(.txt): "; cin >> einput; doCrypt(einput); break; case 'd' : system("CLS"); cout <<"Please type name of input file(.txt): "; cin >> einput; doDcrypt(einput); break; case 'm' : cout <<"\n<COMMAND MENU>\n" <<"*--------------------*\n" <<"| e = 'Encrypt' |\n" <<"| d = 'Decrypt' |\n" <<"| m = 'Command Menu' |\n" <<"| q = 'Quit' |\n" <<"*--------------------*\n"; break; case 'q' : cout <<"\nThanks For Using 'Crypto'\n"; system("PAUSE"); return 0; break; default : break; } } }
Last edited by Ancient Dragon; Sep 9th, 2009 at 1:04 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
Thanks but it still has the same problem.
I have tried everything I can think of.
•
•
•
•
1) Learn to format code in a readable style.
2) Remove all those unnecessary header files. You can always add some back when you need them.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; string str; bool password(); void doCrypt(char *einput) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(einput); if(in.fail()) { in.close(); cout <<"Could not find file: "<<einput<<endl; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } while(in.get(c)) { fileContent += c; } in.close(); for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] += 0x4f; } out.open(einput, ios::trunc); out << fileContent; out.close(); cout <<"FILE ENCRYPTED!\n"; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } void doDcrypt(char *einput) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(einput); if(in.fail()) { in.close(); cout <<"Could not find file: "<<einput<<endl; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } while(in.get(c)) { fileContent += c; for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] -= 0x4f; } } in.close(); out.open(einput, ios::trunc); out << fileContent; out.close(); cout <<"FILE DECRYPTED!\n"; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } int main(int argc, char* argv[]) { char einput[20]; //Problems Start Here //<------------------------------------------------------------------------------> char input_line[81]; char pass1[50]; char pass2[50]; char comd; ifstream file_in("setpCS4.dll"); if (! file_in) { file_in.close(); cout <<"Please make a password: "; cin >> pass2; ofstream file_out("setpCS4.dll"); file_out <<pass2; file_out.close(); cout <<"Password made.\n"; system("PAUSE"); return 1; } cout <<"Please type your password: "; cin.getline(pass1, sizeof(pass1)); while( file_in.getline(input_line, 80) ) { if(strcmp(input_line, pass1)) { break; } } //Problems End Here //<------------------------------------------------------------------------------> system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; while(true) { cin >> comd; switch(comd) { case 'e' : system("CLS"); cout <<"Please type name of input file(.txt): "; cin >> einput; doCrypt(einput); break; case 'd' : system("CLS"); cout <<"Please type name of input file(.txt): "; cin >> einput; doDcrypt(einput); break; case 'm' : cout <<"\n<COMMAND MENU>\n" <<"*--------------------*\n" <<"| e = 'Encrypt' |\n" <<"| d = 'Decrypt' |\n" <<"| m = 'Command Menu' |\n" <<"| q = 'Quit' |\n" <<"*--------------------*\n"; break; case 'q' : cout <<"\nThanks For Using 'Crypto'\n"; system("PAUSE"); return 0; break; default : break; } } }
Line 9 - Call the function Encrypt. That's what people expect it to be called, or something very similar. Call the parameter plainText for the same reason.
Line 56 - Call the function Decrypt. That's what people expect it to be called, or something very similar. Call the parameter encriptedText or cipherText. Make it extremely obvious what it is.
Lines 45 - 52 - This shows up over and over, so stick it in it's own function called
DisplayWelcomeMessage () or something similar.Line 118 - If this the file that contains a password (password for what, by the way? Is this the encryption key?), call it "password.txt" or something.
Lines 118 - 140 - What's the point of all this? It has nothing to do with encryption or decryption. It's just a password that I have enter in order to run your program?
Line 85 - If this is the key, it probably shouldn't be hard-coded. Have a variable called key. And there are all sorts of algorithms out there for encryption/decryption. Add some comments to explain which one your program uses.
I mentioned a lot of this before, but it bears repeating.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
I did everything you said and now stuff is really screwed up(Not because of you just because I must have done it wrong) But anyway I am sorry to take so much of your time I will probably figure it out some time.
But if you are interested here is the new source.
Thanks.
You did some of the things I said, but mostly you kept your old program. You still hard-code the key, you still call have a key that makes things impossible to check (I changed it to 0x01 - much easier - I mentioned that in your previous thread), you still don't call the key a key, and you still don't have comments as to what you are doing. Here's some revised code.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; string str; void Message() { cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } void Encrypt(char *filename, int key) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(filename); if(in.fail()) { in.close(); cout <<"Could not find file: "<<filename<<endl; system("PAUSE"); system("CLS"); Message(); return; } while(in.get(c)) { fileContent += c; } in.close(); for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] += key; } out.open(filename, ios::trunc); out << fileContent; out.close(); cout <<"FILE ENCRYPTED!\n"; system("PAUSE"); system("CLS"); Message(); } void Decrypt(char *filename, int key) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(filename); if(in.fail()) { in.close(); cout <<"Could not find file: "<<filename<<endl; system("PAUSE"); system("CLS"); Message(); return; } while(in.get(c)) { fileContent += c; for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] -= key; } } in.close(); out.open(filename, ios::trunc); out << fileContent; out.close(); cout <<"FILE DECRYPTED!\n"; system("PAUSE"); system("CLS"); Message(); } bool PasswordCheck () { /* ifstream file_in("setpCS4.dll"); if (! file_in) { file_in.close(); cout <<"Please make a password: "; cin >> pass2; ofstream file_out("setpCS4.dll"); file_out <<pass2; file_out.close(); cout <<"Password made.\n"; system("PAUSE"); return 1; } cout <<"Please type your password: "; cin.getline(pass1, sizeof(pass1)); while( file_in.getline(input_line, 80) ) { if(strcmp(input_line, pass1)) { break; } else { return 0; } } */ return true; } int main(int argc, char* argv[]) { char filename[20]; char input_line[81]; char pass1[50]; char pass2[50]; char comd; int key = 0x01; if (!PasswordCheck ()) { cout << "Bad password. Exiting program.\n"; return 0; } system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; while(true) { cin >> comd; switch(comd) { case 'e' : system("CLS"); cout <<"Please type name of file to encrypt (.txt): "; cin >> filename; Encrypt(filename, key); break; case 'd' : system("CLS"); cout <<"Please type name of file to encrypt (.txt): "; cin >> filename; Decrypt(filename, key); break; case 'm' : cout <<"\n<COMMAND MENU>\n" <<"*--------------------*\n" <<"| e = 'Encrypt' |\n" <<"| d = 'Decrypt' |\n" <<"| m = 'Command Menu' |\n" <<"| q = 'Quit' |\n" <<"*--------------------*\n"; break; case 'q' : cout <<"\nThanks For Using 'Crypto'\n"; system("PAUSE"); return 0; break; default : break; } } }
Set up a text file with a single line with "uvwxy" in it. Run it, encrypt it, and you get "vwxyz", which is correct. Now decrypt it and see what you get. You have a problem in your Decrypt function, which I have left. Otherwise I have cleaned up your program a little.
I also took the PasswordCheck out and put it in its own function, though I commented it out. Put it back in if you like, but at least now it's well named and it's out of the way.
![]() |
Similar Threads
- Need 2 techies for tech-support, work from home (Tech / IT Consultant Job Offers)
- Requesting shadowing work at home job (Software Development Job Offers)
- 3D Engine Developer Needed from India - Work from home (Web Development Job Offers)
Other Threads in the C++ Forum
- Previous Thread: Command Line Arguments(CLA)
- Next Thread: 1540-0063 (S) The text "eventqueue_t" is unexpected.
| Thread Tools | Search this Thread |
+ account add aprilfool assembly auto blackmail block browser buttons c# c++ challenge char character click code compression computer content control cool crack csv data decryption dell design desktop ediscovery email encryption enterprise excel explorer facebook favouriteforums file firefox form funny generated gnome government growth guess guessing hardware input interface java karmic kioti16 kmip linked linkedin linux list login microsoft mssqlbackend netbeans news number object opensource panel password php playstation program programing protection ps3 qualitative research save security simple single size softwaredevelopment string sun super survey test time torvalds troubleshoot twitter user useraccounts vb view virus vista webmail windows winforms







