CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: getch() problem

  1. #1
    Join Date
    Nov 2009
    Posts
    1

    getch() problem

    Hello!

    I have a problem with getch() in my program. When i press 2 at the first choice, it isn't get any charachter what i press on the keyboard.
    It is my code:
    #include <stdio.h>
    #include <conio.h>

    int css(){
    int hatter_allapot=0;
    int hatter_menu;
    printf("Milyen html elem stilusat akarod letrehozni?\n\nA sorrend nagyon fontos!!!\nPeldaul, ha a 2. pontot valasztod, akkor mar nem valaszthatod az elsot\n\n1. Az oldal hatterszine\n2. Az oldal hatterkepe\n3. Hatterkep ismetlesi modja\n4. Hatterkep pozicioja\
    \n5. Hatterkep kiiindulasi helye\n6. Egyik sem kell\n\n");
    while (hatter_allapot<6){
    hatter_menu=_getch();
    switch (hatter_menu){
    case 49:
    if (hatter_allapot<1){
    hatter_allapot=1;
    }
    break;
    case 50:
    if (hatter_allapot<2){
    hatter_allapot=2;
    }
    break;
    case 51:
    if (hatter_allapot<3){
    hatter_allapot=3;
    }
    break;
    case 52:
    if (hatter_allapot<4){
    hatter_allapot=4;
    }
    break;
    case 53:
    if (hatter_allapot<5){
    hatter_allapot=5;
    }
    break;
    case 54:
    hatter_allapot=6;
    break;
    default:
    break;
    }
    }
    return 0;
    }
    int uj(int lapszam){
    int biztos=1;
    lapszam=abs(lapszam);
    if (biztos){
    printf("\nBiztos, hogy %d lapot akarsz letrehozni?\n\nI - ha igen\nN - ha nem\n", lapszam);
    biztos=getch();
    switch(biztos){
    case 105:
    break;
    case 110:
    return -1;
    default:
    return -1;

    }
    }
    printf("\n%d lap lesz letrehozva\n\n", lapszam);
    return 0;
    }

    int main(){
    int menupont;
    int allapot=1;
    while (allapot){
    printf("1. Uj html lap letrehozasa\n2. Uj css stilus lap letrehozasa\n3. Kilepes\n\n");
    menupont=getch();
    switch(menupont){
    case 49:
    printf("H\xA0ny html f\xA0jlt kiv\xA0n l\x82trehozni?\n\n");
    if (scanf("%d",&menupont)==1){
    if (uj(menupont)==0)
    allapot=0;
    }
    else
    printf("\nNem megfelelo a bevitt karakter!!!\n\n");
    break;
    case 50:
    if (css()==0)
    allapot=0;
    break;
    case 51:
    return -1;
    default:
    printf("Nem megfelelo gombot nyomt\xA0l meg!!!\n\n");
    break;
    }
    if (allapot){
    printf("\n1. Ujraprobalkozas\n2. Kilepes\n\n");
    menupont=getch();
    switch(menupont){
    case 49:
    break;
    case 50:
    allapot=0;
    break;
    default:
    return -1;
    }
    }
    }
    return 0;
    }

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: getch() problem

    You'll probably get more response if you
    a) put [code] [/code] tags around your code
    b) properly indent your code
    c) change the text and variable names to english
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: getch() problem

    Code:
    hatter_menu=_getch();
    I do not know of any function called _getch.

    I do know of getch though. It reads 1 char for the input stream, and returns a single char.

    From your code, I see two problems:
    1 - hatter_menu is an int. While the cast works, it is proably not what you want: the char '3' could have a value of 107, for example.
    2 - getch reads only one char. If you write "42", then your getch will only read the '4', and then return the char '4' (which could be any integer value)

    You should use another input stream reader. I recommend operator>>.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured