Basil Afzal
Basil Afzal

Reputation: 25

Trouble with Arrays in C

I'm supposed to create a basic program creating an inventory using arrays but i'm having difficulty getting them to print one by one in a list after entering them in the loop, this is what i'm working with right now. The first loop is designed to finish once the user enters '0' as the barcode. Any help or guidance in the right direction is appreciated.

#include <stdio.h>

int main()
{
    int barcode[100], quantity[100], i;
    double price[100];

    printf("Grocery Store Inventory\n");
    printf("=======================\n");

    for(i=0;i<100;i++){
        printf("Barcode:");
        scanf("%d", &barcode[i]);

        if (barcode[100]==0){
            break;
        }

        printf("Price:");
        scanf("%lf", &price[i]);

        printf("Quantity:");
        scanf("%d", &quantity[i]);
    }

    printf("Goods in Stock\n");
    printf("==============\n\n");

    printf("Barcode    Price    Quantity    Value\n");
    printf("-------------------------------------\n");

    for(i=0;i<100;i++){
        printf("%d          %.2lf      %d\n", barcode[i], price[i], quantity[i]);
    }

    return 0;
}

Upvotes: 0

Views: 66

Answers (1)

Assem
Assem

Reputation: 12097

The break in the first loop is with the wrong condition:

if (barcode[100]==0){

break;

}

it should be:

if (barcode[i]==0) break;

You should also stop the second loop when you attend the zero value in barcode.

for(i=0;i<100 && barcode[i] !=0;i++){

Upvotes: 2

Related Questions