Reputation: 352
I tried debugging the lines of code one-by-one and got to know that it shows the error on line " if (strcmp(takein.year, takein.year2) == 0) ".
Please check the link above. I marked the line with red box. No idea why am getting that error.
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
//STRUCTURE
struct date_struct
{
int day;
int month;
int year;
int day2;
int month2;
int year2;
};
//USER-DEFINED FUNCTION
float input(struct date_struct takein);
char tarik(struct date_struct printer);
void header();
//STRUCTURE-VARIABLES
struct date_struct date = { 0,0,0 };
void header()
{
printf("*-*-*-*DATE_STRUCT*-*-*-*");
printf("\n\n");
}
//PROGRAM STARTS HERE
main()
{
//HEADING
header();
//FUCNTION CALL-OUT
input(date);
//TERMINAL_PAUSE
system("pause");
}
float input(struct date_struct takein)
{
printf("Enter the Date(1) : \n");
printf("Day : ");
scanf_s("%d", &takein.day);
printf("\n");
printf("Month : ");
scanf_s("%d", &takein.month);
printf("\n");
printf("Year : ");
scanf_s("%d", &takein.year);
printf("\n");
printf("\n");
printf("Enter the Date-(2) : \n");
printf("Day : ");
scanf_s("%d", &takein.day2);
printf("\n");
printf("Month : ");
scanf_s("%d", &takein.month2);
printf("\n");
printf("Year : ");
scanf_s("%d", &takein.year2);
printf("\n");
tarik(takein);
if (strcmp(takein.year, takein.year2) == 0)
{
if (strcmp(takein.month, takein.month2) == 0)
{
if (strcmp(takein.year, takein.year2) == 0)
{
return(0);
}
else
return(1);
}
else
return(1);
}
else
return(1);
}
char tarik(struct date_struct printer)
{
switch (printer.month)
{
case 1:
printf("Date(1) : January %d, %d\n\n", printer.day, printer.year);
break;
case 2:
printf("Date(1) : February %d, %d\n\n", printer.day, printer.year);
break;
case 3:
printf("Date(1) : March %d, %d\n\n", printer.day, printer.year);
break;
case 4:
printf("Date(1) : April %d, %d\n\n", printer.day, printer.year);
break;
case 5:
printf("Date(1) : May %d, %d\n\n", printer.day, printer.year);
break;
case 6:
printf("Date(1) : June %d, %d\n\n", printer.day, printer.year);
break;
case 7:
printf("Date(1) : July %d, %d\n\n", printer.day, printer.year);
break;
case 8:
printf("Date(1) : August %d, %d\n\n", printer.day, printer.year);
break;
case 9:
printf("Date(1) : September %d, %d\n\n", printer.day, printer.year);
break;
case 10:
printf("Date(1) : Octomber %d, %d\n\n", printer.day, printer.year);
break;
case 11:
printf("Date(1) : November %d, %d\n\n", printer.day, printer.year);
break;
case 12:
printf("Date(1) : Devember %d, %d\n\n", printer.day, printer.year);
break;
}
switch (printer.month2)
{
case 1:
printf("Date(2) : January %d, %d\n\n", printer.day2, printer.year2);
break;
case 2:
printf("Date(2) : February %d, %d\n\n", printer.day2, printer.year2);
break;
case 3:
printf("Date(2) : March %d, %d\n\n", printer.day2, printer.year2);
break;
case 4:
printf("Date(2) : April %d, %d\n\n", printer.day2, printer.year2);
break;
case 5:
printf("Date(2) : May %d, %d\n\n", printer.day2, printer.year2);
break;
case 6:
printf("Date(2) : June %d, %d\n\n", printer.day2, printer.year2);
break;
case 7:
printf("Date(2) : July %d, %d\n\n", printer.day2, printer.year2);
break;
case 8:
printf("Date(2) : August %d, %d\n\n", printer.day2, printer.year2);
break;
case 9:
printf("Date(2) : September %d, %d\n\n", printer.day2, printer.year2);
break;
case 10:
printf("Date(2) : Octomber %d, %d\n\n", printer.day2, printer.year2);
break;
case 11:
printf("Date(2) : November %d, %d\n\n", printer.day2, printer.year2);
break;
case 12:
printf("Date(2) : Devember %d, %d\n\n", printer.day2, printer.year2);
break;
}
return;
}
Upvotes: 1
Views: 4983
Reputation: 1484
strcmp(takein.year, takein.year2)
Says compare the strings store at addresses takein.year
and teakein.year2
.
And what do we have in takein.year
and teakein.year2
?
Those are just normal integers and not addresses. Here strcmp
is trying to access the memory locations outside of it's allocated process memory space and that's why the exception.
Use ==
to compare int
s.
Use strcmp
to compare strings only!
Also what is scanf_s("%d", &takein.year2);
?
I don't believe scanf_s
is the standard library function. Does this code compile and gets link successfully ?
Also if you are using string library functions then include <string.h>
though most probably the default linking library contains the function definitions of most common functions. (e.g. libc
in Linux)
Upvotes: 1
Reputation: 2180
you have been fully answered by fellows, i just want to add some improvement to your code:
// global variable;
const char *months[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
// instead of using switch case
printf("Date(1) : %s %d, %d\n\n",months[printer.month-1], printer.day, printer.year);
printf("Date(2) : %s %d, %d\n\n", months[printer.month2-1],printer.day2, printer.year2);
Upvotes: 1
Reputation: 50775
strcmp(takein.year, takein.year2)
is wrong, the type of takein.year
and takein.year2
is int
and not char*
.
Replacing
strcmp(takein.year, takein.year2)
by
takein.year == takein.year2
Should do the job, but there may be other issues.
Upvotes: 3