如何使getch()函数采用双字符而不是一个?

时间:2016-02-14 12:36:27

标签: c getch

我只想通过(获取字符)或任何其他方式放置多个字符,这是一个使用switch case条件的简单计算器程序,但不能选择具有多个输入{{1}的情况函数为例:10,11 ....

getch

3 个答案:

答案 0 :(得分:0)

Sub CreateSheetsFromAList()

    Dim MyCell As Range
    Dim MyRange As Range

    With Sheets("AllCities").Range("A2")
    Set MyRange = Sheets("AllCities").Range("A2")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))

    For Each MyCell In MyRange

        On Error Resume Next

        Sheets.ADD After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet

        If Err.Number = 1004 Then
            Debug.Print MyCell.Value & "already used as sheet name"
        End If
        On Error GoTo 0   
    Next MyCell
    End With  

End Sub

是你需要的。

更安全:

scanf("%d",&k);

答案 1 :(得分:0)

好吧,getch ()不能超过两个字符。您可以使用getline (),但我认为这不是您所需要的。你在读数字,而不是字符。

scanf ("%d", &k);

或使用@ DivinCodino答案中提到的scanf_s。此外,如果您想阅读字符,请使用上面提到的getline或使用scanf

scanf (" %c", &k);

并将k声明为

char k;

此外,您当前的程序无法正常工作,因为它试图使用getch读取整数,#include <stdio.h> #include <conio.h> void plus_func(); void menus_func(); void mul_func(); void div_func(); void modulus_func(); void shiftleft_func(); void shiftright_func(); void and_func(); void or_func(); void not_func(); void doubleand_func(); void doubleor_func(); void notequal_func(); void xor_func(); void main(void){ int x,y, res; int m; int op1; int op2; int op3; int i=0; int k; while(i==0){ printf("Enter any key to start the calculator function program or escape to exit it \n"); k=getch(); if(k!=0x1b) { printf("enter the first num or press escape to exit \n"); scanf ("%d", &x); if(x==0x1b) { break; } else { op1=x-48; } printf("enter the second num or press escape to exit \n"); scanf ("%d", &y); if(y==0x1b) { break; } else { op2=y-48; } printf("enter the type of the operation \n",m); scanf ("%d", &m); if(m==0x1b) { break; } else { op3=m-48; } switch(op3) { case 1: plus_func(op1,op2);break; case 2: menus_func(op1,op2);break; case 3: mul_func(op1,op2);break; case 4: div_func(op1,op2);break; case 5: modulus_func(op1,op2);break; case 6: shiftleft_func(op1,op2);break; case 7: shiftright_func(op1,op2);break; case 8: and_func(op1,op2);break; case 9: or_func(op1,op2);break; case 10: not_func(op1,op2);break; case 11: doubleand_func(op1,op2);break; case 12: doubleor_func(op1,op2);break; case 13: notequal_func(op1,op2);break; case 14: xor_func(op1,op2);break; default: printf("error"); break; } } else { break; } } } void plus_func(int a, int b) { int c; c=a+b; printf("the result is %d \n",c); } void menus_func(int a, int b) { int c; c=a-b; printf("the result is %d \n",c); } void mul_func(int a, int b) { int c; c=a*b; printf("the result is %d \n",c); } void div_func(int a, int b) { int c; c=a/b; printf("the result is %d \n",c); } void modulus_func(int a, int b) { int c; c=a%b; printf("the result is %d \n",c); } void shiftleft_func(int a, int b){ int c; c=a<<b; printf("the result is %d \n",c); } void shiftright_func(int a, int b) { int c; c=a>>b; printf("the result is %d \n",c); } void and_func(int a, int b) { int c; c=a&b; printf("the result is %d \n",c); } void or_func(int a, int b) { int c; c=a|b; printf("the result is %d \n",c); } void not_func(int a, int b) { int c; c=a+b; c=~c; printf("the result is %d \n",c); } void doubleand_func(int a, int b) { int c; c=a&&b; printf("the result is %d \n",c); } void doubleor_func(int a, int b) { int c; c=a||b; printf("the result is %d \n",c); } void notequal_func(int a, int b) { int c; c=a+b; c=!c; printf("the result is %d \n",c); } void xor_func(int a, int b) { int c; c=a^b; printf("the result is %d \n",c); } 仅用于读取字符。

进行上述更改后,代码为:

getch

此代码仍然无效,但除了将scanf更改为java -version之外,我没有做任何更改。

答案 2 :(得分:0)

此函数将捕获多个数字并将它们连接成一个值。为了节省空间,只有plus_func包含在代码和开关中。该函数接受数字位数,符号标志和指向该值的指针。它返回一个整数,表示已按下ESC。

这样,每个输入都是固定宽度。要输入3,必须输入03

#include <stdio.h>
#include <conio.h>

int getint ( unsigned int digits, int sign, int *value) {
    int ch = 0;
    unsigned int first = digits;

    *value = 0;//set to zero

    while ( digits) {
        while ( ( ch = getch()) < '0' || ch > '9') {
            if ( ch == '-' && sign == 1 && first == digits) {// allow for negative numbers
                sign = -1;
                putchar ( '-');
            }
            if ( ch == 0x1b) {
                return 1;
            }
        }
        putchar ( ch);
        *value *= 10;
        *value += ch - '0';//concatenate the digits
        digits--;
    }
    if ( sign) {
        *value *= sign;
    }
    return 0;
}

void plus_func(int a, int b)
{
    int c = 0;
    c = a + b;
    printf ( "\nthe result is %d\n", c);
}

int main()
{
    int i = 0;
    int op1 = 0;
    int op2 = 0;
    int op3 = 0;
    unsigned int digits = 2;

    do {
        printf("\nenter the first %u digit num or press escape to exit \n", digits);
        if ( getint ( digits, 1, &op1)) {
            break;
        }

        printf("\nenter the second %u digit num or press escape to exit \n", digits);
        if ( getint ( digits, 1, &op2)) {
            break;
        }

        printf("\nenter the type of the operation from 01 to 14\n");
        if ( getint ( digits, 0, &op3)) {
            break;
        }

        switch ( op3) {
            case 1:
                plus_func ( op1, op2);
                break;

            default:
                printf ( "error\n");
                break;
        }

    } while ( i == 0);
    return 0;
}