Форум » C/C++ для начинающих (C/C++ for beginners) » writing structures » Ответить

writing structures

lordcideon: If anyone could help me with this, that would be great. I am not really sure what to do. My problem states: Pass the address of a Date structure variable to the days() function. Write a program in main() than inputs the month, day, and year from the user, writing the inputs into a Date structure variable, call your days() function and display the result. Use the following date structure: struct date { int month; int day; int year; }; In writing this function, use the convention that all years are 360 days and each month consist of 30 days. The function should return the number of days for any date structure passed to it.

Ответов - 4

Сыроежка: As I understand you need to write function days() declared like [pre2] int days( date *d ); [/pre2] Or if it is a C program then [pre2] int days( struct date *d ); [/pre2]

lordcideon: yeah sorry, it is C programming. I forgot to mention that.

lordcideon: This is what i have so far, not sure where to take it from here struct date { int month; int day; int year; }; int main() { int monthMain, dayMain, yearMain; //declaring the int variables int days; int yearCalc; printf("Enter a Month: "); //requesting user to input the month scanf("%d", &monthMain); //accepting the user input for month printf("Enter a Day: "); //requesting user to input the day scanf("%d", &dayMain); //accepting the user input for day printf("Enter a Year: "); //requesting user to input the year scanf("%d", &yearMain); //accepting the user input for year yearCalc = 1900 * 360; yearMain = (yearMain * 360) - yearCalc; if(monthMain == 1) { monthMain = 0; days = monthMain + dayMain + yearMain; } if(monthMain == 2) { monthMain = 30; days = monthMain + dayMain + yearMain; } if(monthMain == 3) { monthMain = 60; days = monthMain + dayMain + yearMain; } if(monthMain == 4) { monthMain = 90; days = monthMain + dayMain + yearMain; } if(monthMain == 5) { monthMain = 120; days = monthMain + dayMain + yearMain; } if(monthMain == 6) { monthMain = 150; days = monthMain + dayMain + yearMain; } if(monthMain == 7) { monthMain = 180; days = monthMain + dayMain + yearMain; } if(monthMain == 8) { monthMain = 210; days = monthMain + dayMain + yearMain; } if(monthMain == 9) { monthMain = 240; days = monthMain + dayMain + yearMain; } if(monthMain == 10) { monthMain = 270; days = monthMain + dayMain + yearMain; } if(monthMain == 11) { monthMain = 300; days = monthMain + dayMain + yearMain; } if(monthMain == 12) { monthMain = 360; days = monthMain + dayMain + yearMain; } printf("the date you entered = %d days", days); return 0; } int days( struct date *d );


Сыроежка: All these calculations that you do in main you need to transfer in function days. Also instead of numerous if statements you could define an array with number of days before a given month.



полная версия страницы