• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

<noob> misc C++ help

Status
Not open for further replies.
Level 5
Joined
Sep 22, 2012
Messages
90
hello peepz,

I have a problem with a C++ problem: "build a program that would print the letters on a cellphone keypad (oldschool 3x4 nokia3310 stuff) and vice versa".

So far I've only made the numkeys to charkeys: 2 = "abc".
>I'm trying to apply the trick they call "fake arrays" but the thing is when it comes to key 7 and 9 the chracters there are 4: 7 = "pqrs".I've solved this and I'm certain that my option numtochar>>keys are working, but how should do it the other way around?

Here's my code:

JASS:
#include <iostream>
using namespace std;
int numkey;
char operation,charkey;
char key_elem[]="abcdefghijklmnopqrstuvwxyz";
//to_keys        22233344455566677778889999*  --27+nullchar|0to25
//to_nums_placer  0  1  2  3  4   5  6   7-*
//to_nums_set    -3--3--3--3--3---4--3---4-*
/******************************
*cellkeys
*valid only to those phones with a 3x4 key layout
******************************/
int main()
{
    cout<<"---number keys---\n";
    cout<<"Supports only lower case characters and numbers from cellphone keys\n";
    while (operation !='x')
    {
          cout<<"\n\n\n\n\n\n----------------------\n";
          cout<<"1> numkeys to chars\n";
          cout<<"2> charkeys to numkeys\n";
          cout<<"x> to EXIT\n";
          cout<<"----------------------\n";
          cout<<"enter operation:";
          cin>>operation;
          switch (operation){
                 //case1
                 case '1':
                      {
                          
                           cout << "enter numkey:";
                           cin >> numkey;
                          if (numkey>1&&numkey<10){
                           numkey=numkey-2;
                           int iter=0,placer=3,ender=3;
                           iter=numkey*placer;
                           //getexactinit
                           if (numkey==5)//incase7
                           {ender=4;}
                           else if (numkey==7)//incase9
                           {iter=iter+1;ender=ender+1;}
                           else if(numkey==6){iter=iter+1;}//incase8
                           //--------------------------------
                           //cout<<"placer: "<<placer<<endl;
                           //cout<<"ender: "<<ender<<endl;
                           //cout<<"iter: "<<iter<<endl;
                           //--------------------------------
                           //getexactend
                           ender=iter+ender;
                           //printelementsfromto
                           for(;iter<ender;cout<<"element: "<<key_elem[iter]<<"\n",iter++)
                           {}
                          }
                          else{
                               cout<<"no chars defined for that numkey"<<endl;}
                          break;                             
                      }
                 //endcase1
                 //case2-notworking:(((-
                 case '2':
                      {
                          cout << "enter your charkey:";
                          cin >>charkey;
                          int x=0,y=0,z=0,k=0,sub_size=3,temp_key;
                          //findthatkey
                          for(;x<26;x++)
                          {
                              if(charkey==key_elem[x]){break;}
                          }
                          //skipcounttogetcellkey
                          for(; y<8 ;y++)
                          {
                                //getsubkeys
                                for( z=(z*3); ; ){}
                          }
                      }
                 //endcase2
                 //default
                 default:
                         {
                         if (operation=='x'){cout<<"exiting....\n";}
                         else {cout<<"\n****invalid operation!!\n";}
                         }
          } 
    }
    system("PAUSE");
    return 0;
}

I do hope someone could help but not too overboard. I know I'm going far off the site's regulation but i ran out of options since other forums are taking too long to respond.
 
Last edited by a moderator:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,207
cout << "enter numkey:";
cin >> numkey;
if (numkey>1&&numkey<10){
numkey=numkey-2;
int iter=0,placer=3,ender=3;
iter=numkey*placer;
//getexactinit
if (numkey==5)//incase7
{ender=4;}
else if (numkey==7)//incase9
{iter=iter+1;ender=ender+1;}
else if(numkey==6){iter=iter+1;}//incase8
//--------------------------------
//cout<<"placer: "<<placer<<endl;
//cout<<"ender: "<<ender<<endl;
//cout<<"iter: "<<iter<<endl;
//--------------------------------
//getexactend
ender=iter+ender;
//printelementsfromto
for(;iter<ender;cout<<"element: "<<key_elem[iter]<<"\n",iter++)
{}
}
else{
cout<<"no chars defined for that numkey"<<endl;}
break;
}
//endcase1

