Reputation: 125
I can't find errors in this code. I'm trying to write a program that returns the number of days (ex: 60) in the year based on day, month and year [ex: 1/3/2000 (3 Mar 2000)].
The compiler give me these errors:
- too few arguments to function 'day_of_year'
- conflicting types for 'day_of_year'
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
int day, month, year, i, count=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
printf ("Enter the day: ");
scanf ("%d", &day);
printf ("Enter the month: ");
scanf ("%d", &month);
printf ("Enter the year: ");
scanf ("%d", &year);
count=day_of_year();
printf ("Count: %d", count);
return 0;
}
int day_of_year (int day, int month, int year, int i, int count){
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (year%4==0) a[2]++;
count = day;
for (i=0;i<month;i++)
count+=a[i];
return count;
}
Upvotes: 0
Views: 71
Reputation: 431
compiler gives error because of below inline reason:
° too few arguments to function 'day_of_year'
Reason: called day_of_year() function without parameter.
° conflicting types for 'day_of_year'
Reason: prototype of day_of_year()
function is different from day_of_year()
definition.
Prototype says to compiler that it take 3 parameter but function definition having 5 parameter. this mismatch causing the error.
Upvotes: 0
Reputation: 2468
I put some comments.
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
// you don't need to declare 'i' here, or a[]
int day, month, year,count;
printf ("\nEnter the day: ");
scanf ("%d", &day);
printf ("\nEnter the month: ");
scanf ("%d", &month);
printf ("\nEnter the year: ");
scanf ("%d", &year);
// you need to pass the parameters to the function
count=day_of_year(day,month,year);
printf ("\nCount: %d", count);
return 0;
}
// here you put in the function signature two more variables.
//they are not used and also they differ from the initial definition.
int day_of_year (int day, int month, int year){
int count=0,i=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (year%4==0) a[1]++;
count = day;
for (i=0;i<month;i++)
count+=a[i];
return count;
}
Upvotes: 1
Reputation: 121427
Apart from mismatching prototype % definition, you are also not passing any arguments to the function.
You have few off-by-one errors. Index in C starts from 0.
Here's a modified version. Pay close attention to the changes in day_of_year
function. Particularly, for February, you want to increment a[1]
(not a[2]
). Similarly, the for
loop's condition must be month - 1
(not month
).
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
int day, month, year, i, count=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
printf ("Enter the day: ");
scanf ("%d", &day);
printf ("Enter the month: ");
scanf ("%d", &month);
printf ("Enter the year: ");
scanf ("%d", &year);
count=day_of_year(day, month, year);
printf ("Count: %d", count);
return 0;
}
int day_of_year (int day, int month, int year)
{
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
int count = day, i;
if (year%4==0) a[1]++;
for (i=0;i<month - 1;i++)
count+=a[i];
return count;
}
Upvotes: 0
Reputation: 125
Thanks to all. I corrected the errors. This is the new code.
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
int day, month, year, i, count=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
printf ("Enter the day: ");
scanf ("%d", &day);
printf ("Enter the month: ");
scanf ("%d", &month);
printf ("Enter the year: ");
scanf ("%d", &year);
count=day_of_year(day, month, year);
printf ("Count: %d", count);
return 0;
}
int day_of_year (int day, int month, int year){
int i, count;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (year%4==0) a[2]++;
count = day;
for (i=0;i<month;i++)
count+=a[i];
return count;
}
Upvotes: 1
Reputation: 4106
There were quite a few errors in your original code, but a solution that compiles can be like this:
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
int day, month, year, count=0;
printf ("Enter the day: ");
scanf ("%d", &day);
printf ("Enter the month: ");
scanf ("%d", &month);
printf ("Enter the year: ");
scanf ("%d", &year);
count=day_of_year(day, month, year);
printf ("Count: %d", count);
return 0;
}
int day_of_year (int day, int month, int year){
int i = 0, count = 0; // declaration was missing
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (year%4==0) a[2]++;
count = day;
for (i=0;i<month;i++)
count+=a[i];
return count;
}
First of all you declare your day_of_year
function at the beginning of your code, but when you implement it you use additional arguments which is forbidden.
You should declare count
and i
inside the body of the function so I moved them from the function's argument list.
Second, when you call day_of_year
in main
you pass nothing, although you have them read from console.
Other than that the logic to determine whether the input year is a leap year is not an ideal solution but I leave that up to you to correct.
Hope it helps.
Upvotes: 0
Reputation: 33283
Declaration:
int day_of_year(int day, int month, int year);
Call:
count=day_of_year();
Definition:
int day_of_year (int day, int month, int year, int i, int count){
C is a strongly typed language meaning the number and the type of functions arguments much match.
So, you need correct the argument list and add the missing parameters to the function call.
It looks like you don't really need the last two arguments in the definition - declare them as local variables instead:
int day_of_year (int day, int month, int year){
int i, count;
Upvotes: 1
Reputation: 1049
You haven't passed any parameters to your function day_of_year.
Upvotes: 0