Thread: Very Inefficient translator.

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Very Inefficient translator.

    I am trying to make a code translator where you would type in a certain number and it would spit out a certain letter. I'm a total newb so I'm guessing this is the most inefficient way of doing this.
    Here's the code. If there are bugs don't be surprised.
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    using namespace std;
    int main(){
        int translate;
        while(translate!=0){
            cout<< "translate?";
            cin>> translate;
        }    
        if (translate==1){
            cout<< "a";
        }else{    
             while(translate!=0){
            cout<< "translate?";
            cin>> translate;
        }    
        if (translate==2){
            cout<< "b";
        }else{    
             while(translate!=0){
            cout<< "translate?";
            cin>> translate;
        }  
        return 0;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    look up the ascii values for each letter, and then add the first value to their input-1, then cast it to a charater...


    try something along these lines
    Code:
    cin>>integer;
    integer=integer-1+65;  //65 is the ASCII value for 'A', and 97 is the ASCII value for 'a'
    character=static_cast<char>(integer);  //maybe a reinterpret_cast ?
    cout<<character;
    use this as a reference
    Last edited by major_small; 10-21-2004 at 08:10 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>integer=integer-1+65;
    >>use this as a reference
    Rather, use 'A'
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Hunter2
    >>integer=integer-1+65;
    >>use this as a reference
    Rather, use 'A'
    heh... yeah, what he said... or, if you really want to use the integer, you can just use 64... (-1+65)

    I just used the integer value so he could get the basic idea of the whole ASCII thing...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Mapping an numeric value or symbol to a letter is called developing a character set. The ASCII character set referenced by the previous posts is one of the standard character sets available, but you can create your own. This is one step in the process of encryption, because you translate a known value to one that makes sense only if you know the conversion/translation. if/else statements, switch statement, and maps are relatively simple mechanisms to accomplish this.

    if(num == 1)
    cout << 'a';
    else if (num == 3791)
    cout << 'b';
    else if (num = -53.21)
    cout << 'c';
    .
    .
    .
    else if(num == 5e367)
    cout << 'z';

    The translation/conversion can be whatever you want.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Could you do this with a sequence of numbers?
    For example, if you typed in 1,2,3 separated by commas it would spit out a,b,c. Think this is possible?
    AIM-PlatinumMjolnir
    [email protected]

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You mean something like this?

    Code:
    int numbers[3];
    int i;
    cout << "enter three numbers" << endl;
     
    for(i = 0; i < 3; ++i)
      cin >> numbers[i];
     
    for(i = 0; i < 3; ++i)
    { 
      if(numbers[i] == 0)
    	 cout << 'a';
      else if(numbers[i] == 1)
    	 cout << 'b';
      else 
    	 cout << "no char to correspond to the number entered" << endl;
    }
    Ya, I think that would be doable. They should be space or other whitespace delimited rather than comma delimited to use this syntax, but sure. If you have to have comma delimited numbers, then read the input as a string rather than individual char, parse out the numbers out of the string and put them in the array, run the translation and display as desired. You can have the output statement put commas between the letters if so desired, too.
    Last edited by elad; 10-22-2004 at 04:19 PM.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Cool.

    Here's another idea I got.
    You could declare all the variable in the beginning like.
    Code:
    int a=1
    int b=2
    int c=3
    //etc.
    Then you could change the users input, for now lets use "a", and make it display the value of "a". So when the user types "a" the program would see "a" and then display 1.

    Very confusing. Even to me.
    AIM-PlatinumMjolnir
    [email protected]

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>So when the user types "a" the program would see "a" and then display 1.
    Wouldn't work. The closest you'll get to this is:

    Code:
    int convert[256];
    
    convert['a'] = 1;
    convert['b'] = 2;
    //etc.
    
    OR
    
    std::map<char, int> convert;
    convert['a'] = 1;
    convert['b'] = 2;
    etc.
    The first method is more efficient, but the second is more expandable.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Hunter2
    >>So when the user types "a" the program would see "a" and then display 1.
    Wouldn't work. The closest you'll get to this is:
    OR, you could reverse what I already gave you:

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
        cout<<static_cast<int>('A')-64<<','<<static_cast<int>('a')-96;
        //you could also put the subtraction inside:
        //cout<<static_cast<int>('A'-64)<<','<<static_cast<int>('a'-96);
        cin.get();
        return 0;
    }
    the only thing with that is if you want 'A' and 'a' to have the same numeric value, you'll need to test to see if it's a capital letter first and then use the correct number to subtract from it...

    same thing the other way... if you want 'A' and 'a' both to be the same, you'll need to change the case internally and then cast it to an int...
    Last edited by major_small; 10-23-2004 at 02:46 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    OR, you could reverse what I already gave you:
    If you read xenoforce's latest post again, you'll see why I said what I said I was just saying that you can't simply declare a variable called 'a' and have the character 'a' in a string automatically map to the variable's value. To clarify, what I meant was, the closest you'll ever get to direct name of variable-to-value of variable mapping is with a hash table or map.

    the only thing with that is if you want 'A' and 'a' to have the same numeric value, you'll need to test to see if it's a capital letter first and then use the correct number to subtract from it...
    You could also use the tolower() or toupper() functions to make sure the case is what you want. I'm not sure if they're standard though.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Hunter2
    You could also use the tolower() or toupper() functions to make sure the case is what you want. I'm not sure if they're standard though.
    I'm pretty sure they are... that's what I meant, but I didn't want to say it exactly...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Nevermind what I said.I got a better way of explaining it now.
    Sorry
    What I really meant to say was that if the user typed in the name of the variable, the program would display the variable's value.
    I feel much better now.
    AIM-PlatinumMjolnir
    [email protected]

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>if the user typed in the name of the variable, the program would display the variable's value.

    Yes, in that case the closest you'll get is with a std::map or hash table. Kind of neat if you want to try writing a scripting engine, to be able to 'declare' and use variables on-the-fly
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Quote Originally Posted by Hunter2
    >>if the user typed in the name of the variable, the program would display the variable's value.

    Yes, in that case the closest you'll get is with a std::map or hash table. Kind of neat if you want to try writing a scripting engine, to be able to 'declare' and use variables on-the-fly

    If I knew what that meant, I would be like this guy but since I don't I am like both of these smilies.
    AIM-PlatinumMjolnir
    [email protected]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. language translator (having problems in tokenizing)
    By jumbo2410 in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 05:29 AM
  2. Power Translator
    By siavoshkc in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 09-02-2006, 02:37 PM
  3. Translator
    By Mitchell in forum Tech Board
    Replies: 2
    Last Post: 04-11-2006, 09:26 AM
  4. Translator
    By edshaft in forum C++ Programming
    Replies: 8
    Last Post: 05-09-2003, 10:27 AM
  5. Morse Code Translator!
    By Paro in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2002, 07:23 PM