question related to keypad

This is a discussion on question related to keypad within the C++ Programming forums, part of the General Programming Boards category; i have just started learning c++ programming for my project. my knowledge to c++ is nearly 0%. Please, i hope ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    11

    Unhappy question related to keypad

    i have just started learning c++ programming for my project. my knowledge to c++ is nearly 0%. Please, i hope someone will give me some pointers on the following question.

    i am using c++ to control a keypad. i have the code for my keypad to work well. that is whenever a key (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) is pressed, the digit pressed will display on the lcd screen. my problem now is that, i need to store 16 digits pressed into a variable perharps...

    for example, i pressed "2" "5" "6" "8" "1" "4".... my variable will store "256814..." which are the digits i pressed.

    can some kind souls please give me some pointers on how to store the digits pressed into some location..

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How are you getting the pressed keys? What type of variable are they stored in?
    Last edited by 7stud; 12-04-2005 at 09:26 AM.

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    So you are saying when you type in 123456 you are getting a number corresponding to 123,456 but you want a 1, a 2, a 3, a 4, a 5 and a 6?

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    i have a keypad... and my code is continously scanning which buttons is pressed. whenever a button is pressed if will write into the lcd.
    Code:
    int keypad() { 
      unint pio = _lr->piodata; // _lr->piodir = 0x000107fc;
      _lr->piodata = (pio | 0x000f0000) & 0xfffeffff;
      udelay(100);
      if (! (_lr->piodata & 0x01000000)) {
        while (!(_lr->piodata & 0x01000000));
        return '1';
      } 
      if (! (_lr->piodata & 0x02000000)) {
        while (!(_lr->piodata & 0x02000000));
        return '2';
      } 
      if (! (_lr->piodata & 0x04000000)) {
        while (!(_lr->piodata & 0x04000000));
        return '3';
    ...
    ...
    ...
    int keypadGUI() { int key = keypad();
      _lr->piodata &= (0xfff0ffff);
      if (key) {
        if (key == '#') {
          lcdwrite8(0,0x01); mdelay(1);
          showmode(*_mode);
          *passwd = 0; 
        } 
        else {
          uchar *ptr = passwd;
          while (*ptr) ptr++;
          lcdwrite8(1,key);
          *ptr++ = key;
          *ptr = 0; 
        }
    ...
    ...
    ...
    for every key pressed, it return the value and put into a int variable, then it is write into the lcd.


    i need to use the keypad to input 16 digits... and the 16 digits that are pressed, needed to be record down together..

    for example, firstly i pressed "2" then i pressed "5", then "7" and so on, the digits in pressed needed to be store into some place ( required answer : 257............) together

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can use what is called a stringstream. It allows you to treat a string like you would a file. For an ouput stringstream, you can write values to the string, just like you would to a file. Then, you can use the str() function to get a string representation of everything written to the string. Here is an example:
    Code:
    #include <iostream>
    #include <sstream>  //ostringstream, str()
    using namespace std;
    
    
    int main() {
       
    	ostringstream out;
    
    	int n = 2;
    	out<<n;
    
    	n = 5;
    	out<<n;
    
    	n = 7;
    	out<<n;
    
    	string str = out.str();
    
    	cout<<str<<endl;
    
    	return 0;
    }
    Or, do you want "257" to be an integer?
    Last edited by 7stud; 12-04-2005 at 06:29 PM.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    thanks a million.... i would like "257" to be an integer... i tried using the search engines to convert string into int... but i do not understand... could you/anyone kindly teach me how to make "257"(in the above example) into interger, please?

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    Code:
    #include <iostream>
    #include <sstream>  //ostringstream, str()
    using namespace std;
    
    int main() {
       
    	ostringstream out;
    
    	int n = 2;
    	out<<n;
    
    	n = 5;
    	out<<n;
    
    	n = 0;
    	out<<n;
    
    
    	string str = out.str();
    
    	cout<<str<<endl;
    
    	int ans;
    	ans = atof(str.c_str());	
    	cout<<ans<<endl;
    
    	return 0;
    }


    can this works?

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    i am really very noob.... the above works...but i encounter some 3 problems...

    i need to store 16bits in the variable.... unsigned long ans cannot reach 16bits.... can teach me how to make it to 16bits pretty please...

    the second problem is that.... if i input a 0 into the stringsteam as the first value, after i change it to int, it wont appear... how can i make the 0 still be the first value in the int...

    the third problem is..... the ans contains 16bits.... for example in the 16 bits... how do i swap the position of a few bits (i.e. bit 5 swap with bit 9, bit 10swap with bit 3)...

    please help me out... thanks a million in advance... T.T

    someone help!!~~ T.T
    Last edited by lovemagix; 12-05-2005 at 06:14 PM.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    help!!!~

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    i need to store 16bits in the variable.... unsigned long ans cannot reach 16bits.... can teach me how to make it to 16bits pretty please...
    Eh? An unsigned long should be 32 bits. An unsigned short would be 16.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    Quote Originally Posted by lovemagix
    i am really very noob.... the above works...but i encounter some 3 problems...

    i need to store 16bits in the variable.... unsigned long ans cannot reach 16bits.... can teach me how to make it to 16bits pretty please...

    the second problem is that.... if i input a 0 into the stringsteam as the first value, after i change it to int, it wont appear... how can i make the 0 still be the first value in the int...

    the third problem is..... the ans contains 16bits.... for example in the 16 bits... how do i swap the position of a few bits (i.e. bit 5 swap with bit 9, bit 10swap with bit 3)...

    please help me out... thanks a million in advance... T.T

    someone help!!~~ T.T
    oh... thanks.. i go back sch try the ulong again.. i tried yesterday i cant reach 32bits

    help me out with the other 2 problems pretty please... i am really a noob in c++ ..

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Eh? An unsigned long should be 32 bits. An unsigned short would be 16.

    Not necessarily... It depends on the platform. This doesn't appear to be a normal Win32 platform, so it is quite possible that a long isn't 32 bits.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    thanks a million.... i would like "257" to be an integer.
    Ok. stringstreams can help in that regard too. You can first read the numbers into a ostringstream like in the example code I posted above. Then you can use the str() function to obtain the resulting string, and the resulting string can be used to create an istringstream. An istringstream can be used just like an input file, and you can read data from it into an int variable. Here is the modified code:
    Code:
    #include <iostream>
    #include <sstream>  //ostringstream, istringstream, str()
    using namespace std;
    
    
    int main() {
       
    	ostringstream out;
    
    	int n = 2;
    	out<<n;
    
    	n = 5;
    	out<<n;
    
    	n = 7;
    	out<<n;
    
    	istringstream in(out.str());
    	int result = 0;
    	in>>result;
    	
    	cout<<result * 2<<endl;
    
    	return 0;
    }
    the second problem is that.... if i input a 0 into the stringsteam as the first value, after i change it to int, it wont appear... how can i make the 0 still be the first value in the int...
    I don't think you can. The only reason to use an int to store the numbers is if you need to use the int in a calculation, and in that case a leading 0 has no effect.
    Last edited by 7stud; 12-05-2005 at 10:40 PM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    >> Eh? An unsigned long should be 32 bits. An unsigned short would be 16.

    Not necessarily... It depends on the platform. This doesn't appear to be a normal Win32 platform, so it is quite possible that a long isn't 32 bits.
    I thought a long had to be at least 32 bits.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I thought a long had to be at least 32 bits.
    Yes, you are right. It doesn't have to be 32 bits, but it must be at least 32 bits, so my original point was not relevant.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 01:25 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 05:22 AM
  3. Yacc question related to %type
    By g_p in forum Tech Board
    Replies: 0
    Last Post: 05-31-2007, 10:55 AM
  4. Question related to getpid and getppid
    By g_p in forum C Programming
    Replies: 4
    Last Post: 12-18-2006, 11:35 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

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