my code is messed up says the compiler

This is a discussion on my code is messed up says the compiler within the C++ Programming forums, part of the General Programming Boards category; here it is Code: #include <iostream.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <dos.h> #include <string.h> #define PROG_NAME ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    16

    my code is messed up says the compiler

    here it is

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    #include <dos.h>
    #include <string.h>
    
    #define PROG_NAME "se"
    
    //variables
    char message[1000];
    //functions
    void encypt ();
    void decrypt ();
    char EncryptChar ();
    void EncryptString ();
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::code starts here
    
    char EncryptChar(char ch)
    {
        if (ch == 'Z')
        {
          return 'A';
        }
        if (ch == 'z')
        {
          return 'a';
        }
        return ch + 1;
    }
    
    void EncryptString(char *str)
    {
        while (*str != '\0')
        {
           *str = EncryptChar(*str++);
        }
    }
    
    void encrypt()
    {
    cout<<"Enter Message to Encrypt:"<<endl;
    cin.getline(message, 1000, '\n');
    EncryptString(message);
    }
    
    void decrypt()
    {
    cout<<"Error"<<endl;
    }
    
    int main(int argc, char* argv[])
    {
    //Command line arguements =)
    if(argc != 3)
      {
        cout<<"Error, Bad Input."; 
        return 0;   
      }
    
    if(argv[2] == '-')
    {
    
    if((argv[3] == 'e') || (argv[3] == 'E'))
    {
    system("cls");
    encrypt();
    }
    else if((argv[3] == 'd') || (argv[3] == 'D'))
    {
    system("cls");
    decrypt();
    }
    else
    {
    cout<<"Error Bad Input, Read the ReadMe File"<<endl;
    return 0;
    }
    
    }
    else
    {
    cout<<"Error Bad Input, Read the ReadMe File"<<endl;
    return 0;
    }
    
    
    return 0;
    }
    im useing bcc 5.5 it says:
    69: Cannot convert 'char' to char *' in function main(int, char * *)
    72:same thing as above
    72:Same thing
    77:same thing
    77:same thing
    • 0927
    • a.k.a 0 9 two 7

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I got 18 errors total.

    >// ::::::::::::::::::::::::::::::::::::::::::::::::::
    ::::::::code starts here

    This is two lines, a C++ comment only works for one line but that may have been caused by the board formatting.

    >if(argv[2] == '-')
    This and other lines similar to this are your problems. argv is an array of pointers to strings, when you try to test a const string and a char for equality the compiler chokes. Also, anything not separated by whitespace will be considered a single string in argv so assuming your command line input is

    c:\>test.exe -e

    The program will take test.exe as argv[0] and -e as argv[1]. So you could copy argv[1] into an array and then parse it, or use strcmp( argv[1], "-e" ); to test for equality.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    16

    ok fixed it a little

    i fixed my earlier problems now it is this

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    #include <dos.h>
    #include <string.h>
    
    #define PROG_NAME "se"
    
    //variables
    char message[1000];
    //functions
    char EncryptChar (char ch);
    void EncryptString (char *str);
    void encypt ();
    void decrypt ();
    
    
    char EncryptChar(char ch)
    {
        if (ch == 'Z')
        {
          return 'A';
        }
        if (ch == 'z')
        {
          return 'a';
        }
        return ch + 1;
    }
    
    void EncryptString(char *str)
    {
        while (*str != '\0')
        {
           *str = EncryptChar(*str++);
        }
    }
    
    void encrypt()
    {
    cout<<"Enter Message to Encrypt:"<<endl;
    cin.getline(message, 1000, '\n'); 
    EncryptString(message);
    }
    
    void decrypt()
    {
    cout<<"Error"<<endl;
    }
    
    int main(int argc, char* argv[])
    {
    
    if(argc == 2)
    {
    
    if(!strcmp(argv[1], "-e"))
    {
    encrypt();
    }
    else
    {
    cout<<"Error, Bad Input 2, se -e,-d"<<endl;
    return 0;
    }
    
    }
    else
    {
    cout<<"Error, Bad Input, se -e or -d";
    return 0;
    }
    
    return 0;
    }
    it compiles and runs but when i call
    se -e
    it asks for the string to encypt i enter it then a message box pops up and says it has preformed and illegal action
    • 0927
    • a.k.a 0 9 two 7

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    I don't see your error but
    char EncryptChar(char ch)
    {
    if (ch == 'Z')
    {
    return 'A';
    }
    if (ch == 'z')
    {
    return 'a';
    }
    return ch + 1;
    }

    would be better as

    char EncryptChar(char ch)
    {
    if (ch == 'Z')
    {
    return 'A';
    }
    else if (ch == 'z')
    {
    return 'a';
    }
    else return ch + 1;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Please excuse my laziness, your code worked for me when I broke it down...so I just rewrote it. :P
    Code:
    #include <iostream>
    using namespace std;
    
    void cryptString(char *str, const char crypt);
    char cryptChar(char ch, const char crypt);
    
    /* argv should have three parameters: The file name,
    ** the line argument to determine encrypt or decrypt,
    ** and the line argument to do the opposite of the 
    ** previous argument, just to make sure it worked.
    **
    ** Line arguments are tested as e and d, but change
    ** the test and you can change the argument.
    */
    int main(int argc, char *argv[])
    {   char message[1000] = {0};
    	
        if( argc != 3 )
            // Never do this, always let the user know
            return EXIT_FAILURE;
    
        cout << "Enter a message to crypt: " << flush;
        cin.getline( message, 1000, '\n' );
    
        // Before
        cout << message << endl;
        cryptString(message, *argv[1]);
        // After
        cout << message << endl;
        cryptString(message, *argv[2]);
        // Back to original
        cout << message << endl;
    
        getchar();
    
        return EXIT_SUCCESS;
    }
    
    void cryptString(char *str, const char crypt)
    {
        while (*str != '\0') {
            *str = cryptChar(*str, crypt);
            str++;
        }
    }
    
    char cryptChar(char ch, const char crypt)
    {
        /* I used a simple rotate 13 encryption
        ** but you can use whatever you want
        */
        if( crypt == 'e' )
            ch += 13;
        else if( crypt == 'd' )
            ch -= 13;
        return ch;
    }
    Your error sounds like it may be implementation defined.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    > I don't see your error but ...
    >would be better as

    I wrote that funciton! How dare you critque my work!
    Since the function extis at the return statement if the condition is true, it doesnt make a difference

    also, your suggestion always flags a warning on my compiler saying not all possible execution lines return a value, and my god I hate warnings.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    the error in your code is here:

    >>>
    void EncryptString(char *str)
    {
    while (*str != '\0')
    {
    *str = EncryptChar(*str++);
    }
    }
    <<<

    since '=' is evaluated right to left, *str++ advances the pointer before the target value is set in the correct spot, so the NULL byte is overwritten. But on some compilers, a char array is initialized to zero automatically, so that would explain why the code may seem to work on some compilers and not others.

    Here is the fix:
    Code:
    void EncryptString(char *str)
    {
        while (*str != '\0')
        {
           *str++ = EncryptChar(*str);
        }
    }
    notice the incrementation happens after the equation has been evaluated

  8. #8
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    int argc, char* argv[])

    this isnt really necessary - you can just call int main (void)
    Monday - what a way to spend a seventh of your life

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    16

    im redoing it

    im redoing it to invovle a key and useing files instead of input ought to be pretty nice thanks for the help fixing those problems.oh ya and about file i/o if i just use
    ifstream(file.txt)
    does it look in the directory the programs in?
    • 0927
    • a.k.a 0 9 two 7

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    yep

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    this line is confusing...

    if(!strcmp(argv[1], "-e"))
    {
    encrypt();
    }


    strcmp can return 0 less than 0(usually -1) or greater than 0 (usually 1) !0 !-1 !1??????

    why not...

    if(strcmp(argv[1], "-e") != 0 )
    {
    encrypt();
    }

    instead...
    zMan

  12. #12
    Unregistered
    Guest
    You're correct,

    if(!strcmp(argv[1], "-e"))

    is potentially confusing but the more explicit form would be:

    if(strcmp(argv[1], "-e") == 0)

    and not

    if(strcmp(argv[1], "-e") != 0)


    What the original code is trying to say, I believe, is if the second command line argument is the flag -e then you should do the encryption. Since argv[1], if it exists, and -e are strings you would routinely use strcmp() to compare them. If they are the same string then strcmp() will return 0. However, 0 is interpreted as false and any non-zero value is interpreted as true, so the original line will resolve to if(!0) which means if(true). I like the more explicit form personally, but to save keystrokes, the form as presented is commonly used, so it pays to get used to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Compiler or code error?
    By Hunter2 in forum Windows Programming
    Replies: 2
    Last Post: 08-03-2003, 05:59 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

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