C arrays - Incorrect response to wrong input?

I've got an assingment for a Programming course (beginner level) and the only thing I couldn't get right (according to the teacher at least) was how one of the functions handled wrong input. Atm it looks like this:

void Modify(int id[], char str[100][30], int kredit[], int count)
{
    system("cls");
    int Search_value;
    bool flag = false;
    printf("Enter ID to be modified (1-100):: ");
    scanf("%d", &Search_value);
    for (int i = 0; i < count; i++)
    {
        if (id[i] == Search_value)
        {
            printf("Old record\n");
            printf("ID :: %d \n", id[i]);
            printf("Name :: %s \n", str[i]);
            printf("Kredit :: %d\n", kredit[i]);
            printf("New record\n");
            printf("Name :: ");
            scanf("%s",str[i]);
            printf("Kredit :: ");
            scanf("%d", &kredit[i]);
            printf("New data saved\n");
            flag = true;
        }
    }
    if (flag == false)
    {
        printf("ID not found\n");
    }
    system("pause");

Part of the task describing how Modify should be done:

c) Modify data

Compiles and runs without any problem whatsoever. Or at least I haven't noticed. I can add more of the code if needed of course.

Upvotes: 0

Views: 59

Answers (1)

Paul92
Paul92

Reputation: 9062

Dealing with errors means thinking about things that could go wrong. After a quick look in your program,

What happens if I input a value that is out of the required range? What happens if count is larger than it should or negative? What happens if kredit is smaller than it should? what happens if a string from str does not have a NUL terminator?

And some more of these can be found. In general, you don't have to validate every argument at every function call (although it might help finding bugs during development), but you should definitely validate user input.

Upvotes: 2

Related Questions