You are using a version without Ads of this website. Please, consider donating:

### Calculation Function?

Below is my code for a calendar program that I'm trying to write. The only conditional statements I haven't written yet are the conditional statements to find the difference between the two dates. The first set of conditions is to prevent input errors. Of course the program won't terminate like I want it to, but meh, I'll deal with that later. What I'm wondering is if I can put the difference between the dates in a separate function than the user input? For example, I'll have the conditional statements that state if a user inputs the wrong numerical value for a month e.g 13, then the program terminates. Then have another function below that to state how the computation will be handled.

The reason I want to do this is so that my code is more organized.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` `````` #include #include #include using namespace std; void graphical_welcome(); void printInstructions(); void userInput(int m1, int d1, int y1, int m2, int d2, int y2); int main() { // Using an array, I numbered each month to have certain amount of corresponding // days. January = 31, February = 28, etc. int days_in_months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int d1, d2; int m1, m2; int y1, y2; int year_diff, day_diff; int month_total; int reg_years = 365; // The block above defines all variables for the program // Display the Welcome Message and program instructions graphical_welcome(); printInstructions(); // Function to prompt the user for input userInput(m1, d1, y1, m2, d2, y2); cin.ignore(); system("pause"); return 0; } void graphical_welcome() { cout << "|----------------------------------------------|" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "| Welcome to the Calendar |" << endl; cout << "| Dates Calculator |" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "|----------------------------------------------|" << endl; cout << "\n"; } void printInstructions() { cout << "When prompted, please enter the numeric value " << endl; cout << "of the month, day, and year you wish to " << endl; cout << "find the difference of." << endl; cout << "\n"; } void userInput(int m1, int d1, int y1, int m2, int d2, int y2) { cout << "The First Calendar Date Request" << endl; cout << "-------------------------------" << endl; cout << "\n"; // Month combined with a conditional statement to prevent wrong input cout << "Please enter the month: "; cin >> m1; if(m1 > 12 || m1 <= 0) { cout<<"Incorrect day entered"<> d1; if(d1 > 31 || d1 <= 0) { cout<<"Incorrect Month entered"<> y1; if(y1 > 9999 || y1 < 0) { cout<<"Incorrect Year Entered"<> m2; if(m2 > 12 || m2 <= 0) { cout<<"Incorrect day entered"<> d2; if(d2 > 31 || d2 <= 0) { cout<<"Incorrect Month entered"<> y2; if(y2 > 9999 || y2 < 0) { cout<<"Incorrect Year Entered"<
`void userInput(int m1, int d1, int y1, int m2, int d2, int y2);`
Doesn't actually take any arguments so it could just be
`void userInput();`
and then you could just declare the variables inside userInput.

The function you could use for computation could be
`void Calculation(int m1, int d1, int y1, int m2, int d2, int y2);`
and would take these as args and use them itself.
The function call would have to be inside userInput.

I think this is what you were asking
hope it helps :)
Oh awesome! Thanks Jidder. I'll definitely do that. And it's exactly what I'm asking. I really want to have a function for each operation. It just seems easier to debug.

I thought the cin.ignore(); would terminate the program preventing inputs, but all it does is say "Incorrect month entered" then it'll go on to ask for input of the day.

Thanks loads though Jidder. This will put me on the path that I'm looking for.
Okay, here's the updated version. Although since I can't figure out the argument portion of it, I'm stuck with this. I've got everything that I want laid out, but I'm having trouble with the arguments ironically LOL.. Still hoping for some help on this. It's probably something small, but I can't find it :(

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409`` ``````#include #include #include using namespace std; // Function Prototypes void graphical_welcome(); void printInstructions(); void userInput(); void dateDiff(int m1, int m2, int d1, int d2, int y1, int y2); int main() { // Display the Welcome Message and program instructions graphical_welcome(); printInstructions(); // Function to prompt the user for input userInput(); //Output the results dateDiff(); cin.ignore(); system("pause"); return 0; } void graphical_welcome() { cout << "|----------------------------------------------|" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "| Welcome to the Calendar |" << endl; cout << "| Dates Calculator |" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "|----------------------------------------------|" << endl; cout << "\n"; } void printInstructions() { cout << "When prompted, please enter the numeric value " << endl; cout << "of the month, day, and year you wish to " << endl; cout << "find the difference of." << endl; cout << "\n"; } void userInput() { //Declare the variables for input int d1, d2; int m1, m2; int y1, y2; cout << "The First Calendar Date Request" << endl; cout << "-------------------------------" << endl; cout << "\n"; // Month combined with a conditional statement to prevent wrong input cout << "Please enter the month: "; cin >> m1; if(m1 > 12 || m1 <= 0) { cout<<"Incorrect day entered"<> d1; if(d1 > 31 || d1 <= 0) { cout<<"Incorrect Month entered"<> y1; if(y1 > 9999 || y1 < 0) { cout<<"Incorrect Year Entered"<> m2; if(m2 > 12 || m2 <= 0) { cout<<"Incorrect day entered"<> d2; if(d2 > 31 || d2 <= 0) { cout<<"Incorrect Month entered"<> y2; if(y2 > 9999 || y2 < 0) { cout<<"Incorrect Year Entered"< y2) { year_diff = (y1 - y2) * (reg_year) + 2; } else { year_diff = (y2 - y1) * (reg_year) + 2; } if(m2 > m1) { if(days_in_months[m1 - 1] > days_in_months[1]) { --year_diff; } } } else { if(y1 > y2) { year_diff = (y1 - y2) * (reg_year) + 1; } else { year_diff = (y2 - y1) * (reg_year) + 1; } if(m1 > m2) { if(days_in_months[m2 - 1] > days_in_months[1]) { --year_diff; } } } } else { if(y1 > y2) { year_diff = (y1 - y2) * (reg_year); } else { year_diff = (y2 - y1) * (reg_year); } } } if(m1 == m2) { months_total = 0; } else { if(m1 > m2) { for(int i = (m1 - 1); i > (m2 - 1); i--) { static int months_total_temp = 0; months_total_temp += days_in_months[i]; months_total = months_total_temp; } } else { for(int i = (m1 - 1); i < (m2 - 1); i++) { static int months_total_temp = 0; months_total_temp += days_in_months[i]; months_total = months_total_temp; } } } int days_total; if (d1 == d2) { day_diff = 0; days_total = (year_diff + months_total) - day_diff; } else { if(d1 > d2) { day_diff = d1 - d2; days_total = (year_diff + months_total) - day_diff; } else { day_diff = d2 - d1; days_total = (year_diff + months_total) + day_diff; } } if(y1 == y2) { } else { if(y1 > y2) { for(int i = (y2 + 1); i < y1; i++) { if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { cout<
`dateDiff(int m1, int m2, int d1, int d2, int y1, int y2);`
will have to be called within userInput after all the variables have been assigned values.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: