/ Forside / Teknologi / Udvikling / C/C++ / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
C/C++
#NavnPoint
BertelBra.. 2425
pmbruun 695
Master_of.. 501
jdjespers.. 500
kyllekylle 500
Bech_bb 500
scootergr.. 300
gibson 300
molokyle 287
10  strarup 270
Cant quite figure out whats going wrong!?
Fra : Simon Mansfield


Dato : 05-12-04 16:41

Im trying to make a C program that takes in a date (birthday) and tells the
user how many days it has been since that date. So far I have got this... It
compiles ok but then crashes, with no idea why I was wondering if anyone
else had any idea??

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

int day, mnth, yr; //The users birthday.
time_t now, birth;
struct tm *birthday, store;
double i;

now = time(0);

//Prompt the user for birthdate.
printf("Please enter your birthday [DD/MM/YYYY]: ");

//Read in the birthday.
scanf("%2d/%2d/%4d", &day, &mnth, &yr);
printf("\n\n");

store.tm_mday = day;
store.tm_mon = (mnth - 1);
store.tm_year = yr;

*birthday = store;
birth = mktime(birthday);
i = difftime(now, birth);

printf("%f", 1);

getchar();
getchar();

return 0;
}



 
 
Thomas Lykkeberg (05-12-2004)
Kommentar
Fra : Thomas Lykkeberg


Dato : 05-12-04 19:08

On Sun, 05 Dec 2004 15:40:54 GMT, "Simon Mansfield"
<global0inferno@ntlworld.com> wrote:

> *birthday = store;

Hmmm, birthday is not initialised to point to anything... This
operation copies the content of "store" into the location that
birthday points to.... and what is that ?

/Thomas
----
My e-mail is composed in the following manner:
<sirname>.<lastname>(a)privat(dot)dk

Bertel Lund Hansen (05-12-2004)
Kommentar
Fra : Bertel Lund Hansen


Dato : 05-12-04 19:10

Simon Mansfield skrev:

> struct tm *birthday, store;

> *birthday = store;

= is not defined to be used that way. If it were, the whole
struct would be copied to an undefines area since birthday has
not been set to point to anything.

Use this instead:

*birthday = &store;

which makes birthday point to the same area that store occupies.

Remember that modifying any of them (store/*birthday) will modify
the other.

--
Bertel
http://bertel.lundhansen.dk/   FIDUSO: http://fiduso.dk/

Bertel Lund Hansen (05-12-2004)
Kommentar
Fra : Bertel Lund Hansen


Dato : 05-12-04 22:50

Bertel Lund Hansen skrev:

>Use this instead:

> *birthday = &store;

Sorry. Drop the star:

birthday = &store;

--
Bertel
http://bertel.lundhansen.dk/   FIDUSO: http://fiduso.dk/

Bertel Brander (05-12-2004)
Kommentar
Fra : Bertel Brander


Dato : 05-12-04 22:36

Simon Mansfield wrote:
> Im trying to make a C program that takes in a date (birthday) and tells the
> user how many days it has been since that date. So far I have got this... It
> compiles ok but then crashes, with no idea why I was wondering if anyone
> else had any idea??
>
> #include <stdio.h>
> #include <stdlib.h>
To use memset, add:
#include <string.h>
> #include <time.h>
>
> int main() {
>
> int day, mnth, yr; //The users birthday.
> time_t now, birth;
> struct tm *birthday, store;
> double i;
>
> now = time(0);
>
> //Prompt the user for birthdate.
> printf("Please enter your birthday [DD/MM/YYYY]: ");
>
> //Read in the birthday.
> scanf("%2d/%2d/%4d", &day, &mnth, &yr);
> printf("\n\n");
>
Better clear store first:
memset(&store, 0, sizeof(store));
> store.tm_mday = day;
> store.tm_mon = (mnth - 1);
> store.tm_year = yr;
tm_year is years since 1900, so change the above to:
store.tm_year = yr - 1900;
>
> *birthday = store;
Let birthday point at store, change the above to:
birthday = &store;
> birth = mktime(birthday);
> i = difftime(now, birth);
>
> printf("%f", 1);
The parameter should be i not 1
>
> getchar();
> getchar();
>
> return 0;
> }
>
>


--
"I learned more from a three minute record
than I ever learned in school"
- Bruce Springsteen

Søg
Reklame
Statistik
Spørgsmål : 177558
Tips : 31968
Nyheder : 719565
Indlæg : 6408914
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste