Thread: Using and searching structures values

  1. #1
    Registered User ataias's Avatar
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    3

    Unhappy Using and searching structures values

    Hello everyone, I'm having some troubles using structures. I should make a program to read a number of bakers, "p" bakers and a number of clients, "c" clients. And I should use structures to this. I'm doing the code below:

    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<NP; 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);
    			}
    	}
    }	
    	return(0);
    	}
    I want with this program an output like this:
    Clients:
    Name1: client1 surname|ID
    Baker_of_first_purchase: baker1|ID1; Amount purchased: X;
    ...
    Baker_of_nth_purchase: bakerN|IDN; Amount purchased: Xn;
    ...

    I want this for each client: the full name of the client, and the bakeries he did the purchases and the amount purchased (numberofbreakpurchases*priceofbread).

    The problem is that there's some trouble with my logic and the program isn't working well. It shows a lot of outputs when it should show only the values of the correct IDs given. I take the ID of the bakery in the struct client and I try find the price of the breads in the bakery with a loop but I think (I'm almost sure) that's not working. Could someone help me?
    See you!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First: Why are these variables (const int NP=1000, NC=10000; ) set so high?

    You might not be able to create the arrays with these large numbers unless you use dynamic memory.

    Second: you have commented out all of the printf() before your data entries.

    Why? When someone runs your program they will not know what and when to enter.

    Just my first thoughts, I have not gotten past the entries yet.

    Jim

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The reason you are printing a lot of outputs is because of this loop at about line 79.

    Code:
             for(a=0; a<NP; a++) {
    This will loop from 0 to NP-1, and since NP == 1000 it will run 1000 times since there are no other exit points for this loop.

    Jim

  4. #4
    Registered User ataias's Avatar
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    3

    Wink A bit better now

    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?

  5. #5
    Registered User ataias's Avatar
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    3

    Unhappy Now...

    Hello again, I have changed the last part of the program:
    It's now like this:
    Output:
    Code:
    	printf("\n$\nClientes:\n");
    	for(i=0; i<C; i++){
    		float money;
    		int ID, aux,k;
    		a=hungry[i].num_compras;
    		printf("Name: %s | %d\n", hungry[i].name, hungry[i].id);
    		for(j=0; j<a; j++){
    			for(k=0; k<=hungry[i].num_compras; k++){
    			if (hungry[i].compras[j].id==bakery[k].id){
    				money=hungry[i].compras[j].paes_comprados*bakery[k].preco_pao;
    				ID=bakery[k].id;
    				aux=k;
    			}
    		}
    			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");
    	}
    Now it's ok about the zeros:
    Code:
    $
    Clientes:
    Name: cliente1 sobrenome1 | 1
    Bakery: padaria1 | 1; Purchase amount: 2.50
    Bakery: padaria2 | 2; Purchase amount: 0.75
    --
    Name: cliente2 | 2
    Bakery: padaria1 | 1; Purchase amount: 2.50
    Bakery: padaria1 | 1; Purchase amount: 1.00
    Bakery: padaria1 | 1; Purchase amount: 1.50
    --
    Name: cliente3 | 3
    Bakery: padaria2 | 2; Purchase amount: 3.75
    --
    $
    Now, I want to sum the values for each purchase for each client, like:

    Code:
    Bakery: padaria1 | 1; Purchase amount: 5.00
    instead the three times appeared. Some tip?

    After corrected this bug, I should order the clients by alphabetic ordering, how should I do this?
    See you all!

Popular pages Recent additions subscribe to a feed