Tema destacado: ¡Aprende hacking con práctica! - arZone, el wargame de elhacker.net
Autor
|
Tema: agenda en c... problema con mostrar contactos y buscar contacto en especifico (Leído 596 veces)
|
attackers
Desconectado
Mensajes: 37
|
amigos tneog un problema con este codigo que despues que quiero mostrar los contactos de mi agenda me sale error tambien quiero agregarle que busque un contacto en especifico #include<stdio.h> #include<conio.h> #include <stdlib.h> #define p printf #define s scanf struct contacto{ char nombre[40]; long telefono; char equipo; }; typedef struct contacto tipo_contacto; int contador; int numerodeamigos; void main() { tipo_contacto c1; int numero; int i; do { p("\n\tPOR FAVOR ELIJA UNA DE LAS SIGUIENTES OPCIONES\n\n\n\n"); p("\t1.AGREGAR CONTACTO\n"); p("\n\t2.MOSTRAR CONTACTOS DE LA AGENDA \n\n"); scanf("%i",&numero); switch(numero) { case 1:{ system ("cls"); printf("cuantos amigos quiere agregar: "); scanf("%d",&numerodeamigos); system("cls"); for (contador=0; contador<numerodeamigos; contador++) { printf("ingrese el nombre: "); fflush(stdin); gets(c1.nombre); printf("diga su equipo: "); fflush(stdin); scanf("%c",&c1.equipo); printf("Ingrese el telefono: "); fflush(stdin); scanf("%d",&c1.telefono); system("cls"); printf("\n"); } } break; case 2: { system ("cls"); for (i=0; i<numerodeamigos; i++) /***abre el for */ { printf("El nombre es: %s, \n", c1.nombre); printf ("el equipo es: %s \n",c1.equipo); printf ("el telefono: %d \n",c1.telefono); system("pause"); } /**cierra el for**/ } /**cierra el case*/ break; } /***cierra el do***/ }/*** cierra el main **/ while(numero<7); return 0; }
|
|
|
|
|
En línea
|
|
|
|
s00rk
Desconectado
Mensajes: 139
|
No sera porque tienes un solo dato y no un arreglo o puntero de ese tipo?
tipo_contacto c1; deberia ser tipo_contacto c1[100];
o eso creo no recuerdo bien si tambien podria ser tipo_contacto *c1;
Saludos & suerte n_n, ahorita no puedo checar lo demas por eso nomas puse eso ejje
|
|
|
|
|
En línea
|
|
|
|
|
|
attackers
Desconectado
Mensajes: 37
|
amigos ya solucione ahora le quiero hacer otras funcion por ejemplo en los equipo quiero ponerle que agregue 2 equipo CARAQUISTA Y MAGALLANES entonce quiero que si piso 3 me muestre los contactos CARAQUISTAS y si PISO 4 que muestre los contactos MAGALLANEROS Aqui les dejo el codigo #include<stdio.h> #include<conio.h> #include <stdlib.h> #define p printf #define s scanf struct contacto{ char equipo[40]; char nombre[40]; int telefono; }; typedef struct contacto tipo_contacto; int numero; /** el numero del menu de opciones*/ int numerodeamigos; /** el numero de amigos que quiero agregar a la agenda */ int contadorAGREGAR; /** el contador del for agregar*/ int contadorELIMINAR; /** el contador del for de eliminar */ void main(void){ tipo_contacto c1[80]; do { p("\n\tPOR FAVOR ELIJA UNA DE LAS SIGUIENTES OPCIONES\n\n\n\n"); p("\t1.AGREGAR CONTACTO\n"); p("\n\t2.MOSTRAR TODOS LOS CONTACTOS DE TU AGENDA \n\n"); p("\t3.MOSTRAR LOS CONTACTOS MAGALLANEROS\n"); p("\t4.MOSTRAR LOS CONTACTOS CARAQUISTA\n"); scanf("%i",&numero); switch(numero) { case 1:{ system ("cls"); printf("cuantos amigos quiere agregar: "); scanf("%i",&numerodeamigos); for (contadorAGREGAR=0; contadorAGREGAR<numerodeamigos; contadorAGREGAR++) { system("cls"); printf("ingrese el nombre: "); fflush(stdin); gets(c1[contadorAGREGAR].nombre); printf("Diga el equipo de su amigo: MAGALLANERO O CARAQUISTA\n"); fflush(stdin); gets(c1[contadorAGREGAR].equipo); printf("Ingrese el telefono: "); fflush(stdin); scanf("%d",&c1[contadorAGREGAR].telefono); system("cls"); printf("\n"); } } break; case 2: { system ("cls"); for (contadorELIMINAR=0; contadorELIMINAR<numerodeamigos; contadorELIMINAR++) /***abre el for */ { printf("\nEl nombre es: %s \n", &c1[contadorELIMINAR].nombre); printf ("el equipo es: %s \n",&c1[contadorELIMINAR].equipo); printf ("el telefono: %d \n",c1[contadorELIMINAR].telefono); } /**cierra el for**/ system("pause"); } /**cierra el case*/ break; case 3: { } break; } /***cierra el do***/ }/*** cierra el main **/ while(numero<7); return 0; }
|
|
|
|
|
En línea
|
|
|
|
s00rk
Desconectado
Mensajes: 139
|
Porque simplemente no recorrer todo el arreglo de c1 y cuando el contacto sea CARAQUISTA lo muestre e igual con el otro ejemplo for(in x = 0; x < numerodeamigos; x++) { if(strcmp(c1[x].equipo,"caraquista") == 0) { //Imprimir datos } } e igual con el otro. P.D. Tu codigo es un tanto ineficiente porque si pones que deceas agregar 4 usuarios, y luego deceas agregar otros 4 usuarios, estos no se agregaran sino reemplazaran a los anteriores y el chiste es que agregue a nuevos sin remover los que ya estan.
|
|
|
|
|
En línea
|
|
|
|
attackers
Desconectado
Mensajes: 37
|
P.D. Tu codigo es un tanto ineficiente porque si pones que deceas agregar 4 usuarios, y luego deceas agregar otros 4 usuarios, estos no se agregaran sino reemplazaran a los anteriores y el chiste es que agregue a nuevos sin remover los que ya estan.
amigo y como mejoraria esa parte ??? aqui te dejo ya el codigo algo corregido #include<stdio.h> #include<conio.h> #include <stdlib.h> #define p printf #define s scanf #include <string.h> struct contacto{ char equipo[40]; char nombre[40]; int telefono; }; typedef struct contacto tipo_contacto; int numero; /** el numero del menu de opciones*/ int numerodeamigos; /** el numero de amigos que quiero agregar a la agenda */ int contadorAGREGAR; /** el contador del for agregar*/ int contadorELIMINAR; /** el contador del for de eliminar */ int main(void) { tipo_contacto c1[80]; do { p("\n\tPOR FAVOR ELIJA UNA DE LAS SIGUIENTES OPCIONES\n\n\n\n"); p("\t1.AGREGAR CONTACTO\n"); p("\n\t2.MOSTRAR TODOS LOS CONTACTOS DE TU AGENDA \n\n"); p("\t3.MOSTRAR LOS CONTACTOS MAGALLANEROS\n"); p("\t4.MOSTRAR LOS CONTACTOS CARAQUISTA\n"); scanf("%i",&numero); switch(numero) { case 1:{ system ("cls"); printf("cuantos amigos quiere agregar: "); scanf("%i",&numerodeamigos); for (contadorAGREGAR=0; contadorAGREGAR<numerodeamigos; contadorAGREGAR++) { system("cls"); printf("ingrese el nombre: "); fflush(stdin); gets(c1[contadorAGREGAR].nombre); printf("Diga el equipo de su amigo: MAGALLANERO O CARAQUISTA\n"); fflush(stdin); gets(c1[contadorAGREGAR].equipo); printf("Ingrese el telefono: "); fflush(stdin); scanf("%d",&c1[contadorAGREGAR].telefono); system("cls"); printf("\n"); } } break; case 2: { system ("cls"); for (contadorELIMINAR=0; contadorELIMINAR<numerodeamigos; contadorELIMINAR++) /***abre el for */ { printf("\nEl nombre es: %s \n", &c1[contadorELIMINAR].nombre); printf ("el equipo es: %s \n",&c1[contadorELIMINAR].equipo); printf ("el telefono: %d \n",c1[contadorELIMINAR].telefono); } /**cierra el for**/ system("pause"); } /**cierra el case*/ break; case 3: { for(int x = 0; x < numerodeamigos; x++) { if(strcmp(c1[x].equipo,"caraquista") == 0) { printf("los contactos caraquista son"); printf("\nEl nombre es: %s \n", &c1[x].nombre); printf ("el telefono: %d \n",c1[x].telefono); } } } break; case 4: { for(int y = 0; y < numerodeamigos; y++) { if(strcmp(c1[y].equipo,"magallanero") == 0) { printf("los contactos caraquista son"); printf("\nEl nombre es: %s \n", &c1[y].nombre); printf ("el telefono: %d \n",c1[y].telefono); } } }/**cierra el caso 4*/ } /***cierra el do***/ }/*** cierra el main **/ while(numero<7); }
|
|
|
|
|
En línea
|
|
|
|
attackers
Desconectado
Mensajes: 37
|
amigos aqui les dejo el codigo con otras cositas mas o con otras dudas la duda la tengo cuando quiero eliminar un contacto en espesifico como tendria que ser ????? #include<stdio.h> #include<conio.h> #include <stdlib.h> #define p printf #define s scanf #include <string.h> struct contacto{ char equipo[40]; char nombre[40]; int telefono; }; typedef struct contacto tipo_contacto; int cont=0; int numero; /** el numero del menu de opciones*/ int numerodeamigos; /** el numero de amigos que quiero agregar a la agenda */ int contadorAGREGAR; /** el contador del for agregar*/ int contadorELIMINAR; /** el contador del for de eliminar */ int main(void) { tipo_contacto c1[80]; /*******************************************/ /*******************************************/ /************ MENU DE OPCIONES **************/ /*******************************************/ /*******************************************/ do { p("\n\tPOR FAVOR ELIJA UNA DE LAS SIGUIENTES OPCIONES\n\n\n\n"); p("\t1.AGREGAR CONTACTO\n"); p("\n\t2.MOSTRAR TODOS LOS CONTACTOS DE TU AGENDA \n\n"); p("\t3.MOSTRAR LOS CONTACTOS CARAQUISTA\n"); p("\t4.MOSTRAR LOS CONTACTOS MAGALLANEROS\n"); p("\t5.BUSCAR CONTACTO\n"); p("\t6.ELIMINAR CONTACTO\n"); scanf("%i",&numero); switch(numero) { /*******************************************/ /*******************************************/ /************ AGREGA LOS CONTACTOS **************/ /*******************************************/ /*******************************************/ case 1:{ system ("cls"); printf("cuantos amigos quiere agregar: "); scanf("%i",&numerodeamigos); for (contadorAGREGAR=0; contadorAGREGAR<numerodeamigos; contadorAGREGAR++) { system("cls"); printf("ingrese el nombre: "); fflush(stdin); gets(c1[contadorAGREGAR].nombre); printf("Diga el equipo de su amigo: caraquista o magallanero \n"); fflush(stdin); gets(c1[contadorAGREGAR].equipo); printf("Ingrese el telefono: "); fflush(stdin); scanf("%d",&c1[contadorAGREGAR].telefono); system("cls"); printf("\n"); } } break; /*******************************************/ /*******************************************/ /*** MUESTRA LOS CONTACTO DE LA AGENDA *****/ /*******************************************/ /*******************************************/ case 2: { system ("cls"); for (contadorELIMINAR=0; contadorELIMINAR<numerodeamigos; contadorELIMINAR++) /***abre el for */ { printf("\nEl nombre es: %s \n", &c1[contadorELIMINAR].nombre); printf ("el equipo es: %s \n",&c1[contadorELIMINAR].equipo); printf ("el telefono: %d \n",c1[contadorELIMINAR].telefono); } /**cierra el for**/ system("pause"); } /**cierra el case*/ break; /*******************************************/ /*******************************************/ /****** MUESTRA CONTACTOS CARAQUISTAS *******/ /*******************************************/ /*******************************************/ case 3: { for(int x = 0; x < numerodeamigos; x++) { if(strcmp(c1[x].equipo,"caraquista") == 0) { printf("los contactos caraquista son"); printf("\nEl nombre es: %s \n", &c1[x].nombre); printf ("el telefono: %d \n",c1[x].telefono); } } } break; /*******************************************/ /*******************************************/ /****** MUESTRA CONTACTOS MAGALLANERO *******/ /*******************************************/ /*******************************************/ case 4: { for(int y = 0; y < numerodeamigos; y++) { if(strcmp(c1[y].equipo,"magallanero") == 0) { printf("los contactos magallanero son"); printf("\nEl nombre es: %s \n", &c1[y].nombre); printf ("el telefono: %d \n",c1[y].telefono); } } break; case 5: /****/ { char busca[40]; system("cls"); fflush(stdin); printf("\nBuscar contacto\n Ingrese el nombre del contacto:"); gets(busca); for(int k=1;k<numerodeamigos;k++){ if(strcmpi(busca,c1[k].nombre)==0){ printf("\nNombre: %s\n", c1[k].nombre); printf("Telefono fijo: %d\n", c1[k].equipo); } } } /******/ case 6: { char busca1[40]; system("cls"); fflush(stdin); printf("\n eliminar contacto \n Ingrese el nombre del contacto:"); gets(busca1); for(int e=1;e<numerodeamigos;e++){ if(strcmpi(busca1,c1[e].nombre)==0){ printf("\nNombre: %s\n", c1[e].nombre); printf("Telefono: %d\n", c1[e].telefono); printf("Telefono: %d\n", c1[e].equipo); printf("ELIMINADO"); c1[e].nombre==NULL; } } } }/**cierra el caso 4*/ } /***cierra el do***/ }/*** cierra el main **/ while(numero<7); }
|
|
|
|
|
En línea
|
|
|
|
s00rk
Desconectado
Mensajes: 139
|
Bueno ahora que tuve tiempo decidi hacerlo a mi modo, y aqui te dejo a como yo lo haria, espero te sirva #include <stdio.h> #include <time.h> #include <iostream.h> using namespace std; struct contacto{ char equipo[40]; char nombre[40]; int telefono; int ocupado; }; typedef struct contacto tipo_contacto; int main() { int cont; int numero; /** el numero del menu de opciones*/ int numerodeamigos; /** el numero de amigos que quiero agregar a la agenda */ bool hayespacio; char busca[40]; tipo_contacto c1[80]; for(int x = 0; x < 80; x++) { c1[x].ocupado = 0; } do { cout << "\tPOR FAVOR ELIJA UNA DE LAS SIGUIENTES OPCIONES" << endl << endl; cout << "\t1.AGREGAR CONTACTO" << endl; cout << "\t2.MOSTRAR TODOS LOS CONTACTOS DE TU AGENDA" << endl; cout << "\t3.MOSTRAR LOS CONTACTOS CARAQUISTA" << endl; cout << "\t4.MOSTRAR LOS CONTACTOS MAGALLANEROS" << endl; cout << "\t5.BUSCAR CONTACTO" << endl; cout << "\t6.ELIMINAR CONTACTO" << endl; cout << "\t0.SALIR" << endl; cin >> numero; system ("cls"); switch(numero) { case 1: cout << "Cuantos amigos deceas agregar:" << endl; cin >> numerodeamigos; hayespacio = false; for(int i = 0; i < numerodeamigos; i++) { for (int x = 0; x < 80; x++) { if(c1[x].ocupado == 0) { system("cls"); cout << "Contacto Numero[" << (i+1) << "]" << endl; cout << "Ingrese el nombre: " << endl; cin >> c1[x].nombre; cout << "Diga el equipo de su amigo: caraquista o magallanero" << endl; cin >> c1[x].equipo; cout << "Ingrese el telefono: " << endl; cin >> c1[x].telefono; c1[x].ocupado = 1; hayespacio = true; break; } } } system("cls"); if(!hayespacio) { cout << "No hubo espacio en la agenda para agregar nuevos contactos" << endl; }else{ cout << "Usuarios agregados Correctamente" << endl; } break; case 2: hayespacio = false; for(int x = 0; x < 80; x++) { if(c1[x].ocupado == 1) { cout << "Contacto #" << (x+1) << endl; cout << "Nombre: " << c1[x].nombre << endl; cout << "Equipo: " << c1[x].equipo << endl; cout << "Telefono: " << c1[x].telefono << endl << endl << endl; hayespacio = true; } } if(!hayespacio) cout << "No hay contactos en la agenda" << endl; break; case 3: cout << "Caraquista" << endl << endl; for(int x = 0; x < 80; x++) { if(strcmp(c1[x].equipo,"caraquista") == 0 && c1[x].ocupado == 1) { cout << "Nombre: " << c1[x].nombre << endl; cout << "Telefono: " << c1[x].telefono << endl; } } break; case 4: cout << "Magallanero" << endl << endl; for(int x = 0; x < 80; x++) { if(strcmp(c1[x].equipo,"magallanero") == 0 && c1[x].ocupado == 1) { cout << "Nombre: " << c1[x].nombre << endl; cout << "Telefono: " << c1[x].telefono << endl; } } break; case 5: cout << "Buscar contacto" << endl << "Ingrese el nombre del contacto:" << endl; cin >> busca; hayespacio = true; for(int x = 0; x < 80; x++) { if(strcmp(c1[x].nombre, busca) == 0 && c1[x].ocupado == 1) { cout << endl << "Nombre: " << c1[x].nombre << endl; cout << "Telefono: " << c1[x].telefono << endl; cout << "Equipo: " << c1[x].equipo << endl; hayespacio = true; } } if(!hayespacio) cout << "No se encontro al contacto" << endl; break; case 6: cout << "Eliminar contacto" << endl << "Ingrese el nombre del contacto:" << endl; cin >> busca; hayespacio = false; for(int x = 0; x < 80; x++) { if(strcmp(c1[x].nombre,busca) == 0 && c1[x].ocupado == 1) { c1[x].ocupado = 0; cout << endl << "Usuario Eliminado" << endl; hayespacio = true; } } if(!hayespacio) cout << "No se encontro al contacto" << endl; break; default: cout << "Eleccion elegida no existe" << endl; break; } getchar();getchar(); system("cls"); }while(numero != 0); cin.get();cin.get(); return 0; }
|
|
|
|
|
En línea
|
|
|
|
|
|