Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. x = getchar(); ????

x = getchar(); ????

Scheduled Pinned Locked Moved The Lounge
6 Posts 3 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Pls examine the code below, provided all the variable is initialized. Now at this point, i prompt for the use to type in one character, and the code will react depends on what key the user pressed. If the key is something outside 0-9, a-z, A-Z, - and _, it beeps. If Enter was press, the loop ended, if back space were press, it goes back one character. But it is not working. Can anyone tell me what's wrong? char Login[12]; int i; for (i=0; i<12; i++) { Login[i] = getchar(); typed = Login[i]; if (typed == 8 && i > 0) { x = x+i-1; printf("backspace pressed"); gotoxy(x,13);cprintf(" "); } if (typed == 13) // I tot 13 was the RETURN key??? { i = 12; printf("RETURN key pressed"); } if ((typed >= 0 && typed <= 12) || (typed >= 14 && typed <= 44)) { sound(1000); delay(500); nosound(); --i; } if ((typed >=46 && typed <= 47) || (typed >= 58 && typed <= 64)) { sound(1000); delay(500); nosound(); printf("second"); --i; } if ((typed >= 91 && typed <= 96) || (typed >= 123 && typed <= 255)) { sound(1000); delay(500); nosound(); --i; } }

    J 1 Reply Last reply
    0
    • L Lost User

      Pls examine the code below, provided all the variable is initialized. Now at this point, i prompt for the use to type in one character, and the code will react depends on what key the user pressed. If the key is something outside 0-9, a-z, A-Z, - and _, it beeps. If Enter was press, the loop ended, if back space were press, it goes back one character. But it is not working. Can anyone tell me what's wrong? char Login[12]; int i; for (i=0; i<12; i++) { Login[i] = getchar(); typed = Login[i]; if (typed == 8 && i > 0) { x = x+i-1; printf("backspace pressed"); gotoxy(x,13);cprintf(" "); } if (typed == 13) // I tot 13 was the RETURN key??? { i = 12; printf("RETURN key pressed"); } if ((typed >= 0 && typed <= 12) || (typed >= 14 && typed <= 44)) { sound(1000); delay(500); nosound(); --i; } if ((typed >=46 && typed <= 47) || (typed >= 58 && typed <= 64)) { sound(1000); delay(500); nosound(); printf("second"); --i; } if ((typed >= 91 && typed <= 96) || (typed >= 123 && typed <= 255)) { sound(1000); delay(500); nosound(); --i; } }

      J Offline
      J Offline
      JoeW 0
      wrote on last edited by
      #2

      A few points, forgive me if I'm mistaken: Where's your code to actually print the character... something like: if (typed >= (short)('A') && typed<=(short)('Z')) { gotoxy(x+i,13); cprintf(typed); } also, your backspace code needs to read something like: if (typed == 8 && i > 0) { gotoxy(x+i-1,13);cprintf(" "); i--; } Where 'x' is your initial starting point.

      L 1 Reply Last reply
      0
      • J JoeW 0

        A few points, forgive me if I'm mistaken: Where's your code to actually print the character... something like: if (typed >= (short)('A') && typed<=(short)('Z')) { gotoxy(x+i,13); cprintf(typed); } also, your backspace code needs to read something like: if (typed == 8 && i > 0) { gotoxy(x+i-1,13);cprintf(" "); i--; } Where 'x' is your initial starting point.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You are correct in both code you told me. But because i use getchar() function, whatever input from the keyboard will be echoed (show on the screen). So, my main point for this is not about showing use what they type, but is that i want to limit my user to only certain keys. Keys allow: a-z, A-Z, 0-9, - and _, backspace, Enter Everything outsite of that range will falg as an error and the program will beep... usage: backspace key : erase previously typed character enter key : tell the program that the imput is finish. So, get my point? Actually this portion of the program is for use at a login screen, where people use to type int her user name.

        G 1 Reply Last reply
        0
        • L Lost User

          You are correct in both code you told me. But because i use getchar() function, whatever input from the keyboard will be echoed (show on the screen). So, my main point for this is not about showing use what they type, but is that i want to limit my user to only certain keys. Keys allow: a-z, A-Z, 0-9, - and _, backspace, Enter Everything outsite of that range will falg as an error and the program will beep... usage: backspace key : erase previously typed character enter key : tell the program that the imput is finish. So, get my point? Actually this portion of the program is for use at a login screen, where people use to type int her user name.

          G Offline
          G Offline
          Ghazi H Wadi
          wrote on last edited by
          #4

          you are using getchar() The getchar() function will wait for a carriage return even if setbuf() is used to unbuffer stdin use getch instead include << conio.h >> to be able to use getch() BTW: I would not assign Login[i] = getch(); typed = Login[i]; I would assign it to typed and test it then assign it to Login[i] if it is ok so you don't have to go back try this


          char Login[12]; int i, x; char typed ; for (i=0; i<12; i++) { typed = getch(); if (typed >='1' &&typed <='9' || typed >='A' && typed <='Z' || typed >='a' && typed <='z' || typed == '_') { printf("*"); Login[i] = typed ; }else { i--; printf("\a"); } } printf("%s",Login);


          hope this might help cheers G Alfadhly

          L 2 Replies Last reply
          0
          • G Ghazi H Wadi

            you are using getchar() The getchar() function will wait for a carriage return even if setbuf() is used to unbuffer stdin use getch instead include << conio.h >> to be able to use getch() BTW: I would not assign Login[i] = getch(); typed = Login[i]; I would assign it to typed and test it then assign it to Login[i] if it is ok so you don't have to go back try this


            char Login[12]; int i, x; char typed ; for (i=0; i<12; i++) { typed = getch(); if (typed >='1' &&typed <='9' || typed >='A' && typed <='Z' || typed >='a' && typed <='z' || typed == '_') { printf("*"); Login[i] = typed ; }else { i--; printf("\a"); } } printf("%s",Login);


            hope this might help cheers G Alfadhly

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            BTW: I think the question should have been posted on the discussion (I hope I spelled it right) board cheers

            1 Reply Last reply
            0
            • G Ghazi H Wadi

              you are using getchar() The getchar() function will wait for a carriage return even if setbuf() is used to unbuffer stdin use getch instead include << conio.h >> to be able to use getch() BTW: I would not assign Login[i] = getch(); typed = Login[i]; I would assign it to typed and test it then assign it to Login[i] if it is ok so you don't have to go back try this


              char Login[12]; int i, x; char typed ; for (i=0; i<12; i++) { typed = getch(); if (typed >='1' &&typed <='9' || typed >='A' && typed <='Z' || typed >='a' && typed <='z' || typed == '_') { printf("*"); Login[i] = typed ; }else { i--; printf("\a"); } } printf("%s",Login);


              hope this might help cheers G Alfadhly

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Alfadhly, Thanks. You provide me with good help. Yes, i was a bit "in excess" of first assign the user input to Login[i]. getch() is much better. Thanks.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups