Domenic
Domenic

Reputation: 23

Questions About Double Pointer , structs and a not working scanf();

My Prof. gave me the assignment to write a warehouse management tool that can add, remove, search and show entries.

So I worked on this code for around 6 hours (don't laugh at me); now I have some questions:

  1. Why does my code skip printf("\n\nAdd another product ? (Y/N)");?
  2. Is it right that I use double pointer when I have a struct with 2 items? (someone yelled me that).
  3. Is there an easier way to access structs?
  4. Why does my code crash if I enter something else instead of 1,2,3,4,5? It should just print "Wrong Input".
int main() {

    struct managementtool {
        char artikel[200];
        int anzahl;
    };

    //wh = warehouse

        struct managementtool **wh = malloc(200 * sizeof(struct managementtool *));

        for (int i = 0; i < 200; i++) {
            wh[i] = malloc(sizeof(struct managementtool));
        }



    printf("Welcome to Warehouse Management 97\n\n\nWhat do you want to do ?\n");

    int exit = 0;
    int x,v;
    int f = 1;
    int i =  0;

    char ques;
    int end;
    do {
        printf("\n(1)Add article\n(2)Remove article.\n(3)Search entry.\n(4)Show stock.\n(5)Exit\n");
        scanf("%x",&x);

        switch (x) {
            case 1://add
                do {
                    printf("\nEnter the product name:  ");
                    scanf("%s", wh[f]->artikel);

                    printf("\nAmount of products:  ");
                    scanf("%i", &wh[f]->anzahl);

                    printf("\n\nAdd another product ? (Y/N)");
                    scanf("%c", &ques);

                    switch (ques) {
                        case 'Y':
                            v++;
                            f++;
                            break;
                        case 'N':
                            end = 1;
                            v = 0;
                            break;
                        default:
                            printf("Wrong entry\n");
                            break;
                    }
                } while (end != 1);
                if (v >= 2) {
                    printf("Product added successfully\n\n");
                }else {
                    printf("Products have been successfully added\n\n");
                }
                break;

            case 2://del
                printf("x ");
                scanf("%i", &v);
                int e;

                for (e=0;e<5;e++) {
                    printf("test");
                }

                break;
            case 3://search
                break;
            case 4://Spam-it
                while (i<f) {
                    printf("Product number %i\n", i);
                    puts(wh[i]->artikel);
                    printf("%d", wh[i]->anzahl);
                    printf("\n");
                    i++;
                }
                break;
            case 5://go away
                printf("Goodbye :)");
                exit=1;
                break;
            default://well
                printf("Wrong Input\n");
                break;
        }
    } while (exit==0);
}

Upvotes: 1

Views: 57

Answers (1)

GoRo3
GoRo3

Reputation: 161

Why does my code skip printf("\n\nAdd another product ? (Y/N)");?

Because your Input buffer is not empty after last insertion. Try to add

scanf("%i", &wh[f]->anzahl);

while ((getchar()) != '\n'); 

printf("\n\nAdd another product ? (Y/N)");

between lines and see what happen.

Is it right that I use double pointer when I have a struct with 2 items? (someone yelled me that).

Now you have pointer that points to table of structs. I don't see any logical reason for this solution in that case. You may do it like that:

struct managementtool* wh = (struct managementtool*) malloc(200 * sizeof(struct managementtool));

and you don't need first loop to populate this table. Remember to access member by . not ->; It will work too.

Is there an easier way to access structs?

You have array of structures. That's wy accessing them are so complicated. You may write or find some library for table or list abstraction but in raw C you have to do it in that way.

Why does my code crash if I enter something else instead of 1,2,3,4,5? It should just print "Wrong Input".

What do you mean at crash? What values do you input. I have tried different integer values and it worked with printing Wrong Input.

My other observations are:

  • You are using malloc so you need to add a header: #include <stdlib.h>
  • int f should be from 0 not 1, because arrays are iterated from 0 as the first element.

Upvotes: 1

Related Questions