Help with a Struct problem :)

This is a discussion on Help with a Struct problem :) within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> struct cliente { char cliente[30]; }; struct cliente dato[30]; struct vendedor { char ne[30]; }; ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    18

    Help with a Struct problem :)

    Code:
    #include <stdio.h>
    #include <conio.h>
    struct cliente
    {
    	char cliente[30];
    };
    struct cliente dato[30];
    struct vendedor
    {
    	char ne[30];
    };
    struct vendedor dato2[30];
    struct producto
    {
    	char np[30];
    	float pp;
    	int cp;
    };
    struct producto dato3[30];
    struct tienda
    {
    	char nt[30],fecha[20];
    };
    struct tienda dato4[30];
    void leer(int);
    void mostrar(int);
    void main()
    {
    	int i;
    	clrscr();
    	printf("\nCuantos clientes desea evaluar:");
    	scanf("%d",&i);
    	leer(i);
    	mostrar(i);
    	getch();
    }
    void leer(int i)
    {
    	int j;
    	clrscr();
    	for(j=0;j<i;j++)
    	{
    	printf("Ingrese nombre de la tienda: ");
    	scanf(" %[^\n]",dato4[j].nt);
    	printf("\nIngrese fecha actual: ");
    	scanf(" %[^\n]",dato4[j].fecha);
    	printf("\nIngrese nombre del cliente: ");
    	scanf(" %[^\n]",dato[j].cliente);
    	printf("\nIngrese nombre del vendedor: ");
    	scanf(" %[^\n]",dato2[j].ne);
    	printf("\nIngrese el nombre del producto: ");
    	scanf(" %[^\n]",dato3[j].np);
    	printf("\nIngrese la cantidad que se compro de dicho producto: ");
    	scanf("%d",&dato3[j].cp);
    	printf("\nIngrese el precio unitario del producto: ");
    	scanf("%f",&dato3[j].pp);
    	clrscr();
    	}
    }
    void mostrar(int i)
    {
    	float st,stt,t;
    	int j;
    	clrscr();
    	for(j=0;j<i;j++)
    	{
    	st=(dato3[j].cp*dato3[j].pp);
    	stt=(0.15*st);
    	t=(st+stt);
    	printf("\n\t\t\t\tTienda %s",dato4[j].nt);
    	printf("\t\t\t%s",dato4[j].fecha);
    	printf("\n\nCliente %s",dato[j].cliente);
    	printf("\nVendedor %s",dato2[j].ne);
    	printf("\n\nProducto %s",dato3[j].np);
    	printf("\nCantidad de Producto: %d",dato3[j].cp);
    	printf("\nPrecio Producto: %.2f",dato3[j].pp);
    	printf("\n\nSub-Total: %.2f",st);
    	printf("\nIVA: 15 Porciente");
    	printf("\n\nTotal: %.2f",t);
    	}
    }
    First and formost i apologize the program is mostly in spanish im not from the US but from latinamerica Secondly yes my code is prob sloppy and sux but anywho down to my problem.
    Ok what the program does is preety much a receipt from a store,so it askes how many clients we want to do receipts for,then asks the name of the client,vendor name,product name,quantity of said product and its price,oh and store name and date.
    Ok so my problem is the follow,when i compile it,doesnt give me any errors but when it gets to:

    Code:
    printf("\nIngrese la cantidad que se compro de dicho producto: ");
    scanf("%d",&dato3[j].cp);
    Meaning when on the screen appears "Ingrese la cantidad que se compro de dicho producto" (Insert quantity bought of said product),ok i input a integer number lets say 3,i press enter and it exits the program I have no idea what the problem is,any help is great appreciated.Thank you in advance for your help.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    scanf("%d",&(dato3[j].cp));
    Kurt

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    18
    Still exits the program after that line

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yeah just noticed that this is not the problem. Sorry.
    Kurt

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    18
    No problem been trying to figure out what the problem is but cant seem to see any problems whatsoever.
    I tried the following,instead of making pp a float a made it an integer and the program worked just fine sadly i cant leave pp as an integer cause pp=product price wich can be anything from 2 to 1.75,etc so sadly it has to be a float

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Actually the program runs fine for me.
    The only problem I can see is that you don't protect against bufferoverflows when you read strings.
    You should do something like this
    Code:
    	printf("Ingrese nombre de la tienda: ");
    	scanf(" %29[^\n]",dato4[j].nt);
    Kurt

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    18
    hhmm it doesnt exit when you get to:
    Code:
    printf("\nIngrese la cantidad que se compro de dicho producto:");
    scanf("%d",&dato3[j].cp);
    Odd ive ran it on 2 diffrent machines and it exits as soon as i input a value for cp(quantity of product).

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I have only removed all the clrscr().
    My output
    Code:
    Cuantos clientes desea evaluar:1
    Ingrese nombre de la tienda: a
    
    Ingrese fecha actual: a
    
    Ingrese nombre del cliente: a
    
    Ingrese nombre del vendedor: a
    
    Ingrese el nombre del producto: a
    
    Ingrese la cantidad que se compro de dicho producto: 5
    
    
    Ingrese el precio unitario del producto: 12.3
    
                                    Tienda a                        a
    
    Cliente a
    Vendedor a
    
    Producto a
    Cantidad de Producto: 5
    Precio Producto: 12.30
    
    Sub-Total: 61.50
    IVA: 15 Porciente
    
    Total: 70.72
    Kurt

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    18
    Well elimination all the clear screens helped with something,the error on screen when run is "Floating point formats not linked" "Abnormal program termination" So there we go :\ what problem i have with float i have no idea.

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Think I heard about that. Some VC bug.
    Think a workaround is just to initialize any float value e.g. in main
    Code:
    int main() {
       float f= 0.0f;
        .....
    }
    Kurt

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    18
    What i found when i searched the error msg in google was

    Code:
    static void forcefloat(float *p)
    {
    float f = *p;
    forcefloat(&f);
    }
    That fixed it :\ have no idea what that line of code does but it fixed it good luck to me explaning to my professor what that means anywho thank you for your help Zuk i appreciate it

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    There is some kind of "smart linker" that does not detect that you are using floatingpoint values in your program ( you are using float values only in scanf statements ) so it doesn't link the floatingpoint support to your program. Initailizig a variable helps.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 09:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21