Thread: Structure in Switch Case

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    12

    Structure in Switch Case

    Hello! i need to make a little program (as an exercise) for a guitar store , i used a structure for saving items in the inventory but whenever i try to display the items i get wrong information... (i just learned about structures so im really new to this)

    I hope you can help me with this.

    Code:
    #include <stdio.h>
    
    
    struct guitarra{
    	char marca[40],modelo[25];
    	float precio;
    	};
    
    
    int main(){
    	int opcion=0,n=1,i;
    
    
    	while(opcion!=3){
    	printf("1.Registrar Instrumento\n");
    	printf("2.Mostrar inventario\n");
    	printf("3.Salir\n");
    	printf("\nOpcion: ");
    	scanf("%d",&opcion);
    	printf("\n");
    
    
    	if (opcion==1){
    	printf("Cuantas guitarras desea registrar: ");
    	scanf("%d",&n);
    	printf("\n");
    	}
    
    
    	struct guitarra info[n];
    
    
    	switch (opcion){
    	case 1:{
    	     for (i=0; i<n; i++){
    		printf("Marca de la guitarra: ");
    		scanf("%s", info[i].marca);   
    		printf("Nombre de la guitarra: ");
    		scanf("%s", info[i].modelo);
    		printf("precio de la guitarra: ");
    		scanf("%f", &info[i].precio);
    		printf("\n");
    		}
    	}
    	break;
    	case 2:{
    	     for(i=0;i<n;i++){
    		printf("Marca de la guitarra: %s",info[i].marca);
    		printf("\nNombre de la guitarra: %s",info[i].modelo);
    		printf("\nPrecio de la guitarra: %f",info[i].precio);
    		printf("\n\n");
    		}
    	}
    	break;
    	case 3:
    		opcion=3;
    	break;
         }
      }				
    }         		
    Thanks !

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    looks like you creating your array of structs on each iteration of the loop

    So everything you have added to the array on the previous loop iteration is just lost
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    Thank you , so what would be a good solution? I tried putting the struct right after the declaration of the first variables on main but it screwed everything up :s

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would move following code
    Code:
    	printf("Cuantas guitarras desea registrar: ");
    	scanf("%d",&n);
    	printf("\n");
    	struct guitarra info[n];
    before the loop - but instead of asking for number of guitars to enter on this iteration - I would ask for maximum number of guitars ever...

    And in the loop I would only ask how many to fill or print - and check that it does not exceeds the number of allocated structs

    When posting code - if you do not want to use English in printf - at least add English comment that translate the printf text for us (and pass code as text - so it could be color and line numbered)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    Ok , I'll do that the next time.

    Thanks a lot for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  2. switch case
    By lilhawk2892 in forum C++ Programming
    Replies: 18
    Last Post: 09-21-2005, 08:23 PM
  3. Need help with switch/case selection structure
    By nickadeemus2002 in forum C++ Programming
    Replies: 13
    Last Post: 06-29-2004, 02:16 PM
  4. switch, case help
    By campermama in forum C++ Programming
    Replies: 7
    Last Post: 06-16-2004, 09:49 AM
  5. switch case/structure...
    By gcn_zelda in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 04:16 PM