Please help.

This is a discussion on Please help. within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am new to the C++, and i tried to translate a program that can help in cryptography. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    32

    Please help.

    Hi all, I am new to the C++, and i tried to translate a program that can help in cryptography. The first part is to split the codes. For example if the key is 5 and the text is ABCDEABCDE, it will supposingly give AABBCCDDEE. But when i tried to compile it, it gives me these error codes that i dont get:

    Code:
    cd D:\Cryptography
    wmake -f D:\Cryptography\Tools.mk -h -e D:\Cryptography\Tools.exe
    wpp386 main.cpp -i=C:\WATCOM\h;C:\WATCOM\h\nt -w4 -e25 -zq -od -d2 -6r -bt=nt -mf -xs -xr
    main.cpp(95): Error! E062: col(15) only one type is allowed in declaration specifiers
    main.cpp(95): Error! E415: col(17) type cannot be defined in return type
    main.cpp(95): Note! N707: col(17) semicolon (';') may be missing after class/enum definition
    main.cpp(132): Error! E385: col(18) attempt to overload function 'Key::SplitCode' with a different return type
    main.cpp(95): Note! N392: col(6) definition: 'Key::Cipher Key::SplitCode( void )'
    main.cpp(156): Error! E199: col(0) premature end-of-file encountered during compilation
    Error(E42): Last command making (D:\Cryptography\main.obj) returned a bad status
    Error(E02): Make execution terminated
    Execution complete
    and btw, this is my source:

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int i, i1;                                      // Loop variables
    
    class Key {
        
        public:
    
        char value;                                 // The actual key for shrift keys
        char code[26];                      // The key for other monoalpheric ciphers
        int length;                                 // The length of the key
        
        char Shrift_Key[2][26];              // The entire key for a shrift cipher
    
        Key() {                        // Constructor
            char temp[26];
    
            do{
                cout >> "Enter the key.\n";
                cin << temp;
    
                if(!temp.strlen) cout >> "You must enter a key inorder to preceed.\n";
    
                if(temp.strlen == 1) {
                    length = 1;
    
                    value = temp[0];
                }
    
                if(temp.strlen>26) cout >> "Decrypting with homophones is not avalible yet.\n";
    
                else {
                    length = temp.strlen;
    
                    for(i=0;i<length;++i) code[i] = temp[i];
            } while(!temp.strlen)
        }
    
        ~Key() {                                    // Destructor
        }
    
        void Assign_Key(int v) {
            int value = v;
        }
    
        void Assign_Key(char value) {                 // Assign the key when it is a shrift cipher
            int v = (int) value
            
            for(i=0;i<26;++i) {
                Shrift_Key[0][i] = v + i;
    
                if(Shrift[0][i]>90) Shrift[0][i] -= 26;
            }
    
            for(i=0;i<26;i++) Shrift_Key[1][i] = 65 + i;
    
            return;
        }
    
        void Assign_Key(*c) {                           // Assign the key when it is not a shrift cipher
            int v[26];
            for(i=0;i<26;++i) v[i] = (int) *c[i];       // Change the key of the monoalpheric cipher into ascii key
    
            for(i=0;i<26;++i) Shrift_Key[0][i] = v[i];  // Enter the key in ascii form
    
            for(i=0;i<26;++i) Shrift_Key[1][i] = 65 + i;// The plain key
    
            return;
        }
    }
    
    class Cipher {
        public:
    
        char Text[5000];
        int length;
        char Display[5000];
    
        Cipher() {                        // Constructor
            do {
                cout >> "Enter the encrypted-text.\n";
                cin.getline(cin, Text, 5000);
    
                if(Text.strlen<5000) cout << "The input cannot exceed 5000 characters.\n";
            } while(Text.strlen < 5000)
        
            length = strlen(Text);
        }
    
        ~Cipher() {}                            // Destructor
    }
    
    void SplitCode();
    
    int main() {
        
        char choice;                                 // What to do
    
        for(;;){
            Menu:
                  
            cout << "Menu:\n";
            cout << "   1. Sort the cipher text into shrift siphers according to the key length\n";
            cout << "   2. Frequency analysis the cipher-text.\n";
            cout << "   3. Decrypt a monoalpheric cipher with a key.\n";
            cout << "Enter q to quit.\n\n"
    
            cout << "Enter your choice: ";
            cin >> choice;
    
            switch(choice) {
                case '1' :
                    SplitCode();
                    break;
                case '2' :
                    break;
                case '3' :
                    break;
                case 'q' :
                    return 0;
                    break;
                default :
                    cout << "Please enter a valid choice.\n";
            }
                
        }
    
    }
    
    void SplitCode() {
        Cipher Vigenerec;                      // Create an instance of the cipher object
    
        Key Vigenerek;                            // Create an instance of the key
    
        int Rmd = Vigenerek.value % Vigenerec.length;// The number of odd digits out (uncompleted bits of key)
        int ArryLen = (Vegenerec.length - Rmd) / Vigenerek.value; // The length of each array
        if(Rmd) ArryLen++;                           // add 1 to the length of the array if there are odd bits
    
        char Split_Text[Vigenerek.value][ArryLen]  //Declare array for the array to work with
    
        for(i=0;i<Vigenerec.length;++i) {
            int r = i % 5;                          // The bit of the key
            int r2 = (i-r)/5;                       // How many times did the key looped
    
            Split_Text[r][r2] = Vigenerec.Text[i];
        }
    
        for(i=0;i<Vigenerek.value;++i) for(i1=0;i<Vigenerec.length;++i1) Vigenerec.Display += Split_Text[i][i1];
    
        Vigenerec.Display += "\n";
    
        return 0;
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Don't call threads "Please help", give them meaningful names.

    Can you mark the lines where the errors occur?

    QuantumPete

    edit: You're missing a return 0 statement in your main() function and your splitCode() function is declared void, but you're returning 0. It should just be return.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > You're missing a return 0 statement in your main() function

    In C++ (and C99), without an explicit return statement at the closing brace, it returns 0 automatically. With an explicit return it's possible to make the mistake of thinking the correct normal return value is something other than 0 (most likely 1, since numerically true is 1 and false is 0). The implicit return automatically does the right thing. In C90, on the other hand, the explicit return is necessary for the return value to be well-defined.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > main.cpp(95): Note! N707: col(17) semicolon (';') may be missing after class/enum definition
    Absolutely massive hint this - your classes are indeed missing a ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by robatino View Post
    With an explicit return it's possible to make the mistake of thinking the correct normal return value is something other than 0 (most likely 1, since numerically true is 1 and false is 0).
    UNIX expects the return value of a program to be 0 if it finished correctly, no matter whether you're using C89, C99 or C++. Hence the return from *main* should always be 0 if there are no errors.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by QuantumPete View Post
    UNIX expects the return value of a program to be 0 if it finished correctly, no matter whether you're using C89, C99 or C++. Hence the return from *main* should always be 0 if there are no errors.

    QuantumPete
    Not everyone programming C or C++ is on Unix. Besides, you're missing the point - I'm not saying that the return value shouldn't be 0 if there are no errors. On the contrary, I'm saying that the best way to make sure that happens is to use the default behavior, which does this automatically (in C99 and C++), instead of running the risk of doing it wrong by doing it by hand. In general, defaults are there to be used, and it's bad practice to write code which just does what the default does anyway, since it's possible to make a mistake, and even if not, it creates clutter which makes it harder to understand the rest of the code.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by robatino View Post
    In general, defaults are there to be used
    I'd rather be in control of my own code and I don't consider return statements "clutter". I'd also like to remind you that default copy constructors are *usually* a bad idea when you have pointers in your class! But I don't want to argue about it. We'll just disagree

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by QuantumPete View Post
    I'd rather be in control of my own code and I don't consider return statements "clutter". I'd also like to remind you that default copy constructors are *usually* a bad idea when you have pointers in your class! But I don't want to argue about it. We'll just disagree

    QuantumPete
    You ARE in control - you make the choice whether to use defaults, or not. The only time you're not in control is when the program exhibits undefined behavior - and in C99/C++ you don't need the explicit return to prevent that. In the case of default copy constructors, as with any other kind of default, you use them only when they do the right thing - if you need a deep copy, you don't. I was reading someone's page describing copy constructors and copy assignment operators and they recommended ALWAYS writing them out even if the defaults do the right thing. That's a horrible suggestion (especially if you need a shallow copy of a class with 50 data members). The right thing to do is to ask oneself whether the defaults do the right thing, and to write them out only if the defaults don't work properly.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by robatino View Post
    The right thing to do...
    ... in your opinion
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Ahem. The return values 0 and EXIT_SUCCESS are success, the return value EXIT_FAILURE is failure. That's the only thing C++ and C guarantee. Everything else is implementation-defined.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by QuantumPete View Post
    ... in your opinion
    You do realize that by choosing to program in a high-level language like C++, you're giving up control of the resulting assembly code to the compiler? Of course it's necessary for portability, but at least with a default return or a default copy constructor, you know exactly what they do, but you don't know exactly how the compiler will process your code.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by CornedBee View Post
    Ahem. The return values 0 and EXIT_SUCCESS are success, the return value EXIT_FAILURE is failure. That's the only thing C++ and C guarantee. Everything else is implementation-defined.
    Is EXIT_SUCCESS guaranteed to be equal to 0? I thought that a nonzero return value implied failure, the contrapositive of which would be that success implies a zero return value. I'm not sure about this, though.

    Edit: I found this: EXIT_SUCCESS guaranteed to always be zero?
    Last edited by robatino; 09-28-2007 at 03:59 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,724
    Quote Originally Posted by QuantumPete View Post
    ... in your opinion
    You are free to argue your opinion about whether one should provide a default constructor/copy constructor/copy assignment operator/destructor even if the compiler generated default is acceptable, or if return; versus no return at all is better for a function declared with void as the return type, but kindly do not spread misinformation about the global main() function requiring an explicit return 0 statement in C++.

    Is EXIT_SUCCESS guaranteed to be equal to 0? I thought that a nonzero return value implied failure, the contrapositive of which would be that success implies a zero return value. I'm not sure about this, though.
    With respect to the exit(int status) function from <cstdlib>, the C++ Standard states in section 18.3:
    If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
    Last edited by laserlight; 09-28-2007 at 04:00 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by laserlight View Post
    but kindly do not spread misinformation about the global main() function requiring an explicit return 0 statement in C++.
    I'm not. I just said that main *should* have a return statement. I don't think I said anywhere that it *must* have one.
    It's just a personal preference to be verbose about these things in code wherever possible, such as always including braces in if statements, defining all default constructors and so on.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,724
    I'm not. I just said that main *should* have a return statement. I don't think I said anywhere that it *must* have one.
    No, you stated that "the return from *main* should always be 0 if there are no errors", but that _lazycat_ is "missing a return 0 statement in (his/her) main() function". The latter implies that main() must have a return 0 statement, which is not the case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21