Thread: Help displaying string letter by letter

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Angry Help displaying string letter by letter

    I am supposed to display a text string letter by letter, and it has to be incremented once by pressing any letter on the keyboard it will output the first letter in the string, then the second letter, then the third and so on.
    this is what I have so far, integer data is the ascii values in hex of the keys I am pressing. I'm trying to say for any value between the first key (nul) and the last key (del) the text string will be incremented by 1 so it would go T-e-x-t_s-t-r-i-n-g and so on. what am I doing wrong? I don't think this works.
    Code:
    int put_edit(volatile int data)
    {
        char text_string[] = "\nText String incremented\n> \0";
        int i;
        
        for( i = 0; text_string[i] !=0; ++i){
        if (data < 0x00 & data > 0x7F)
        {
             i++;
        }
        
        
        
        }
            
        
        return data;
        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (data < 0x00 & data > 0x7F)
    It's usual practice to use the boolean && operator in logical expressions, not the bitwise version.

    Also, something can't be less than zero and greater than something positive at the same time.

    Finally, shouldn't you be comparing text_string[i] with data in some way?
    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.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    Quote Originally Posted by Salem View Post
    > if (data < 0x00 & data > 0x7F)
    It's usual practice to use the boolean && operator in logical expressions, not the bitwise version.

    Also, something can't be less than zero and greater than something positive at the same time.

    Finally, shouldn't you be comparing text_string[i] with data in some way?
    I what you're saying, so i fixed my if statement, and am I supposed to compare text_string[i] with data? can I make data just = to text string[i]?
    Last edited by Robsta107; 03-01-2017 at 07:50 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    You can't do this in standard C as there is no kbhit() or similar function which scans the keyboard for key presses.
    The stdin functions like getchar are line-based, they wait for the user to compose an entire line and only give input after he presses return.

    Assuming you have a kbhit() it is easy;

    Code:
    char *text = "Text String Incremented".
    int i = 0;
    while(text[i] !=0)
    {
        if(kbhit())                    // test for keyboard press - function may have different name on your platform
        {
           getchar();                  // consume the character (may work differently on your platform
           printf("%c", text[i]);    // print out the letter
           i++;
        }
    }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-24-2012, 04:10 AM
  2. Change one letter from a string
    By gunitinug in forum C Programming
    Replies: 4
    Last Post: 12-01-2011, 04:05 AM
  3. Remove a letter from a string
    By StarOrbs in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2005, 03:03 PM
  4. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  5. checking if a string contains a letter
    By lonbgeach in forum C Programming
    Replies: 2
    Last Post: 04-07-2003, 03:40 PM

Tags for this Thread