You seem to be comparing a character (ascii?) to a static number which will not reflect the reality as '0' to '9' are located at offset 0x30 to 0x39. The simplest solution is to normalize this around '0' which is itself a number.

cin >> numkey;
numkey-= '0';

You can now test if the key is a number in the appropiate range like you do already as numkey has been remaped so '0' is 0 and '9' is 9 instead of their usual ascii numbers.

As for the mapping of keys to characters. The most efficient solution is to use an array of strings (char *). All strings are null terminated (char 0) so you can treat them as finite lengthed arrays (as you know when they end).
const char *keyelements[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

keyelements[num] will evaluate to a pointer to a string containing the letters for character num (where num is key-2) that should support direct printing via appropiate methods. This saves any need for itteration and the special cases of 7 and 9 making the code smaller and program more efficient.

For the reverse direction you use an array mapping the alphabet to their keys.

const char alpha2key[] = {'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','7','7','7','8','8','8','9','9','9','9'};

using alpha2key[inchar-'a'] where inchar is an input character will thus return a character representing which button was pressed (can be displayed directly without futher processing via printf); Remember to check if inchar is lowercase and an alphabet character to prevent a segementation fault or garbage output.
 
Level 5
Joined
Sep 22, 2012
Messages
90
My numkey var is an integer, the thing about converting it from "numkey to charkey" is that the keys 7 and 9 contains 4 characters and so i had to do some arithmetics for the locals on switch case 1 in order to trick the size 4 keys(7 and 9). I also have questions regarding what your said:

1>>
You seem to be comparing a character (ascii?) to a static number which will not reflect the reality as '0' to '9' are located at offset 0x30 to 0x39. The simplest solution is to normalize this around '0' which is itself a number.
What's a static? I think i'm not comparing chars on my switch case1 because my numkey variable is an int.

@dr super good:
1)
is it possible to narrow down the index of the character i'm looking for into the numpad key? I tried doing for sets of 3 and skipcounting by 8 using nested for loops and it seemed to work fine but the thing is 7 and 9 consists of 4 chars. I'm trying to come up with a solution with less arrays as possible.

2) what does " inchar-'a' " code do?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,207
What's a static?
Sorry I Java language "mispoke" (LOLOLOL). I meant a constant number (usually embeded in the actual instruction itself once compiled).

I think i'm not comparing chars on my switch case1 because my numkey variable is an int.
In most compilers an int is a 32 bit word while a character is an 8 bit word. You are reading a raw character which is input as text from stdin. '0' is not 0 but actually 0x30 as mentioned earlier so your condition makes no sense as it is. Since '0' to '9' are sequential you can normalize like I said to convert them into an integer equivelent to their meaning.


I'm trying to come up with a solution with less arrays as possible.
Arrays are highly efficient structures which often compile direction into instructions. If anything you want to use as many arrays as possible and as few conditional flow control (cases/ if / loops) as possible. Arrays can be pipelined while flow control cannot always.

2) what does " inchar-'a' " code do?
It suptracts the value of inchar by the value of 'a'. 'a' is an Ascii character so converts into a number.

Everything in C is numbers as that is what computers do. You typecast it to clasify the type of number (like HANDLE) but still the computer represents them as numbers as that is all computers can do.
 
Level 5
Joined
Sep 22, 2012
Messages
90
so one thing @dr supergood, Does it mean that a program flow often/sometimes depend on the structure of your code? i mean like if dozens of ifelse/switches are made then it is poorly efficient? I'll try your suggestion sir, tnx for helping. GREATLY appreciated :)
 
Status
Not open for further replies.
Top