Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/04/2016, 07:44
Blast06
 
Fecha de Ingreso: julio-2015
Ubicación: San Juan
Mensajes: 7
Antigüedad: 8 años, 9 meses
Puntos: 0
Pregunta Error en codigo al ordenar struct por int

Saludos.

Estoy tratando de ordenar un struct por un dato int en especifico.(campo "codigo" para ser exactos). Pero me tira un error al compilar y ni diablos con el. Aqui les dejo el codigo a ver que tal.

El error es el siguiente: [Error] no match for 'operator[]' (operand types are 'estructura_amigo' and 'int')

Este es el código:

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <string.h>
  6.  
  7. #define USER "admin"
  8. #define PASS "admin"
  9. #define L 80 /* Longitud máxima de las cadenas */
  10.  
  11.  
  12. int loggin (void);
  13. void sistema (void);
  14. void registrar (void);
  15. void ordernar (void);
  16.  
  17.  void gotoxy(int x,int y){  
  18.       HANDLE hcon;  
  19.       hcon = GetStdHandle(STD_OUTPUT_HANDLE);  
  20.       COORD dwPos;  
  21.       dwPos.X = x;  
  22.       dwPos.Y= y;  
  23.       SetConsoleCursorPosition(hcon,dwPos);  
  24.  }
  25.  
  26. struct estructura_amigo {
  27.        int codigo; 
  28.        char nombre[40];
  29.        float precio;
  30.        float precio_v;
  31.        char suplidor[40];
  32.    
  33.        };
  34.  
  35. struct estructura_amigo amigo, *p_amigo;
  36.  
  37.  int main () {
  38.     int temp=1;
  39.    
  40.     temp=loggin();
  41.     if(temp!=0) {    sistema();     }
  42.    
  43.    
  44.     return 0;
  45.    
  46.  }
  47.  
  48. int loggin (void) {
  49.  
  50.     char usuario[L + 1], password[L + 1];
  51.     int contador = 0 ;
  52.     char ingresa = 0; /* Variable booleana 1 = true, 0 = false */
  53.     int i = 0;  /* Indica la posición del caracter leído en la cadena */
  54.     char caracter; /* Guarda el caracter leído mediante la función getch */
  55.  
  56.     do {
  57.         system("cls");
  58.         gotoxy(10,0);printf("***************Proyecto final Estructura**************");
  59.         gotoxy(10,1);printf("***************:) HOLA**************");
  60.    
  61.         gotoxy(30,10);printf("Usuario: ");
  62.         gets(usuario);
  63.  
  64.         gotoxy(30,11);printf("Password: ");
  65.  
  66.         /* Lectura del password */
  67.         i = 0;  /* Indica la posición del caracter leído en la cadena */
  68.  
  69.         caracter = getch();
  70.         while (i < L && caracter != 13) {
  71.             printf("*");
  72.             password[i] = caracter;
  73.             i++;
  74.             caracter = getch();
  75.         }
  76.         password[i] = '\0';
  77.  
  78.         if (strcmp(usuario, USER) == 0 && strcmp(password, PASS) == 0) {
  79.             ingresa = 1;
  80.         } else {
  81.             gotoxy(20,20);printf("El usuario y/o password son incorrectos");
  82.             getchar();
  83.             contador++;
  84.         }
  85.  
  86.     } while (ingresa == 0 && contador < 3);
  87.  
  88.     if (ingresa == 0) {
  89.       gotoxy(20,20);printf("Usted no pudo ingresar al sistema. ADIOS"); getchar(); return ingresa;
  90.    } else {
  91.      gotoxy(25,20);printf("Bienvenido al sistema....\n");
  92.  
  93.       /* */
  94.    }
  95.  
  96.    getchar();
  97.    return ingresa;
  98.  
  99.  
  100.  }
  101.  
  102.  void sistema (void) {
  103.     char opc;
  104.    do
  105.      {
  106.        system("cls");
  107.        gotoxy(30,8);printf("1.Registrar Datos");
  108.        gotoxy(30,10);printf("2.Actualizar");
  109.        gotoxy(30,12);printf("3.Eliminar");
  110.        gotoxy(30,14);printf("4.Ordenar");
  111.        gotoxy(30,16);printf("5.Salir");
  112.        opc=getch( );
  113.        switch(opc)
  114.      {
  115.        case '1': registrar(); break;
  116.        case '4': ordernar (); break;
  117.      }
  118.       }while (opc!='5');
  119.  }
  120.  
  121. void registrar (void) {
  122.     p_amigo = &amigo;
  123.  
  124.      /* Introducimos los datos mediante punteros */
  125.      system("cls");
  126.      printf("Codigo: ");fflush(stdin);
  127.      scanf("%i", &p_amigo->codigo);
  128.      printf("Nombre: ");fflush(stdin);
  129.      gets(p_amigo->nombre);
  130.      printf("Precio: ");fflush(stdin);
  131.      scanf( "%f", &p_amigo->precio );
  132.      printf("Precio Ganancia: ");fflush(stdin);
  133.      scanf( "%f", &p_amigo->precio_v );
  134.      printf("Suplidor: ");    fflush(stdin);
  135.      gets(p_amigo->suplidor);
  136.    
  137.    
  138. }
  139.  
  140. void ordernar (void)
  141. {    
  142.       /*system("cls");
  143.       while(p_amigo!='\0')
  144.       {
  145.         printf( "Codigo Producto:  %i ", p_amigo->codigo);
  146.         printf( " Nombre: %s ", p_amigo->nombre);
  147.         printf( "Precio: %f.\n", p_amigo->precio);
  148.       }*/
  149.      
  150.      
  151.      
  152.      
  153.  
  154.     int i,j, bandera,aux,n;
  155.     for(i=1; i<n; i++)
  156.     {
  157.     bandera=0;         //inciamos la bandera en 0
  158.      for(j=n-1; j>=i; j--){
  159.         if(amigo[j-1].codigo > amigo[j].codigo){
  160.                 aux=amigo[j].codigo;
  161.                 amigo[j].codigo=amigo[j-1].codigo;
  162.                 amigo[j-1].codigo=aux;
  163.                 bandera=1; //si hubo cambio cambiamos la bandera a 1
  164.             }
  165.          }
  166.          if (bandera==0)
  167.         break; //si no hubo cambios entonces salir del for
  168.     }
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.      
  176.    
  177. }