簡體   English   中英

為什么我不能將指向一個字符的指針與單個字符進行比較?

[英]why cant I compare a pointer pointing to one character to a single character?

我的任務是制作銀行數據庫應用程序,並且對於菜單,必須允許用戶鍵入“部分或完整”選項名稱。 例如,對於“添加”,他們可以鍵入、添加、廣告或其他任何內容來調用添加 function。 我的問題是,當用戶輸入只是 ai 得到一個段錯誤但當我輸入 add 或 ad 時它工作得很好。 這是我的代碼,對不起,我知道名稱和值等到處都是,程序的 rest 位於我的文本編輯器中。 我懷疑問題出在: if (*firstCharA == 'a' || *firstCharA == 'A') 區域。 請救救我

#include <stdio.h>
#include <string.h>
#include "record.h"
#include "database.h"


int debug = 0;

void getaddress(int, char [], char []);

int main ()
{
    struct record * start = NULL;
    int on = 0;
    const char userInput[60];

    int addRec;
    const char add[7] = "add";

    int printAllRec;
    char printall[] = "printall";

    int findRec;
    char find[] = "find";

    int deleteRec;
    char delete[] = "delete";

    int quitPro;
    char quit[] = "quit";



    printf("\nWelcome to the ZaeBank database! Here you can get all of your bank record needs from adding a record to finding a record!\n");

    printf("To get started, type either add, printall or find and then hit enter!\n");

    while (on < 1)
    {

        printf("\nadd: add a bank record and store it in the ZaeBank database\n");
        printf("\nprintall: print all of the currcently existing bank records in the ZaeBank database\n");
        printf("\nfind: find a currently existing bank record in the ZaeBank database\n");
        printf("\ndelete: delete a currently existing bank record in the ZaeBank database\n");
        printf("\nquit: type 'quit' to quit the program\n");

        scanf("%s", &userInput);

        char *firstCharA;
        char *firstCharP;
        char *firstCharF;
        char *firstCharD;
        char *firstCharQ;

        firstCharA = strpbrk(userInput, add);
        firstCharP = strpbrk(userInput, printall);
        firstCharF = strpbrk(userInput, find);
        firstCharD = strpbrk(userInput, delete);
        firstCharQ = strpbrk(userInput, quit);

        addRec = strcmp(userInput, add);
        printAllRec = strcmp(userInput, printall);
        findRec = strcmp(userInput, find);
        deleteRec = strcmp(userInput, delete);
        quitPro = strcmp(userInput, quit);


        if (*firstCharA == 'a' || *firstCharA == 'A')
        {
            if (addRec == 0)
            {
                printf("1");
            }
            else if (addRec < 0)
            {
                printf("2");
            }

        }

我將如何解決這個問題: ctype.h包含有用的分類和轉換功能,可能會有所幫助。 isspace還包括換行符和制表符,這在處理用戶輸入時很有用。

#include <ctype.h> /* tolower isspace */

為了分離輸入和邏輯,使用某種表示 state 的數值很有用。 這通常是一個enum

enum action { NONE, ADD, PRINTALL, FIND, DELETE, QUIT };
static const char *action[] =
    { "none", "add", "printall", "find", "delete", "quit" };

這允許輕松測試解決方案。 也就是說,測試用戶輸入是否是任何操作的子字符串。

#include <assert.h>
...
    enum action act;
    act = parse_action(""), assert(act == NONE);
    act = parse_action("A"), assert(act == ADD);
    act = parse_action(" aD \n"), assert(act == ADD);
    act = parse_action("as"), assert(act == NONE);
    act = parse_action("f"), assert(act == FIND);
    act = parse_action("find"), assert(act == FIND);
    act = parse_action("foo"), assert(act == NONE);
    act = parse_action(" FiN \t\r\n"), assert(act == FIND);
...

確保測試空字符串。 一般來說,適合這種情況的數據結構是前綴樹 通過單獨檢查五個條目中的每一個,可以很容易地偽造它; 在這種情況下,每對值之間的最低公共子序列是空字符串。 如果一個人不希望它返回與它進行比較的第一件事,則必須對其進行特殊處理(這可能是理想的。)

  • strpbrk將從提供的任何字符中查找第一個字符。 搜索子字符串,也不關心傳遞給它的字符的順序,以及彼此之間的關系。 此外,它區分大小寫,因此首先使用小寫字符串調用strpbrk ,然后檢查結果是否為大寫是沒有意義的。

  • 首先將用戶輸入轉換為小寫可能更明智,例如在循環中對每個字符調用tolower 然后搜索匹配的子字符串而不是匹配的字符。 您可以使用strstr搜索子字符串。

  • 無論您使用哪個搜索 function,您都應該檢查結果以查看 function 是否找到任何東西。 strpbrkstrstr這樣的函數會返回 NULL 以防找不到任何東西。

  • 您的 while 循環始終為真,並且永遠持續下去。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM