Calling a function with pointers - c

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.

Related

Please help. Function not printing [closed]

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')

While Loop to continually check user input for integer

I know this question has been asked before and I am sorry but I cannot find a solution for my code. I am a new student to the C language and my problem is that I am trying to create a while loop that continually asks the user to update the data if they enter non integers. I have looked into the isdigit() function and I think that would work. I just can't seem to make it work for my code, as in whenever I try creating a while loop with isdigit as its condition, I either get a infinite loop, or my isdigit returns values that I was not expecting. I think the problem could be that my scanf() is scanning three integers separated by '/' but I am not sure if that is where the problem is. Any help would be appreciated.
Here is the code in question
#include <stdio.h>
#include <ctype.h>
int calcAge(int a, int b, int c, int d, int e, int f);
int maxHeart(int a);
int heartRangeLow(int a);
int heartRangeHigh(int a);
int main(void) {
int birthMonth = 0, birthDay = 0, birthYear = 0, targetRangeLow = 0;
int month = 0, day = 0, year = 0, age = 0, heartRate = 0, targetRangeHigh = 0;
printf("What is your birthday?(written in MM/DD/YYYY)"
"\nHit Enter after you type it in.\n");
scanf_s("%d/%d/%d", &birthMonth, &birthDay, &birthYear);
printf("What is the date today?(written as MM/DD/YYYY)"
"\nHit Enter after you type it in.\n");
scanf_s("%d/%d/%d", &month, &day, &year);
age = calcAge(birthMonth, birthYear, birthDay, month, day, year);
heartRate = maxHeart(age);
targetRangeLow = heartRangeLow(heartRate);
targetRangeHigh = heartRangeHigh(heartRate);
printf("You are %d years old.\n", age);
printf("Your maximum heart rate is %d beats per minute.\n", heartRate);
printf("Your target-heart-range is %d"
" to %d beats per minute.\n", targetRangeLow, targetRangeHigh);
system("pause");
return 0;
}
int calcAge(int a, int b, int c, int d, int e, int f)
{
int age;
age = ((f * 10000 + d * 100 + e) - (b * 10000 + a * 100 + c)) * .0001;
return age;
}
int maxHeart(int a)
{
int heartRateMax;
heartRateMax = 220 - a;
return heartRateMax;
}
int heartRangeLow(int a)
{
int lowTarget = a * .5;
return lowTarget;
}
int heartRangeHigh(int a)
{
int highTarget = a * .85;
return highTarget;
}
to create a while loop that continually asks the user to update the data if they enter non integers.
Make a helper function.
Read user input with fgets() into a buffer and then parse it for valid input with strtol() or sscanf(), is...(), etc. Return 1, 0, EOF depending on input.
// Untested code
// 1 success
// 0 bad input
// EOF end of file
#define DATE_STR_SIZE 80
int enter_date_mdy(char *prompt, int *m, int *d, int *y) {
if (prompt) {
fputs(prompt, stdout);
fflush(stdout);
}
char buffer[DATE_STR_SIZE];
if (fgets(buffer, sizeof buffer, stdin) == NULL) {
return EOF;
}
int n = 0;
sscanf(buffer, "%d /%d /%d %n", m, d, y, &n);
// If scan incomplete or extra junk at the end ....
if (n == 0 || buffer[n]) {
return 0;
}
// Additional tests as desired ....
if (*m < 1 || *m > 12 || *d < 1 || *d > 31) {
return 0;
}
return 1;
}
Sample use
int count;
do {
count = enter_date_mdy("What is your birthday?(written in MM/DD/YYYY)",
&birthMonth, &birthDay, &birthYear);
if (count == EOF) return(EXIT_FAILURE);
} while (count < 1);
do {
count = enter_date_mdy("What is the date today?(written as MM/DD/YYYY)",
&month, &day, &year);
if (count == EOF) return(EXIT_FAILURE);
} while (count < 1);
Tip: instead of "What is the date today?", research time(), mktime().

C - Pass structure variable from one function to another function

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*/

My loop won't finish

I want the user to choose when to finish inputting so i initialized " int finish". But the loop bypasses that.
I have tried while, do while loops but it doesn't do it for me
char snails[8][11];
int count;
int finish;
do
{
for (count = 0; count < 8; count++)
{
printf("Enter the snail's name:");
scanf("%10s", &snails[count]);
int timea;
int timeb;
int timec;
int timed;
printf("Enter %s 's time for the first leg:", snails[count]);
scanf("%d", &timea);
printf("Enter %s 's time for the second leg:", snails[count]);
scanf("%d", &timeb);
printf("Enter %s 's time for the third leg:", snails[count]);
scanf("%d", &timec);
printf("Enter %s 's time for the fourth leg:", snails[count]);
scanf("%d", &timed);
int timef = timea + timeb + timec + timed;
int time1 = timef / 60;
int time2 = timef % 60;
printf("%s finished in %d minutes and %d seconds \n", snails[count], time1, time2);
printf("Type 1 if you have finished inputting or 0 if you have not:");
scanf("%d", &finish);
}
} while (finish == 0);
return 0;
}
If your intention is to execute the loop up to 8 times, unless they indicate they are finished:
1.Remove the do while loop.
2.Do one of the following:
change the condition of the for look to:
count < 8 && finish != 1
or break from the for loop :
if (finish == 1) break;
then it will execute the for loop until either:
it does it 8 times
or they enter 1 when asked if finished

How can I avoid prompting the user multiple times for input?

I am creating a simple program which asks for user input in one function mark and then passes the input values to a second function characters for validation.
#include <stdio.h>
void marks(float *mark1, float *mark2, float *mark3);
void characters(float mark1, mark2, float mark3);
void marks(float *mark1, float *mark2, float *mark3) {
float firstMark, secMark, thirdMark;
printf("Enter mark1, mark2, and mark3: ");
scanf("%f,%f,%f", mark1, mark2, mark3);
while (mark1 < 0 || mark2 < 0 || mark3 < 0) {
printf("Error, marks can't be less than zero \n");
printf("Enter mark1, mark2, and mark3: ");
scanf("%f,%f,%f", mark1, mark2, mark3);
firstMark = *mark1;
secMark = *mark2;
thirdMark = *mark3;
}
}
void characters(float mark1, float mark2, float mark3) {
marks(&mark1, &mark2, &mark3);
if (mark1 < 50 || mark2 < 50 || mark3 < 50) {
printf("Really Bad! You have failed. \n");
}
else if (mark1 > 50 && mark2 > 50 && mark3 > 50) {
printf("You have passed. \n");
}
else {
printf("ERROR! \n");
}
}
int main(void) {
int i;
float mark1, mark2, mark3;
printf("Grade list \n");
marks(&mark1, &mark2, &mark3);
characters(mark1, mark2, mark3);
}
The problem is in characters when I call the mark it asks for user input, but I only want the program to ask user to enter marks in main.
How can I call marks in characters without the program asking for data input, so that I can use it's parameters to calculate values?
If you do not want to ask the user for the input twice, remove the second call to marks. The input has been collected for characters by main through calling marks, and passed through the three parameters; there is no need to call it the second time.
This will fix your problem:
void characters(float mark1, float mark2, float mark3) {
// No second call to marks
if (mark1 < 50 || mark2 < 50 || mark3 < 50) {
...
}
...
}

Resources