Sorry, I forgot to explain somethings. NP is the max number of bakeries and NC is the max number of clients (the maximum array extension). By the way, they're constants, not variables. I created this array cause the tutors in my University said it should be something not very small, at least NP=1000 and NC=10000. I haven't studied how to use dynamic memory and I'm still not sure about what it is.
I used:
Code:
for(a=0; a<NP; a++) {
I commented the printfs cause they are not necessary, they are only for testing.
Cause I want to find the price of bread in any bakery (NP = number of bakeries) that have the ID given.
The problem is really in this line you said, and now it's getting closer the output I want. I changed this line and the code is now like this:
Code:
#include<stdio.h>
typedef struct{
char name[50];
int id, num_paes;
float preco_pao;
} R_Padaria;
typedef struct{
int id, paes_comprados;
}R_vetor_compra;
typedef struct{
char name[50];
int id, num_compras;
R_vetor_compra compras[100];
} R_Cliente;
int main(){
const int NP=1000, NC=10000;
int i, j, P, C, X, a, b;
R_Padaria bakery[NP];
R_Cliente hungry[NC];
// printf("Type the number of bakeries: ");
scanf("%d%*c", &P);
for(i=0; i<P; i++){
// printf("Enter the name of the bakery %d: ", i+1);
scanf("%[^\n]s%*c", bakery[i].name);
// printf("Enter the ID of the bakery %d: ", i+1);
scanf("%d%*c", &bakery[i].id);
// printf("Enter the price of each bread of the bakery %d: ", i+1);
scanf("%f%*c", &bakery[i].preco_pao);
// printf("Enter the number of breaks in the bakery %d: ", i+1);
scanf("%d%*c", &bakery[i].num_paes);
}
// for(i=0; i<P; i++){
// printf("%s\n", bakery[i].name);
// printf("%d\n", bakery[i].id);
// printf("%.2f\n", bakery[i].preco_pao);
// printf("%d\n", bakery[i].num_paes);
// }
// printf("Enter the number of clients: ");
scanf("%d%*c", &C);
for(i=0; i<C; i++){
// printf("Enter the name of client %d: ", i+1);
scanf("%[^\n]s%*c", hungry[i].name);
// printf("Enter the ID of client %d: ", i+1);
scanf("%d%*c", &hungry[i].id);
// printf("Enter the number of purchases of client %d: ", i+1);
scanf("%d%*c", &hungry[i].num_compras);
for(j=0; j<hungry[i].num_compras; j++){
// printf("Enter the bakery ID of purchase %d of client %d: ", j+1, i+1);
scanf("%d%*c", &hungry[i].compras[j].id);
// printf("Enter the numbers of breaks of purchase %d of client %d", j+1, i+1);
scanf("%d%*c", &hungry[i].compras[j].paes_comprados);
}
}
// for(i=0; i<C; i++){
// printf("Name: ");
// printf("%s\n", hungry[i].name);
// printf("ID: ");
// printf("%d\n", hungry[i].id);
// printf("Amount of purchasings: ");
// printf("%d\n", hungry[i].num_compras);
// for(j=0; j<hungry[i].num_compras; j++){
// printf("Purchase %d\n", j+1);
// printf("bakery ID: ");
// printf("%d\n", hungry[i].compras[j].id);
// printf("Amount of bread purchased: ");
// printf("%d\n", hungry[i].compras[j].paes_comprados);
// }
// }
printf("\n$\nClientes:\n");
for(i=0; i<C; i++){
float money;
int ID, aux;
a=hungry[i].num_compras;
printf("Name: %s | %d\n", hungry[i].name, hungry[i].id);
for(j=0; j<a; j++){
for(a=0; a<=hungry[i].num_compras; a++){
if (hungry[i].compras[j].id==bakery[a].id){
money=hungry[i].compras[j].paes_comprados*bakery[a].preco_pao;
ID=bakery[a].id;
aux=a;
}
}
if (hungry[i].compras[j].id==bakery[aux].id){
printf("Bakery: %s | %d; Purchase amount: ", bakery[aux].name, ID);
printf("%.2f\n", money);
}
}
printf("--\n");
}
printf("$\n");
return(0);
}
Testing:
in.txt (input)
Code:
2
padaria1
1 0.5
50
padaria2
2
0.75
25
3
cliente1 sobrenome1
1
2
1 5
2 1
cliente2
2
3
1 5
1 2
1 3
cliente3
3
1
2 5
Output:
Code:
$
Clientes:
Name: cliente1 sobrenome1 | 1
Bakery: padaria1 | 1; Purchase amount: 2.50
Bakery: padaria2 | 2; Purchase amount: 0.75
Bakery: | 0; Purchase amount: 0.00
--
Name: cliente2 | 2
Bakery: padaria1 | 1; Purchase amount: 2.50
Bakery: padaria1 | 1; Purchase amount: 1.00
Bakery: padaria1 | 1; Purchase amount: 1.50
Bakery: | 0; Purchase amount: 0.00
--
Name: cliente3 | 3
Bakery: padaria2 | 2; Purchase amount: 3.75
--
$
The program isn't finalized and until this moment (I should add somethings). but until now, the only problems I have is that it should show only one time each id. Example: it showed three times "padaria1". but it should be shown only once with the total of the three purchases like:
Code:
Bakery: padaria1 | 1; Purchase amount: 5.00
And I don't know why it's showing
Code:
"Bakery: | 0; Purchase amount: 0.00";
Thanks for your comment, it helped me. And now?