-3

Currently I'm making a storage program, but it still print 1, when I already add an item. Can someone tell me what's wrong with this code?

Direction: Input Storage (1-10) enter 1 -> Add Item (enter anything) -> Back to main menu -> Show Item

#include <stdio.h>
#include <stdlib.h>

struct info1
{
    int quantity2;
    char name[60];
};

int main(void)
{
    start: ;
    struct info1 item1;
    struct info1 item2;
    struct info1 item3;
    struct info1 item4;
    struct info1 item5;
    struct info1 item6;
    struct info1 item7;
    struct info1 item8;
    struct info1 item9;
    struct info1 item10;

    int quantity1, mainmenu;
    item1.quantity2 = 0;
    item2.quantity2 = 0;
    item3.quantity2 = 0;
    item4.quantity2 = 0;
    item5.quantity2 = 0;
    item6.quantity2 = 0;
    item7.quantity2 = 0;
    item8.quantity2 = 0;
    item9.quantity2 = 0;
    item10.quantity2 = 0;

    printf("\n==Storage==");

    printf("\n\nInput Storage (1-10) : ");
    scanf("%d", &quantity1);

    printf("\nMain Menu\n1.Add Item\n2.Show Item\n3.Search Item\n4.Exit\n");
    scanf("%d", &mainmenu);

    if (quantity1 == 1)

    {

        if (mainmenu == 1)
        {
            printf("\nItem Name : ");
            scanf("%s", item1.name);
            fflush(stdin);
            printf("\nItem Quantity : ");
            scanf("%d", &item1.quantity2);

        }

        else if (mainmenu == 2)
        {
            printf("\n==Item List==");

            if (item1.quantity2 == 0)
            {
                printf("\n1. - ");
            }
            else if (item1.quantity2 > 0)
            {
                printf("\n1. %s    %d pcs", item1.name, item1.quantity2);
            }

        }

    }

    goto start;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
RyvH
  • 3
  • 3

2 Answers2

3

In essence your program has this structure

int main(void)
{
start: //doesn't need ';'
    struct info1 item1; //declared something
    int quantity1, mainmenu;
    //... and other setup

    //then some stuff you want to do over and over again

    goto start;
}

However, you want to keep using the values so don't want to go back to start. What you really want to do is go back to the menu (and then think about making exit work later).

So this structure:

int main(void)
{
    struct info1 item1; //declared something
    int quantity1, mainmenu;
    //... and other setup

start: //MOVED
    //then some stuff you want to do over and over again

    goto start;//like where we set stuff up we don't actually want to re-setup - oops!
}

i.e. in your code

int main(void)
{
    struct info1 item1;
    //... other stuff...
    printf("\n==Storage==");

    printf("\n\nInput Storage (1-10) : ");
    scanf("%d", &quantity1);
start: //<------- more a loop than a start

    printf("\nMain Menu\n1.Add Item\n2.Show Item\n3.Search Item\n4.Exit\n");
    scanf("%d", &mainmenu);

    //... menu code
       //...
   goto start;
}

There are those who say using goto is a bad idea. We could use a loop instead:

int main(void)
{
    struct info1 item1;
    // etc

    int quantity1=0, mainmenu=0; //,- initialise!
    item1.quantity2 = 0;
    // etc

    printf("\n==Storage==");

    printf("\n\nInput Storage (1-10) : ");
    scanf("%d", &quantity1);

    while (mainmenu != 4) //<- stop when the user says 4 for exit
    {
        printf("\nMain Menu\n1.Add Item\n2.Show Item\n3.Search Item\n4.Exit\n");
        scanf("%d", &mainmenu);

        if (quantity1 == 1)
        {
           //etc
        }
    }
}
doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

It prints 1 over and over again because goto makes it run from start (that is at the beginning of the program) so you are doing the same thing every time. There is no loop counter, you always work on the same item1 object.

Looks like you need to store a number of some values - use array instead of manually declaring 10 items. Also you need to loop over that array then.

I recommend you to read some decent book or take a look at some tutorials.

Community
  • 1
  • 1
Yuriy Ivaskevych
  • 956
  • 8
  • 18