Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/01/2013, 18:07
extol
 
Fecha de Ingreso: enero-2013
Mensajes: 9
Antigüedad: 11 años, 3 meses
Puntos: 0
Duda con un programa en C

Buenas a todos, soy nuevo en esta comunidad y me registre por la siguiente razon, estoy aprendiendo C y tengo el siguiente codigo:

#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>

struct sucursal
{
char nombre[10];
char direc[10];
char loca[10];
float imp;
float neto;
float total;
};

struct sucursal *carga(struct sucursal *reg1)
{
int i;
system("cls");
for(i=0;i<5;i++)
{
puts("Ingrese nombre");
fflush(stdin);
scanf("%s",reg1[i].nombre);
fflush(stdin);
puts("Ingrese direccion");
fflush(stdin);
scanf("%s",reg1[i].direc);
puts("Ingrese localidad");
fflush(stdin);
scanf("%s",reg1[i].loca);
puts("Ingrese importe");
fflush(stdin);
scanf("%f",&reg1[i].imp);
puts("Ingrese neto");
fflush(stdin);
scanf("%f",&reg1[i].neto);
puts("Ingrese importe total de ventas");
fflush(stdin);
scanf("%f",&reg1[i].total);
};
return reg1;
}

void modifica(struct sucursal *reg2,char *nom,int codigo_campo)
{
int j=0;
char t;
system("cls");
puts("Ingrese nombre a buscar");
fflush(stdin);
gets(nom);
do{
j++;
}while(strcmp(nom,reg2[j].nombre)!=0 && j<5);

if (strcmp(nom,reg2[j].nombre)==0)
{
puts("Ingrese nombre");
fflush(stdin);
scanf("%s",reg2[codigo_campo].nombre);
puts("Ingrese direccion");
fflush(stdin);
scanf("%s",reg2[codigo_campo].direc);
puts("Ingrese localidad");
fflush(stdin);
scanf("%s",reg2[codigo_campo].loca);
puts("Ingrese importe");
fflush(stdin);
scanf("%f",&reg2[codigo_campo].imp);
puts("Ingrese neto");
fflush(stdin);
scanf("%f",&reg2[codigo_campo].neto);
puts("Ingrese importe total de ventas");
fflush(stdin);
scanf("%f",&reg2[codigo_campo].total);
}
else puts("No encontrado");
};
void listado(struct sucursal [5]);


main()
{
char nomb[10],resp;
struct sucursal reg[5],*r;
int i;
puts("Empieza la carga");
r=carga(reg);
system("cls");
puts("Ingrese nombre para enviar como argumento");
gets(nomb);
i=0;
do{
puts("Desea modificar algo(s/n)?");
fflush(stdin);
resp=getchar();
}while(resp!='s' && resp!='n');
while(resp!='n' && i<5)
{
i++;
modifica(r,nomb,i);
do{
puts("Desea modificar algo mas(s/n)?");
fflush(stdin);
resp=getchar();
}while(resp!='s' && resp!='n');
};
listado(reg);
getche();
}

void listado(struct sucursal re[5])
{
int i,j;
float aux3,aux4,aux5;
char aux[10],aux1[10],aux2[10];
system("cls");
for(i=0;i<4;i++)
{for(j=i+1;j<5;j++)
{
if(strcmp(re[i].loca,re[j].loca)>0) {

strcpy(aux,re[i].nombre);
strcpy(re[i].nombre,re[j].nombre);
strcpy(re[j].nombre,aux);

aux3=re[i].imp;
re[i].imp=re[j].imp;
re[j].imp=aux3;

aux4=re[i].neto;
re[i].neto=re[j].neto;
re[j].neto=aux4;

aux5=re[i].total;
re[i].total=re[j].total;
re[j].total=aux5;


strcpy(aux1,re[i].direc);
strcpy(re[i].direc,re[j].direc);
strcpy(re[j].direc,aux1);




strcpy(aux2,re[i].loca);
strcpy(re[i].loca,re[j].loca);
strcpy(re[j].loca,aux2);
};
};};
for(i=0;i<5;i++)
{
puts("");
printf("Nombre %s\n",re[i].nombre);
fflush(stdin);
printf("Direccion %s\n",re[i].direc);
fflush(stdin);
printf("Localidad %s\n",re[i].loca);
fflush(stdin);
printf("Importe %f\n",re[i].imp);
fflush(stdin);
printf("Neto %f\n",re[i].neto);
fflush(stdin);
printf("Importe total de ventas %f\n",re[i].total);
fflush(stdin);
puts("");
};
}


Mi problema es que al momento de mostrar el listado se mezclan algunas coas, por ejemplo pongo direccion 'San juan' y localidad 'Cordoba' y en algunos sale Sn JuanCordoba,.... o sea sale como mezclado.

Espero que me puedan ayudar, estoy hace horas con esto. Muchas gracias de antemano.

P.D: le puse mucho fflush(stdin); por las dudas, por el tema del buffer.