Beginner Needs a bit of help

This is a discussion on Beginner Needs a bit of help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, im slowly learning the basics and i thought id make some sort of a program that converts a ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Beginner Needs a bit of help

    Hey guys, im slowly learning the basics and i thought id make some sort of a program that converts a string to ascii then screws with the numbers and kind of "encodes" the string. My only problem is the decoding section of it. Im not trying to do anything serious cause i know youd alll laugh at me when you read the code, im just writing simple little programs so i can learn from trial and error, but im quite stumped now. Any help would be greatly appreciated thanks.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        int choice;
        cout<<"ENCRPTER PROGRAM"<<endl<<"Please choose which function you want to perform"<<endl;
        cout<<"1. Encrypt a message"<<endl<<"2. Decrypt a message"<<endl;
        cin>>choice;
        cin.ignore();
        
        if (choice==1)
    {
        char phrase[201];
        float codephrase[201];
        cout<<"Enter a phrase to be encoded: "<<endl;
        cin.getline(phrase,201,'\n');
        
        for (int x=0;x<201;x++)
        {
            codephrase[x] = (int)phrase[x];
    }
        cout<<"Succesful Encode";
        cin.get();
    //    cout<<"The sentence in ASCII: "<<endl;
    //        for (int i=0;i<201;i++)
    //    {
    //        if (codephrase[i]==0 )
    //        {
    //        break;
    //        }
    //        cout<<codephrase[i]<<" ";
    //    }
    //    cout<<endl;
        cout<<"The sentence encoded: "<<endl;
        for (int y=0;y<201;y++)
        {
            if (codephrase[y]==0)
            {
            break;
            }
            cout<<codephrase[y]/2*3/2*3/2*3<<" ";
    
        }
    
    }
        
    
    // This is the section i need a bit of help with
    
        else if (choice==2)
    {
             
        char phrase[201];
        float codephrase[201];
        float decodephrase[201];
        cout<<"Enter a phrase to be decoded: "<<endl;
        cin>>codephrase[201];
        cin.ignore();
        
        for (int x=0;x<201;x++)
        {
            decodephrase[x] = codephrase[x]/3*2/3*2/3*2;
    }
        for (int o=0;o<201;o++)
        {
            phrase[o] = (char)decodephrase[o];
    }
        
        cout<<"Succesful Decode";
        cin.get();
        cout<<"The sentence decoded: "<<endl;
        for (int y=0;y<201;y++)
        {
    //        if (codephrase[y]==0)
    //        {
    //        break;
    //        }
            cout<<phrase[y]<<" ";
    
        }
    
    }
        else
        {
                cout<<"That isnt an option";
    }
    
    
    cin.get();
    }
    once again thanks for any help in advance that you can offer me
    ~lac

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What!? Why would you want to convert a stringto ASCII? In most cases, it's already in ASCII. Secondly, what is with the (char) casts?
    Code:
    phrase[o] = (char)decodephrase[o];
    The resulting float number won't be one char. Make an 2-dimensional array of, say char[20]'s and use gcvt:
    Code:
    char phrase[20][201];
    ...
    for (int i=0;i<201;i++) //Matter of preference
        {
            gcvt(decodephrase[i],11,&phrase[i])   
        }
    Note that gcvt is not standard and is not supported by all compilers.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    ok you dont have to call me an idiot. ive only done about 7 lessons on this site and im using DevC++ so im only going on what i know. like i said, its how i learn best.
    also, could you explain what gcvt is and how it works? thanks.
    ~lac

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I didn't call you an idiot. But perhaps before trying a program like this you should build yourself up to it. Anyways, gcvt converts a floating point number into a char. The first argument is the number, the second is the size of the buffer (minus 8-9) and the third is the buffer itself.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    alright cool thanks. just one last question though.
    If i want to practice writing programs with the new stuff i learn after lessons. Is there a site or somewhere on here that can sort of tell you a program to write so you can try and do it yourself?
    thanks..

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Have you looked at the challenges?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    obviously not , but are there any other places like this that have, um, more to offer?
    Last edited by Lacanau; 11-10-2006 at 10:03 AM.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try those. They are usually a good challenge (well, duh) at first.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Or stick around the forums and check out other people's problems.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operations on Bit Patterns
    By mthemapc in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2008, 03:04 PM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 11:54 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  4. 32 bit v.s. 64 bit
    By xddxogm3 in forum Tech Board
    Replies: 4
    Last Post: 01-14-2005, 09:58 AM
  5. Replies: 7
    Last Post: 12-10-2004, 08:18 AM

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