Functions and Structures Inventory Program

I am writing a program that reads a file that has inventory on it then the user selects the inventory and quantity they want and program should print out a nice bill. I been struggling with the bill, if anyone can help that would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define Max 20             //variables are initialized
using namespace std;
FILE *infile;

struct Product{             //structure with name, price, number of objects in inventory
    char name[51];
    float price;
    int number;
};

struct Bill{
       Product inventory[];
       int quantity;
       char response;
       float Total;
};  

void initInventory(struct Product *inventory);  //functions are shown
void inputProduct(struct Product *inventory);
void displayData(struct Bill);
Product outputData(int pd_id,struct Product inventory[]);
Product getProduct(int pd_id,struct Product inventory[]);
int checkFile();

int main(){

    struct Product inventory[Max];     //variables are initialized
   
   
    int pd_id,i;
    
    if(checkFile()==0)      //if statement to check is file exists 
    {
        printf("Couldn't find the txt.file\n");
        getchar();
        return(1);
    }
     
    initInventory(inventory);       //functions are run
    inputProduct(inventory);
    
    printf("look up an ID:\t");     //asks user what product then want to look up
    scanf("%d",&pd_id);
    fflush(stdin);
    
    for(i = 0;i < Max;i++)     //prints out the product info the user wanted to look up
    {
        if(pd_id == inventory[i].number && !(pd_id <= 0))
        {
            printf("%d\t%s\t\t%0.2f\n",getProduct(pd_id,inventory).number,getProduct(pd_id,inventory).name,getProduct(pd_id,inventory).price);
            break;
        }
        
    }
    
    displayData();
    system("Pause");

    getchar();
    getchar();
    return(0);    //end of program 
}

void initInventory(struct Product *inventory)   
{
    int i;
    for(i = 0;i < Max;i++)
    {
        strcpy(inventory[i].name,"");
        inventory[i].number = 0;
        inventory[i].price = 0.0;
    }
}
int checkFile() //opens file with inventory on it
{
    FILE *infile;
	
    infile = fopen("inventory.txt", "r");
    
    if(infile == NULL)
    {
        return 0;
    }
    else
    {
        return 1;
    }

    fclose(infile);
}

void inputProduct(struct Product *inventory)    //reads the file and prints out whats on it
{
    FILE *infile;
    int i;
	
    infile = fopen("inventory.txt", "r");
    
    for(i=0; i<5; i++)    //for statement for differnet number of product
	{         
        fscanf(infile,"%d",&inventory[i].number);
        fscanf(infile,"%s",&inventory[i].name);
        fscanf(infile,"%f",&inventory[i].price);
        
        printf("%d\t%s\t\t%0.2f\n",inventory[i].number,inventory[i].name,inventory[i].price);	
        printf("\n");
    }
    
    fclose(infile);     //file is closed
    fflush(stdin);  
}

Product getProduct(int pd_id,struct Product inventory[])    //gets product info
 {
        int i;
        
        for(i = 0;i < 5;i++)
        {
            if(pd_id == inventory[i].number)
            {
                return inventory[i];
            }
        }
}

void displayData(){
     int pd_id,quantity;
     char response;
    // inputProduct(inventory);
do{
    printf("Enter the Product Number: ");       //asks for product and quantity
    scanf("%d",&pd_id);
    printf("What Quantity do you need?: ");
    scanf("%d",&quantity);
    printf("Would that be all for today? <y/n> ");
    scanf ("%s",&response);  
    }while(response=='n'||response=='N');

//displays the customers bill
    char name;
    float price,Total;

    pd_id=0;
    
    displayData();
    system("cls");             
    printf("Product Nu.\tItem Name\t\tQty\tCost\n");    //shows info in a table
    printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printf("%d\t%s\t\t%d\t%.2f\n",getProduct(pd_id,inventory).number,getProduct(pd_id,inventory).name,quantity,getProduct(pd_id,inventory).price*quantity);
    printf("\t\t\t\t\t\t~~~~~~\n");    
    printf("Total\t\t\t\t\t\t%.2f",Total);

}
I been struggling with the bill, if anyone can help that would be great.


What exactly is the problem with this part and what do you want it to do?
The program function should ask for the product the user wants and it's quantity then gets it from the file and prints or a nice receipt. The problem is I don't know how to create the function and print out the products the user wants.
Topic archived. No new replies allowed.