Hi i've just started to use constructors and I'm having some problems with the following code with data not saving(saving to the correct place)

please Help :(

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>



typedef struct details // struct for all data

{

        char name[50][50];                  

        char card[50][16];                                      

        long sort[50], acco[50];                              

        int  pin[50];                                              

} DETAILS;



void info();





int main() 

{

     DETAILS dt;

     strcpy(dt.name[0],"SAM BERWICK");

     dt.sort[0] = 102030;

     dt.acco[0] = 12345678;

     strcpy(dt.card[0],"1234567812345678");

     dt.pin[0] = 1234;

     

     strcpy(dt.name[1],"DAN BERWICK");

     dt.sort[1] = 302010;

     dt.acco[1] = 87654321;

     strcpy(dt.card[1],"8765432187654321");

     dt.pin[1] = 4321;

     info();

     

}



void info()

{

     DETAILS dt;

     printf("Details n1\n");

     printf("%d",dt.sort[0]);

     system("pause");

}

Dani AI

Generated

Quick diagnosis and why the data looked wrong
The code shown is plain C, not C++, so there are no "constructors" involved. The immediate bug is that info() declares its own local DETAILS dt; — that variable is uninitialized, so printing dt.sort[0] shows garbage. Also printf("%d", ...) is wrong for long values; use %ld (or match the type). As hinted, either pass the filled struct to info() or make it global/static; do not expect a local variable inside info() to contain main's data.

A simple fix (pass by pointer)
Use a function that takes a pointer to the data you populated in main. For example:

void info(const DETAILS *d)
{
    printf("Details n1\n");
    printf("%ld\n", d->sort[0]);
}

/* call it as */
info(&dt);

Better data modeling
As suggested, an array-of-structures is cleaner than a struct-of-arrays for this use. Define a single record type and make an array of that type. This reduces indexing mistakes and improves readability:

typedef struct {
    char name[50];
    char card[17]; /* 16 digits + NUL */
    long sort;
    long acco;
    int pin;
} Account;

Account accounts[50] = {0};

Use safe copying (e.g., strncpy/snprintf) and ensure the card buffer is at least 17 bytes to hold 16 digits plus the terminating NUL. Initialize structures with = {0} or memset to avoid uninitialized reads.

Note on rounding to nearest 10
If you prefer integer-only rounding (alternative to the lround approach), handle positives and negatives explicitly:

int round_to_10(int n)
{
    return (n >= 0) ? ((n + 5) / 10) * 10 : ((n - 5) / 10) * 10;
}

These changes address the uninitialized-variable bug, improve type safety, and make the data layout easier to work with.

Recommended Answers

All 5 Replies

Ooops please remove this one.

Not sure why you have fifty of everything...

char name[50][50];

Why don't you create an array of structures instead of hard coding fifty elements into your structure members.

DETAILS dt[50];

Hi i've just started to use constructors and I'm having some problems with the following code with data not saving(saving to the correct place)

please Help :(

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>



typedef struct details // struct for all data

{

        char name[50][50];                  

        char card[50][16];                                      

        long sort[50], acco[50];                              

        int  pin[50];                                              

} DETAILS;



void info();





int main() 

{

     DETAILS dt;

     strcpy(dt.name[0],"SAM BERWICK");

     dt.sort[0] = 102030;

     dt.acco[0] = 12345678;

     strcpy(dt.card[0],"1234567812345678");

     dt.pin[0] = 1234;

     

     strcpy(dt.name[1],"DAN BERWICK");

     dt.sort[1] = 302010;

     dt.acco[1] = 87654321;

     strcpy(dt.card[1],"8765432187654321");

     dt.pin[1] = 4321;

     info();

     

}



void info()

{

     DETAILS dt;

     printf("Details n1\n");

     printf("%d",dt.sort[0]);

     system("pause");

}

Where's the constructor problem? I didn't quite catch you..If you are trying to write a function that'll print your struct's variable data then you have to pass a struct parameter in your function..

Never mind about that I managed to sort it out. In C how would I round a number to the nearest 10.

Its pretty straight forward

#include <stdio.h>
#include <math.h>

int main()
{
	double val = 123.33;

	fprintf(stdout, "rounded->%d\n", lround(val/10.0) * 10);
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.