Thread: Encoding and coding text.

  1. #16
    Registered User
    Join Date
    May 2011
    Posts
    8
    is this ok... ?
    this have compiled only the first time i run..
    now i can't compile...


    Code:
    #include <stdio.h>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    void encoding(string s){
         for(int i=0; i<s.length(); i++)
         {if(((int)(s.at(i)))==32){s.at(i)=' ';}
         if(((((int)s.at(i)))>47) && ((((int)(s.at(i)))<58))){
                 switch((int)(s.at(i))){case 48: s.at(i)=(char)((int)(s.at(i)+9)); break;
         case 57: s.at(i)=(char)((int)(s.at(i)-9)); break;
         case 49: case 51: case 53: case 55:s.at(i)= (char)(((int)(s.at(i)+1))); break;
         case 50: case 52: case 54: case 56:s.at(i)= (char)(((int)(s.at(i)-1))); break;}}
         else{if(i%2!=0){if(((((int)(s.at(i)))>64) && (((int)(s.at(i)))<=88)) ||
          ((((int)(s.at(i)>96) && (((int)(s.at(i)))<=120))))){ s.at(i)= (char)(((int)(s.at(i)))+2); }if((((int)(s.at(i)))==89) ||
           (((int)(s.at(i)))==121) ||
            (((int)(s.at(i)))==90 ||
             (((int)(s.at(i))==122)))){s.at(i)= (char)(((int)(s.at(i)))-24);}}
             else{s.at(i)= (char)(s.at(i));}}}ofstream outFile;
             outFile.open("c://test.txt");outFile.clear();
             outFile <<s;outFile.close();}
    
             void decoding(){
                  string s;
             ifstream file;
             file.open("c://test.txt");
    
             while(getline(file,s)){
             cout<<s<<endl<<endl;}
             file.close();
             for(int i=0; i<s.length(); i++){
             if(((int)(s.at(i)))==32){
             cout<<" ";}
             if(((((int)(s.at(i)))>47) && (((int)(s.at(i)))<58))){switch((int)(s.at(i))){case 48: cout<<(char)((int)(s.at(i)+9)); break;
             case 57: cout<<(char)((int)(s.at(i)-9)); break;
             case 49: case 51: case 53: case 55:cout<<(char)(((int)(s.at(i)+1))); break;
             case 50: case 52: case 54: case 56:cout<<(char)(((int)(s.at(i)-1))); break;}}
             else{if(i%2!=0){if(((((int)(s.at(i)))>66) && (((int)(s.at(i)))<=90)) ||
              ((((int)(s.at(i)>98) && (((int)(s.at(i)))<=122))))){cout<<(char)(((int)(s.at(i)))-2);}if((((int)(s.at(i)))==65) ||
               (((int)(s.at(i)))==66) || (((int)(s.at(i)))==97 ||
                (((int)(s.at(i))==98)))) {cout<<(char)(((int)(s.at(i)))+24);}}else{cout<<(char)(s.at(i));}}}cout<<endl;}
    
    int main(int argc, char *argv[]){
        string s;
        ifstream file;
        file.open("string.txt");
        if(!file){cout<<"Can't open !"<<endl;
        return 1;}
        while(!file.eof()){ getline(file,s); }
        file.close();
        int letters=0;
        int numbers=0;
        int isigns=0;
        int empty=0;
        int ksigns=0;
        int ascii;
        char sign;
        for(int i=0; i<s.length(); i++){ if(((int)(s.at(i)))==32) empty++;
         else if (isalpha(s.at(i))) letters++;
          else if(isdigit(s.at(i))) numbers++;
           if(ispunct(s.at(i))) isigns++;
            else
             if((s.at(i)==' ')) empty++;
              else
               if( ((int)(s.at(i))==40) ||((int)(s.at(i))==41) ||
                ((int)(s.at(i))==91) || ((int)(s.at(i))==93) ||
                 ((int)(s.at(i))==123) || ((int)(s.at(i))==125)) kznaci++;}
        cout<<"The has letters: "<<letters<<",numbers: "<<numbers<<" empty spaces: "<<empty<<" punctual signs: "<<isigns<<" control signs: "<<ksigns<<endl<<"\n";
    int choise=-1;
     cout<<"Program for encoding and decoding ! \n";
     while(choise!=0){cout<<"Please enter: \n\n1. encoding \n2. Decoding \n0. Out \n\n";
     cin>>choise;
    switch(choise){case 1: encoding (s); break;case 2: decoding(); break;case 0:break;}}
    system("PAUSE");return EXIT_SUCCESS;}

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Tell you what ... reformat your code to it is at least readable and try again...

  3. #18
    Registered User
    Join Date
    May 2011
    Posts
    8
    the first time i compiled and run, everything was alright, now it shows noyting, can't compile..
    here is what i wrote:

    Code:
    #include <stdio.h>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    void encoding ( string s ) {
         for (int i=0; i<s.length(); i++)
    
         { if ((( int) ( s.at (i))) == 32 )  { s.at(i) = ' '; }
    
         if ((((( int ) s.at ( i ))) > 47 ) && (((( int )( s.at ( i ))) <58 )))  {
                 switch ((int)( s.at(i)))  { case 48: s.at(i) = ( char )((int)(s.at(i)+9 )); break;
    
                         case 57: s.at(i) = ( char )((int)( s.at (i) - 9 )); break;
    
                         case 49: case 51: case 53: case 55: s.at (i) = ( char )(((int)( s.at (i) + 1 ))); break;
    
                         case 50: case 52: case 54: case 56: s.at (i)= (char) (((int) (s.at (i)-1))); break;}  
                 }
    
                 else 
                { if (i%2!=0) {if(((((int) (s.at (i))) > 64) && (((int) (s.at (i))) <= 88)) ||
                  ((((int) (s.at (i) > 96) && (((int) (s.at (i) )) <= 120))))) { s.at (i)= (char) (((int) (s.at (i))) + 2); }
    
                 if  
                   ((((int)(s.at (i))) == 89) || (((int) (s.at (i))) == 121) || (((int )(s.at(i))) == 90 ||
                    (((int)(s.at(i))==122)))){s.at(i)= (char)(((int)(s.at(i)))-24);  }
                 }
    
             else
                { s.at (i) = (char) (s.at (i));}}}
             ofstream outFile;
             outFile.open ("c://test.txt");
             outFile.clear();
             outFile <<s;
             outFile.close();
                }
    
             void decoding() {
                  string s;
                  ifstream file;
                  file.open("c://test.txt");
    
             while(getline(file,s)){
             cout<<s<<endl<<endl;  }
             file.close();
             for(int i=0; i<s.length(); i++) {
             if(((int) (s.at (i))) == 32) {
             cout<<" ";  }
             if
              (((((int) (s.at (i))) > 47) && (((int) (s.at (i))) < 58)))  {
                    switch  ((int) (s.at (i)))
                            { case 48: cout<<(char) ((int) (s.at (i) + 9)); break;
                               case 57: cout<<(char) ((int) (s.at (i) - 9)); break;
                               case 49: case 51: case 53: case 55:cout<<(char) (((int) (s.at (i) +1))); break;
                               case 50: case 52: case 54: case 56:cout<<(char) (((int) (s.at (i) - 1))); break;}  }
                    else 
                            { if (i%2!=0) {if (((((int) (s.at (i))) > 66) && (((int) (s.at (i))) <= 90)) ||
                                                     ((((int)(s.at (i) > 98) && (((int) (s.at (i))) <= 122)))))
                            {cout<<(char) (((int) (s.at (i))) -2); }
    
                               if ((((int) (s.at (i))) == 65) || (((int)(s.at(i)))==66) || (((int) (s.at (i))) == 97 ||
                                   (((int) (s.at (i)) == 98)))) 
                            {cout<<(char) (((int) (s.at (i))) + 24);}  
                            }
                    else
                             {cout<<(char) (s.at (i)); } } }
                              cout<<endl;}
               
    
    int main(int argc, char *argv[]) {
        string s;
    
        ifstream file;
    
        file.open("string.txt");
    
        if(!file){cout<<"Can't open !"<<endl;
    
        return 1;}
    
        while(!file.eof()){ getline(file,s); }
    
        file.close();
    
        int letters=0;
    
        int numbers=0;
    
        int isigns=0;
    
        int empty=0;
    
        int ksigns=0;
    
        int ascii;
    
        char sign;
    
        for (int i=0; i<s.length(); i++)
             { if(((int) (s.at (i))) == 32) empty++;
                else 
                if (isalpha(s.at(i))) letters++;
                else if(isdigit(s.at(i))) numbers++;
                if(ispunct(s.at(i))) isigns++;
                else
                if((s.at(i)==' ')) empty++;
                else
                if 
                (((int) (s.at(i)) == 40) ||((int) (s.at (i)) == 41) ||
                 ((int) (s.at (i)) == 91) || ((int) (s.at(i)) == 93) ||
                 ((int) (s.at (i)) == 123) || ((int) (s.at(i)) == 125)) ksigns++;}
        cout<<"The has letters: "<<letters<<",numbers: "<<numbers<<" empty spaces: "<<empty<<" punctual signs: "<<isigns<<" control signs: "<<ksigns<<endl<<"\n";
    int choise=-1;
     cout<<"Program for encoding and decoding ! \n";
     while(choise!=0){cout<<"Please enter: \n\n1. encoding \n2. Decoding \n0. Out \n\n";
     cin>>choise;
    switch(choise){case 1: encoding (s); break;case 2: decoding(); break;case 0:break;}}
    system("PAUSE");return EXIT_SUCCESS;}
    Last edited by dzadze; 05-12-2011 at 10:19 AM.

  4. #19
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by CommonTater View Post
    Tell you what ... reformat your code to it is at least readable and try again...
    Nope, it's still a bunch of badly formatted code that only some decoders can read!
    Devoted my life to programming...

  5. #20
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    And you're still in the wrong forum. Maybe someone will be nice enough to move this to the C++ forum for you.

    Your formatting makes me want to stab my eyes out with a pen. My hand reached for the pen. Seriously.
    If you understand what you're doing, you're not learning anything.

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here are some reasons why your code sucks:
    • You're still posting C++ in a C forum. If you can't tell the difference, time to pick a new major/career.
    • You have inconsistent indentation. Pick one of the first 3 formats listed here: Indent style - Wikipedia, the free encyclopedia.
    • You put an arbitrary number of statements on each line. One statement per line only.
    • You use way too many magic numbers. At least use the character literal versions, like '0' instead of 48 and '9' instead of 57.
    • You have an obscene amount of casting and parentheses in your if conditions and elsewhere. Characters are basically integer types, no need to cast for a == comparison.

    You really need to correct all of those, then go post your code IN THE C++ FORUM.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Several passes of indent -kr -nut -ts2 foo.cpp later...
    Code:
    #include <stdio.h>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    void encoding(string s)
    {
        for (int i = 0; i < s.length(); i++) {
            if (((int) (s.at(i))) == 32) {
                s.at(i) = ' ';
            }
    
            if (((((int) s.at(i))) > 47) && ((((int) (s.at(i))) < 58))) {
                switch ((int) (s.at(i))) {
                case 48:
                    s.at(i) = (char) ((int) (s.at(i) + 9));
                    break;
    
                case 57:
                    s.at(i) = (char) ((int) (s.at(i) - 9));
                    break;
    
                case 49:
                case 51:
                case 53:
                case 55:
                    s.at(i) = (char) (((int) (s.at(i) + 1)));
                    break;
    
                case 50:
                case 52:
                case 54:
                case 56:
                    s.at(i) = (char) (((int) (s.at(i) - 1)));
                    break;
                }
            } else {
                if (i % 2 != 0) {
                    if (((((int) (s.at(i))) > 64) && (((int) (s.at(i))) <= 88))
                        || ((((int) (s.at(i) > 96)
                              && (((int) (s.at(i))) <= 120))))) {
                        s.at(i) = (char) (((int) (s.at(i))) + 2);
                    }
    
                    if ((((int) (s.at(i))) == 89) || (((int) (s.at(i))) == 121)
                        || (((int) (s.at(i))) == 90
                            || (((int) (s.at(i)) == 122)))) {
                        s.at(i) = (char) (((int) (s.at(i))) - 24);
                    }
                } else {
                    s.at(i) = (char) (s.at(i));
                }
            }
        }
        ofstream outFile;
        outFile.open("c://test.txt");
        outFile.clear();
        outFile << s;
        outFile.close();
    }
    
    void decoding()
    {
        string s;
        ifstream file;
        file.open("c://test.txt");
    
        while (getline(file, s)) {
            cout << s << endl << endl;
        }
        file.close();
    
        for (int i = 0; i < s.length(); i++) {
            if (((int) (s.at(i))) == 32) {
                cout << " ";
            }
            if (((((int) (s.at(i))) > 47) && (((int) (s.at(i))) < 58))) {
                switch ((int) (s.at(i))) {
                case 48:
                    cout << (char) ((int) (s.at(i) + 9));
                    break;
                case 57:
                    cout << (char) ((int) (s.at(i) - 9));
                    break;
                case 49:
                case 51:
                case 53:
                case 55:
                    cout << (char) (((int) (s.at(i) + 1)));
                    break;
                case 50:
                case 52:
                case 54:
                case 56:
                    cout << (char) (((int) (s.at(i) - 1)));
                    break;
                }
            } else {
                if (i % 2 != 0) {
                    if (((((int) (s.at(i))) > 66) && (((int) (s.at(i))) <= 90))
                        || ((((int) (s.at(i) > 98)
                              && (((int) (s.at(i))) <= 122))))) {
                        cout << (char) (((int) (s.at(i))) - 2);
                    }
    
                    if ((((int) (s.at(i))) == 65) || (((int) (s.at(i))) == 66)
                        || (((int) (s.at(i))) == 97
                            || (((int) (s.at(i)) == 98)))) {
                        cout << (char) (((int) (s.at(i))) + 24);
                    }
                } else {
                    cout << (char) (s.at(i));
                }
            }
        }
        cout << endl;
    }
    
    
    int main(int argc, char *argv[])
    {
        string s;
        ifstream file;
    
        file.open("string.txt");
        if (!file) {
            cout << "Can't open !" << endl;
            return 1;
        }
    
        while (!file.eof()) {
            getline(file, s);
        }
        file.close();
    
        int letters = 0;
        int numbers = 0;
        int isigns = 0;
        int empty = 0;
        int ksigns = 0;
        int ascii;
        char sign;
    
        for (int i = 0; i < s.length(); i++) {
            if (((int) (s.at(i))) == 32)
                empty++;
            else if (isalpha(s.at(i)))
                letters++;
            else if (isdigit(s.at(i)))
                numbers++;
            if (ispunct(s.at(i)))
                isigns++;
            else if ((s.at(i) == ' '))
                empty++;
            else if
                (((int) (s.at(i)) == 40) || ((int) (s.at(i)) == 41) ||
                 ((int) (s.at(i)) == 91) || ((int) (s.at(i)) == 93) ||
                 ((int) (s.at(i)) == 123) || ((int) (s.at(i)) == 125))
                ksigns++;
        }
        cout << "The has letters: " << letters << ",numbers: " << numbers <<
            " empty spaces: " << empty << " punctual signs: " << isigns <<
            " control signs: " << ksigns << endl << "\n";
        int choise = -1;
        cout << "Program for encoding and decoding ! \n";
        while (choise != 0) {
            cout <<
                "Please enter: \n\n1. encoding \n2. Decoding \n0. Out \n\n";
            cin >> choise;
            switch (choise) {
            case 1:
                encoding(s);
                break;
            case 2:
                decoding();
                break;
            case 0:
                break;
            }
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Oh, and moved to C++
    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.

  8. #23
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i am confused about what you want to do. do you just want to count the letters each time they occur or replace them and change them ? what language did you want to use ? what are "steam numbers" ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from a text file into an array and coding.
    By tombocollbo in forum C Programming
    Replies: 6
    Last Post: 12-09-2010, 01:25 PM
  2. Characters encoding
    By guillermoh in forum C Programming
    Replies: 4
    Last Post: 02-05-2008, 09:36 PM
  3. Character Encoding
    By bithub in forum Windows Programming
    Replies: 3
    Last Post: 01-07-2006, 01:39 AM
  4. URL Encoding
    By nickname_changed in forum C# Programming
    Replies: 2
    Last Post: 03-13-2004, 03:22 AM
  5. Encoding
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 06-20-2003, 10:17 AM