Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Help, my function yearToVote is not printing out any statements. The function is supposed to calculate how many elections someone would be able to vote based on their birthday, not sure why the function is not printing anything based on the if statements. Run the program if you need to see what I am trying to do.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//Prototype
double calc_age(const unsigned int m, const unsigned int d, const unsigned int y);
void yearToVote(int daysAge, int birthDay, int birthMonth);
void ageNextElection(AgeinDays);
int main()
{
int month, day, year, AgeinDays;
printf("Enter birthday (mm): ");
scanf("%d", &month);
printf("Enter birthday (dd): ");
scanf("%d", &day);
printf("Enter birthday (yyyy): ");
scanf("%d", &year);
//Call
AgeinDays = calc_age(month, day, year);
printf("%d \n", AgeinDays -1);
ageNextElection(AgeinDays);
yearToVote(AgeinDays, day, month);
}
//First Function: Calculates Age in Days
double calc_age(const unsigned int m1, const unsigned int d1, const unsigned int y1) { //unsigned - can't be negative. const- can't be cahnged
struct tm t = {0};
time_t t_of_day;
t.tm_year = y1 - 1900;
t.tm_mon = m1 -1;
t.tm_mday = d1;
t.tm_isdst = -1;
t_of_day = mktime(&t);
time_t now;
time(&now);
return round((difftime(now,t_of_day)/(60.0*60.0*24.0)));
}
//Second Function: How many times and years voted
void yearToVote(int daysAge, int Bday, int Bmonth)
{
int A, B, m[11] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 374}, monthsDay;
A = daysAge/365;
if (Bmonth == 1){
monthsDay = m[0];
}
if (Bmonth == 2){
monthsDay = m[1];
}
if (Bmonth == 3){
monthsDay = m[2];
}
if (Bmonth == 4){
monthsDay = m[3];
}
if (Bmonth == 5){
monthsDay = m[4];
}
if (Bmonth == 6){
monthsDay = m[5];
}
if (Bmonth == 7){
monthsDay = m[6];
}
if (Bmonth == 8){
monthsDay = m[7];
}
if (Bmonth == 9){
monthsDay = m[8];
}
if (Bmonth == 10){
monthsDay = m[9];
}
if (Bmonth == 11){
monthsDay = m[10];
}
if (Bmonth == 12){
monthsDay = m[11];
}
B = Bday + monthsDay;
if (A >= 18){
if (daysAge <= 1986 && daysAge >= 1980 && B <= 307) {
printf ("You could have voted in the last 4 elections and this election.\n");
} else if (daysAge <= 1990 && daysAge > 1986 && B <= 307) {
printf ("You could have voted in the last 3 elections and this election.\n");
} else if (daysAge <= 1994 && daysAge > 1990 && B <= 307) {
printf ("You could have voted in the last 2 elections and this election.\n");
} else if (daysAge <= 1998 && daysAge > 1994 && B <= 307) {
printf ("You could have voted in the last election and this election.\n");
} else if (daysAge <= 2002 && daysAge > 1998 && B <= 307) {
printf ("You can vote in this election.\n");
} else if (daysAge >= 2002 && B > 307) {
printf("You are not eligible to vote.\n");
}
}
}
//Third Function: Age in Eligibility of next election
void ageNextElection(int AgeinDays){
int AgeinNext;
AgeinDays = AgeinDays + 1460;
AgeinNext = AgeinDays / 365;
printf("Your age in the next election is: %d \n", AgeinNext);
if(AgeinNext >= 18){
printf("You are eligible to vote next election! \n");
}
else{
printf("You will not be eligible to vote \n");
}
}
On my computer your code seems OK:
Enter birthday (mm): 01
Enter birthday (dd): 01
Enter birthday (yyyy): 2000
7611
Your age in the next election is: 24
You are eligible to vote next election!
Of course, there are some things to fix:
src/main.c:132:1: warning: parameter names (without types) in function declaration
132 | void ageNextElection(AgeinDays);
| ^~~~
src/main.c: In function 'yearToVote':
src/main.c:178:70: warning: excess elements in array initializer
178 | int A, B, m[11] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 374}, monthsDay;
| ^~~
src/main.c:178:70: note: (near initialization for 'm')
Related
I've been staring at the same code for hours and I just need another person to proof read it because I cant figure out the problem anymore ive probably read my 1000 line code a hundred times already. This is the chunk with the problem. I know the solution is probably an extra * or & but i just need help finding it. I would really appreciate it thanks.
My error says:
Cannot convert int** to int* for argument 1 to void dayCheck(int*, int*, float*, float*, int, int, float*, char*, char*)
void restDayCheck(float *, float *, char *, char *);
void dayCheck(int *, int *, float *, float *, int, int, float *, char *,
char *);
void timeLoop(int *, int *, int *, int *, int *, float *, int *, int *, float *,
float *);
void restDayCheck(float *totalSalary, float *hourlyRate, char *restAns,
char *restWorkAns) {
do {
printf("\n\tIs this day a rest day? [y/n] : ");
scanf(" %c", restAns);
printf("\n\tDid you work on this day? [y/n] : ");
scanf(" %c", restWorkAns);
if (*restAns != 'y' || *restAns != 'Y' || *restAns != 'n' ||
*restAns != 'N')
printf("Answer/s are invalid.");
} while (*restAns != 'y' || *restAns != 'Y' || *restAns != 'n' ||
*restAns != 'N');
}
void timeLoop(int *startMonth, int *startDate, int *endMonth, int *endDate,
int *totalDays, float *totalSalary, int *shiftHours,
int *shiftMins, float *hourlyRate, float *minRate, char *restAns,
char *restWorkAns) {
// VARIABLES
int timeIN, timeOUT;
int newDate, newMonth, i;
// int x, y; extra if i need
float restDayPay = (*hourlyRate * 8) * 0.30;
printf("\n \t \tCLOCKING IN\n");
// LOOP
for (i = 0; i >= *totalDays; i++) {
float OTrate = *hourlyRate * 0.25;
if (*startMonth == 1 || *startMonth == 3 || *startMonth == 5 ||
*startMonth == 7 || *startMonth == 8 || *startMonth == 10 ||
*startMonth == 12) {
do {
printf("\n\tDate : %d/%d", *startMonth, *startDate);
do {
printf("\n \tEnter Time-In (Military Time 0000-2359): ");
scanf("\t%d", &timeIN);
if (timeIN < 0 || timeIN > 2359)
printf("\n \t Time-In is invalid (Military time ONLY)");
} while (timeIN < 0 || timeIN > 2359);
do {
printf("\n \tEnter Time-Out (Military Time 0000-2359): ");
scanf("\t%d", &timeOUT);
if (timeOUT < 0 || timeOUT > 2359)
printf("\n \t Time-Out is invalid (Military time ONLY)");
} while (timeOUT < 0 || timeOUT > 2359);
computeTotalHours(timeIN, timeOUT, shiftHours, shiftMins);
restDayCheck(totalSalary, hourlyRate, restAns, restWorkAns);
dayCheck(&startDate, &startMonth, &hourlyRate, &minRate, timeIN,
timeOUT, &totalSalary, &shiftHours, &shiftMins, &restAns,
&restWorkAns);
*startDate = *startDate + 1;
// TEMPORARY to check if it worked
printf("\n\tTotal Hours: %d\n", *shiftHours);
printf("\n\tTotal Minutes: %d\n", *shiftMins);
} while (*startDate <
32); // less than 32 to limit date of the months to 31
}
if (*startMonth == 2) {
do {
printf("\n\tDate : %d/%d", *startMonth, *startDate);
do {
printf("\n \tEnter Time-In (Military Time 0000-2359): ");
scanf("\t%d", &timeIN);
if (timeIN < 0 || timeIN > 2359)
printf("\n \t Time-In is invalid (Military time ONLY)");
} while (timeIN < 0 || timeIN > 2359);
do {
printf("\n \tEnter Time-Out (Military Time 0000-2359): ");
scanf("\t%d", &timeOUT);
if (timeOUT < 0 || timeOUT > 2359)
printf("\n \t Time-Out is invalid (Military time ONLY)");
} while (timeOUT < 0 || timeOUT > 2359);
computeTotalHours(timeIN, timeOUT, shiftHours, shiftMins);
restDayCheck(totalSalary, hourlyRate, restAns, restWorkAns);
dayCheck(&startDate, &startMonth, &hourlyRate, &minRate, timeIN,
timeOUT, &totalSalary, &shiftHours, &shiftMins, &restAns,
&restWorkAns);
*startDate = *startDate + 1;
// TEMPORARY to check if it worked
printf("\n\tTotal Hours: %d\n", *shiftHours);
printf("\n\tTotal Minutes: %d\n", *shiftMins);
} while (*startDate < 29); // since 28 is the total days in february, less
// than 29 to limit the days
}
if (*startMonth == 4 || *startMonth == 6 || *startMonth == 9 ||
*startMonth == 11) {
do {
printf("\n\tDate : %d/%d", *startMonth, *startDate);
do {
printf("\n \tEnter Time-In (Military Time 0000-2359): ");
scanf("\t%d", &timeIN);
if (timeIN < 0 || timeIN > 2359)
printf("\n \t Time-In is invalid (Military time ONLY)");
} while (timeIN < 0 || timeIN > 2359);
do {
printf("\n \tEnter Time-Out (Military Time 0000-2359): ");
scanf("\t%d", &timeOUT);
if (timeOUT < 0 || timeOUT > 2359)
printf("\n \t Time-Out is invalid (Military time ONLY)");
} while (timeOUT < 0 || timeOUT > 2359);
computeTotalHours(timeIN, timeOUT, shiftHours, shiftMins);
restDayCheck(totalSalary, hourlyRate, restAns, restWorkAns);
dayCheck(&startDate, &startMonth, &hourlyRate, &minRate, timeIN,
timeOUT, &totalSalary, &shiftHours, &shiftMins, &restAns,
&restWorkAns);
*startDate = *startDate + 1;
// TEMPORARY to check if it worked
printf("\n\tTotal Hours: %d\n", *shiftHours);
printf("\n\tTotal Minutes: %d\n", *shiftMins);
} while (*startDate < 31); // 31 is less than 30 which is the total days
// in the months listed in the if statement
}
*startMonth = *startMonth + 1;
}
}
void dayCheck(int *startDate, int *startMonth, float *hourlyRate,
float *minRate, int timeIN, int timeOUT, float *totalSalary,
int *shiftHours, int *shiftMins, char *restAns,
char *restWorkAns) {}
I did not include the code in dayCheck anymore because my error is with calling the function. Thanks so much again.
Ive tried pretty much everything. I know my error is just something ive overlooked or skipped. I just need another set of eyes tbh.
New coder taking class in C. I'm attempting to write a program that calculates a number for a given date and then uses the number to determine what day of the week the date fell on. Program is unfinished because I am unable to pass user input stored in a structure (struct date udate) from function get_date to function calc_date_number. Any help would be appreciated.
#include <stdio.h>
/*Define structure
------------------*/
struct date
{
int month;
int day;
int year;
};
/*Declare function prototypes
-----------------------------*/
struct date get_date (struct date);
void calc_date_number (struct date);
void main (void)
{
struct date udate, calc_date;
printf("Welcome to the Date to Day-of-the-Week program.\n\nThe program will give the day of the
for any date from 1/1/1900.\n\n");
get_date (udate);
calc_date_number (calc_date);
}
/*Define functions get_date
----------------------------*/
struct date get_date (struct date udate)
{
do
{
printf ("Enter the date (mm/dd/yyyy): ");
scanf ("%d/%d/%d", &udate.month, &udate.day, &udate.year);
if (udate.month < 1 || udate.month > 12)
printf ("Invalid month. Please re-enter date.\n\n");
else if (udate.day <1 || udate.day > 31)
printf ("Invalid day. Please re-enter date.\n\n");
else if (udate.year < 1900)
printf ("Invalid year. Please re-enter date.\n\n");
else if (udate.month ==2 && udate.day == 29 && (udate.year !=0 && (udate.year == 0 ||
udate.year % 400 != 0)))
printf ("Invalid date. Not a leap year. Please re-enter date.\n\n");
}while (udate.month < 1 || udate.month > 12 || udate.day < 1 || udate.day > 31 || udate.year <
1900);
return udate;
} /*End get_date*/
/*Define function calc_date_number
----------------------------------*/
void calc_date_number (struct date calc_date)
{
printf("calc_date is %i %i %i\n\n", calc_date.month, calc_date.day, calc_date.year);
long int n;
if (calc_date.month <= 2)
{
calc_date.year = calc_date.year - 1;
calc_date.month = calc_date.month + 13;
}
else
{
calc_date.month = calc_date.month + 1;
}
n = 1461 * calc_date.year / 4 + 153 * calc_date.month / 5 + calc_date.day;
}/*End function calc_date_number*/
Here is a working version of your program with extra comments to explain.
#include <stdio.h>
/*Define structure
* ------------------*/
struct date
{
int month;
int day;
int year;
};
/*Declare function prototypes
* -----------------------------*/
struct date get_date (struct date);
long int calc_date_number (struct date); /* now return the number */
/* use int instead of void */
int main (void)
{
struct date udate, calc_date;
printf("Welcome to the Date to Day-of-the-Week program.\n\nThe program will give the day of the"
"for any date from 1/1/1900.\n\n");
calc_date = get_date (udate); /* store the result in calc_date */
long int n = calc_date_number (calc_date); /* store the result in n */
printf("calculated date number : %ld\n", n); /* display the value just calculated */
return 0; /* return code of the program */:
}
/*Define functions get_date
* ----------------------------*/
struct date get_date (struct date udate)
{
do
{
printf ("Enter the date (mm/dd/yyyy): ");
scanf ("%d/%d/%d", &udate.month, &udate.day, &udate.year);
if (udate.month < 1 || udate.month > 12)
printf ("Invalid month. Please re-enter date.\n\n");
else if (udate.day <1 || udate.day > 31)
printf ("Invalid day. Please re-enter date.\n\n");
else if (udate.year < 1900)
printf ("Invalid year. Please re-enter date.\n\n");
else if (udate.month ==2 && udate.day == 29 && (udate.year !=0 && (udate.year == 0 ||
udate.year % 400 != 0)))
printf ("Invalid date. Not a leap year. Please re-enter date.\n\n");
}while (udate.month < 1 || udate.month > 12 || udate.day < 1 || udate.day > 31 || udate.year <
1900);
return udate;
} /*End get_date*/
/*Define function calc_date_number
* ----------------------------------*/
long int calc_date_number (struct date calc_date)
{
printf("calc_date is %i %i %i\n\n", calc_date.month, calc_date.day, calc_date.year);
long int n;
if (calc_date.month <= 2)
{
calc_date.year = calc_date.year - 1;
calc_date.month = calc_date.month + 13;
}
else
{
calc_date.month = calc_date.month + 1;
}
n = 1461 * calc_date.year / 4 + 153 * calc_date.month / 5 + calc_date.day;
return n;
}/*End function calc_date_number*/
This is a menu driven program that allows a user to:
(1)view a calendar
(2)view on which day of the week a particular date falls
(3)exit the program.
But its not running please help:
#include <stdio.h>
#include<conio.h>
#include<math.h>
void load_menu();
void view_calender();
void view_date();``
int main (int argv, char** arv)
{
load_menu();
return ;
}
void load_menu()
{
int choice;
do
{
printf("Menu\n");
printff("1. View Calender\n");
printf("2. View on which day a particular dau falls\n");
printf("3. Exit Programme\n");
scanf("%d", &choice);
switch(choice)
{
case 1: view_calender();
break;
case 2: view_date();
break;
case 3: printf("Exiting Programme!\n");
exit(0);
break;
default: printf("Invalid Choice chosen\n");
break;
}
} while (choice != 3);
}
void view_calender()
{
int is_leap_year( int year ); /* True if leap year */
int leap_years( int year ); /* The number of leap year */
int today_of( int y, int m, int d); /* The number of days since the beginning of the year */
long days( int y, int m, int d); /* Total number of days */
void calendar(int y, int m); /* display calendar at m y */
int year,month;
printf("Enter the Month(mm): ");
scanf("%d", &month);
printf("Enter the Year(yyyy): ");
scanf("%d", &year);
calendar(year, month);
return 0;
int isLeapYear( int y ) /* True if leap year */
{
return(y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
}
int leapYears( int y ) /* The number of leap year */
{
return y/4 - y/100 + y/400;
}
int TodayOf( int y, int m, int d) /* The number of days since the beginning of the year */
{
static int DayOfMonth[] =
{ -1,0,31,59,90,120,151,181,212,243,273,304,334};
return DayOfMonth[m] + d + ((m>2 && is_leap_year(y))? 1 : 0);
}
long Days( int y, int m, int d) /* Total number of days */
{
int last_year;
last_year = y - 1;
return 365L * last_year + leapy_years(last_year) + TodayOf(y,m,d);
}
void view_calendar() /* display calendar at m y */
{
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char* day[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday" "Friday", "Saturday"};
int d;
int moti;
int yel;
int i;
printf("Enter date in this format dd-mm-yyyy: ");
scanf("%d-%d-%d", &d, &moti, &yel);
// correction for leap year
for (i = 1900; i < yel; i++)
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
d += 366;
else
d += 365;
for (i = 0; i < moti - 1; i++)
d += month[i];
printf("This is a %s.\n", day[d % 7]);
return 0;
}
}
If the user selects option (2), the program must display the day on which the user's entered date falls.
For instance, if the user wants to know on which day 24th October 2021 falls, they must be prompted to enter the year (i.e. 2021), the month (i.e. 10 for October), and the date (i.e. 24).
The output must be the day of the week e.g. "Sunday".
The program, after executing option (1) or (2) must display the menu again.
The source code you show gets a warning about an incompatible definition of exit because:
exit is a reserved identifier; it should be used only for the standard library routine named exit.
Your program does not include the <stdlib.h> that would provide a declaration of exit.
Your program uses exit without a declaration for it. When a routine is used without a declaration, your compiler supplies a default declaration.
The default declaration is for a function returning int, but the correct declaration for exit is a function returning nothing (void), so the compiler warnings you the declaration (provided by default) is incompatible with the built-in standard function.
To fix this, insert #include <stdlib.h> in your program.
So let me explain:
On input u got:
e.g.:
5 7 1904 5 5 5
675876789
day,month,year hours,mins,secs and some number k.
At the output you need to get:
4.12.1925. 20:38:14
I'm constantly getting 5.12.1925 20:38:14 just because of leap years and my algorithm breaks there. I'm posting my code bellow if anyone knows how to help me?
#include<stdio.h>
int main()
{
int D, M, G, H, m, s;
unsigned int k;
unsigned int ks,km,kH,kG,kM,kD;
scanf("%d %d %d %d %d %d",&D,&M,&G,&H,&m,&s);
while(G<1900 || G>2015)
{
scanf("%d %d %d %d %d %d",&D,&M,&G,&H,&m,&s);
}
int month;
scanf("%u",&k);
if(M == 1)
{
month=31;
}
if(M == 2 && G%4==0 && G%100!=0 && G%400==0)
{
month=29;
}
if(M == 2 && G%4!=0 && G%100!=0 && G%400!=0)
{
month=28;
}
if(M == 3)
{
month=31;
}
if(M == 4)
{
month=30;
}
if(M == 5)
{
month=31;
}
if(M == 6)
{
month=30;
}
if(M == 7)
{
month=31;
}
if(M == 8)
{
month=31;
}
if(M == 9)
{
month=30;
}
if(M == 10)
{
month=31;
}
if(M == 11)
{
month=30;
}
if(M == 12)
{
month=31;
}
kG=k/31536000;
G=G+kG;
k=k-kG*31536000;
kM=k/(86400*month);
k=k-kM*86400*month;
kD=k/86400;
k=k-kD*86400;
kH=k/3600;
k=k-kH*3600;
km=k/60;
ks=k-km*60;
s=s+ks;
m=m+km;
H=H+kH;
M=M+kM;
while(s>=60)
{
m++;
s-=60;
}
while(m>=60)
{
H++;
m-=60;
}
while(H>=24)
{
D++;
H-=24;
}
while(D>month)
{
M++;
D-=31;
}
while(M>12)
{
G++;
M-=12;
}
printf("%d.%d.%4d. %02d:%02d:%02d",D,M,G,H,m,s);
return 0;
}
You should really use a decent library to do this. There are not only leap years; there is daylight savings time, and if you are asking about dates in the early last century, there will be a few calendar changes here and there, which you have no chance taking into account.
And at the very least you need to know the locale of the date, because daylight savings time is different from locale to locale.
That said, your logic for calculating which months are leap years is totally wrong. You need to learn the difference between && and ||. Try G = 2016 and M = 2: months (which is a misleading variable name for days in the month) will not be set at all.
Seems like here, you have 31 days in every month:
while(D>month)
{
M++;
D-=31;
}
And, unrelated to this problem, you should also check the rules for leap years and the year 2000 (which is an exception to an exception).
Condition for leap year
if the year is divisible by 100 it must be divisible by 400 to be leap year;
and if the year is not divisible by 100 it would be leap year only if the year is divisible by 4;
i.e,
if (((year%100==0) && (year%400==0)) || ((year%100!=0) && (year%4==0))
code to find leap years in a range:
int leapyears(int start_year, int ending_year)
{
int k=0;
while (start_year<=ending year)
{
//&& operator have higher preference than ||
if (start_year%4==0 && start_year%100!=0 || start_year%400==0)
k++;
start_year++;
}
return k;
}
im doing a vending machine simulator. I need to only accept the real coins like (0.05€ / 0.10€ / 0.20€ etc...) i dont want 0.25€ for example.
code1:
if(coin!= 0.05|| coin!= 0.10 || coin!= 0.20 || coin!= 0.50 || coin!= 1 || coin!= 2 )
printf("It isnt a coin");
else
return coin;
code2(tryed this too cause of flots):
if((aux-0.05 >= 0.001) || (aux-0.10 >= 0.0001)||(aux-0.20 >= 0.0001)||(aux-0.50 >= 0.0001)||(aux-1 >= 0000.1)||(aux-2 >= 0000.1))
printf("It isnt a coin");
else
insert_value += aux;
neither code 1 or 2 works... the code2 accepts 0.05 coin, but ignore the others
By declaring an array of acceptable coins, the cash slot on your vending machine can be easily modified to accept 1 or 2 cent coins or a future €5 coin.
#include <stdio.h>
#include <stdlib.h>
int acceptable [] = {5, 10, 20, 50, 100, 200};
// return 1 if acceptable coin
int test_coin (int cents) {
int num_accept = sizeof(acceptable) / sizeof(int);
int index;
for (index=0; index<num_accept; index++)
if (cents == acceptable[index])
return 1;
printf ("Unacceptable coin\n");
return 0;
}
int main()
{
int cents;
char str [10];
do {
printf ("Enter coin value in cents ");
*str = 0;
fgets (str, 10, stdin);
cents = atoi(str);
} while (!test_coin(cents));
printf("You inserted %d cents coin\n", cents);
return 0;
}
You need to be using ANDs, not ORs in your clauses.
For example: coin != 1 || coin != 2 is always true.