Like Tree3Likes

Encoding and coding text.

This is a discussion on Encoding and coding text. within the C++ Programming forums, part of the General Programming Boards category; is this ok... ? this have compiled only the first time i run.. now i can't compile... Code: #include <stdio.h> ...

  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...
    Salem likes this.

  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
    1,625
    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,164
    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,496
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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" ?

Page 2 of 2 FirstFirst 12
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, 12:25 PM
  2. Characters encoding
    By guillermoh in forum C Programming
    Replies: 4
    Last Post: 02-05-2008, 08:36 PM
  3. Character Encoding
    By bithub in forum Windows Programming
    Replies: 3
    Last Post: 01-07-2006, 12:39 AM
  4. URL Encoding
    By nickname_changed in forum C# Programming
    Replies: 2
    Last Post: 03-13-2004, 02:22 AM
  5. Encoding
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 06-20-2003, 10:17 AM

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