0

This is just a function from my program that is supposed to search for the string or integer the user enters in the struct array. What I'm trying to do is add an error message and the ability to try again when that entered string or integer is not found in the struct array. It searches just fine if you enter the correct string or integer but nothing happens if you don't. I'm trying to change that but can't find the solution.

I've tried for a while now with one of the cases in my switch statement but I need to do it for all three. But so far I've only tried on case 3.

search(struct items aItems[], int *num_items)
{
    int choice_of_search, b=0, found_word=0, search_num, i=0, j=0, k=0;
    char search_phrase[20]; struct items search[MAX];

    printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
    scanf("%d", &choice_of_search);

    while(choice_of_search < 1 || choice_of_search > 3)
    {
        printf("Wrong choice!\n");
        printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
        scanf("%d", &choice_of_search);
    }


    switch(choice_of_search)
    {
        case 1:
            printf("Item number?\n");
            scanf("%d", &search_num);
            for(i = 0; i < *num_items; i++)
            {
                if(search_num == aItems[i].itemnumber)
                {
                    printf("Item number found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                }
            }
            break;

        case 2:
            printf("Name?\n");
            scanf("%s", search_phrase);
            for(i = 0; i < *num_items; i++)
            {
                if(strstr(aItems[i].name, search_phrase))
                {
                    printf("Name found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                }
            }
            break;

        case 3:
            printf("Balance?\n");
            scanf("%d", &search_num);
            for(i = 0; i < *num_items; i++)
            {
                if(search_num == aItems[i].balance)
                {
                    printf("Balance found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                }
                else
                {
                    printf("Balance not found! Try again.\n");
                    printf("Balance?\n");
                    scanf("%d", &search_num);
                }
            }
            break;
    }


    while(b < found_word)
    {
        printf("Item number: %d Name: %s  Balance: %d\n", search[b].itemnumber, search[b].name, search[b].balance);
        b++;
    }
}
5
  • 2
    search is missing a return type. Commented Oct 8, 2017 at 9:50
  • @melpomene Thanks for the heads up. The problem I have though is with my cases, do you have any idea on what I should do ? A hint or tip or whatever really. Commented Oct 8, 2017 at 9:57
  • 1
    Please read minimal reproducible example. Commented Oct 8, 2017 at 10:00
  • for ease of readability and understanding: 1) follow the axiom: only one statement per line and (at most) one variable declaration per statement. 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line. Commented Oct 9, 2017 at 15:32
  • for us to properly debug your posted code, we would have to 'invent' the data definitions and the main() function. That is NOT what we are here to do. Please post code that is 'short' cleanly compiles, and still exhibits the problem. Commented Oct 9, 2017 at 15:34

1 Answer 1

3

Maybe this can help

int done;
...
...

    case 3:
        done = 0;
        while(1);
        {
            printf("Balance?\n");
            scanf("%d", &search_num);
            for(i = 0; i < *num_items; i++)
            {
                if(search_num == aItems[i].balance)
                {
                    printf("Balance found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                    done = 1;
                }
            }
            if (done) break;

            printf("Balance not found! Try again.\n");
        } 
        break;

But notice that the code isn't user friendly as it doesn't allow the user a way to stop the search without a match. So maybe you should consider adding a "Would you like try again" option.

A simple approach could be

            printf("Balance not found! Would like to try again?.\n");
            scanf(" %c", &some_char);
            if (some_char != 'y') break;
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry I'm still very new to programming. I'm not sure I understand that while loop and if statement you added. Do you mind explaining ?
@Camel while(1){...} means "loop forever". The only way to stop the loop is to have a break statement inside. Therefore the if (done) break
Hey man, thanks a lot. I've been trying for a loooong time to get this right. And thanks for the added tip about the user friendly thing. I was planning on adding that as soon as I could get this thing right.
Btw about that if(done) break; I'm used to if statements with conditions like if(variable == 1) for example. But in your if statement the condition is only the variable itself. Is that the same as if(done ==1) break; ?
Is there a way to make the scanf code safer to use ? Because if I enter more than one character or number. The program kind of bugs out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.