函数调用不在函数中

时间:2016-11-15 17:32:50

标签: c

#include<stdio.h>


void clearKeyboard(void){
    while(getchar()!='\n');
}


void pause(void){
    printf("Press <ENTER> to continue...");
    clearKeyboard();
}


void printWelcome(void){
    printf ("---=== Grocery Inventory System ===---\n\n");
}



int getInt(void){
    int iVal;
    char charCheck='x';
    while (charCheck != '\n'){
            scanf ("%d%c",&iVal,&charCheck);
            if ( charCheck != '\n'){
                    clearKeyboard();
                    printf ("Invalid integer, please try again: ");
            }
    }
    return iVal;
}


int getYesOrNo(void){

    //list of variables declared and initialized
    char ch;
    int ret;
    ch = 0;
    ret = 0;

    //it will keep asking the user as long as they don't reply with y or n
    while(ch != 'Y' || ch != 'y' || ch != 'N' || ch != 'n')
    {
            scanf(" %c", &ch);
            clearKeyboard();

            if (ch == 'Y' || ch == 'y'){

                    ret = 1;
                    return ret;

            }
            else if (ch == 'N' || ch == 'n'){
                    ret = 0;
                    return ret;
            }
            //if they type other than y or n, it will print out this message
            else{
                    printf("Only (Y)es or (N)o are acceptable: ");
            }
    }
    return ret;


}




int getIntLimited(int lowerLimit, int upperLimit){
    int iVal;
    do{
            iVal = getInt();
            if (!(iVal >= lowerLimit && iVal <= upperLimit))
                    printf ("Invalid value, 0 <= value <= 7: ");
            }while (!(iVal >= lowerLimit && iVal <=upperLimit));
    return iVal;
}




int getMenuChoice(void){

    int SEL;
    int temp;
    printf("1- List all items\n");
    printf("2- Search by SKU\n");
    printf("3- Checkout an item\n");
    printf("4- Stock an item\n");
    printf("5- Add new item or update item\n");
    printf("6- delete item\n");
    printf("7- Search by name\n");
    printf("0- Exit program\n> ");
    scanf("%d", &SEL);
    if (SEL > 7){
            temp = getIntLimited(0,7);
    }
    return SEL;
}


void GrocInvSys(void){
    int SEL;
    int DONE = 0;
    SEL = 0;
    printWelcome();

    while (DONE == 0){
            SEL = getMenuChoice();
            clearKeyboard();
            if (SEL == 1){
                    printf("List Items!\n");
                    pause();
            }
            if (SEL == 2){
                    printf("Search Items!\n");
                    pause();
            }
            if (SEL == 3){
                    printf("Checkout Item!\n");
                    pause();
            }
            if (SEL == 4){
                    printf("Stock Item!\n");
                    pause();
            }
            if (SEL == 5){
                    printf("Add/Update Item!\n");
                    pause();
            }
            if (SEL == 6){
                    printf("Delete Item!\n");
                    pause();
            }
            if (SEL == 7){
                    printf("Search by name!\n");
                    pause();
            }
            if(SEL == 0){
                    printf("Exit the program? (Y)es/(N)o): ");
                    DONE = getYesOrNo();
            }
    }

}

int main(void){
    GrocInvSys();
    return 0;
}

对于此代码,它将显示getMenuChoice中的所有项目,每当使用键入每个项目的编号时,它将打印一条特殊消息。

一切正常,但每当我输入0或1或2或3或4或5或6或7以外的东西时,它应该说“无效值,0&lt; value&lt; 7:”。

所以我猜我的getIntl​​imited函数在getMenuChoice函数中不起作用。任何猜测为什么?

P.S。我输入了pause();对于每一个如果SEL ==声明,有什么方法可以让它变得更好吗?

1 个答案:

答案 0 :(得分:0)

主要问题是getMenuChoice()中的一些过多代码。它只需要打印菜单并返回getIntLimited(0,7)

的值
int getMenuChoice(void){
    printf("1- List all items\n");
    printf("2- Search by SKU\n");
    printf("3- Checkout an item\n");
    printf("4- Stock an item\n");
    printf("5- Add new item or update item\n");
    printf("6- delete item\n");
    printf("7- Search by name\n");
    printf("0- Exit program\n> ");
    return getIntLimited(0,7);
}

另一个问题是在clearkeyboard()调用getMenuChoice()后呼叫GrocInvSys()。这会读取更多输入到下一个换行符,但getInt()已经读取了终止整数的换行符。无需读取另一行,因为其内容只是被忽略。删除对clearkeyboard()的调用以解决该问题。