#How to take input of dates without any libraries?

64 messages · Page 1 of 1 (latest)

hoary dew
#

Hi, I need to take an input called difference that can range from -7 to 7. I then need to enter a date. The program is supposed to output the date plus the difference.

glass islandBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hoary dew
#

I have no clue where to start, and I'm not allowed to use AI. So my first question is how can I get an input of dates?

#

As a starter, im assuming that the year isnt a leap year, and all months have 30 days

latent pagoda
#

You take it in as a string and then you parse the string.

hoary dew
#

because so far we have covered these things, and I assume we are only allowed to use these: basics, if statements, real numbers.

latent pagoda
#

It's not complicated absolutely speaking.

hoary dew
#

assume I am only allowed to use the basic things, how would I do it?

#

I am thinking of this:

#
  1. Dissect the input into day month and year
  2. Add the difference to the day
  3. If the day goes over 30 then add one to the month
  4. If the month goes over 12, then add one to the year
#

would this be suitable?

#

It would soon get very complicated after considering different amount of days in each month

inner helm
#

"without any libraries" - what about the standard library?

#

Because there is date manipulation in it, going back all the way from C

inner helm
latent pagoda
hoary dew
inner helm
#

Determine the number of days in a month. You could use a bunch of ifs to do that, or try to come up with something more clever

latent pagoda
#

For my reminder app I would convert the date into an integer representation and then convert it back into day, month and year for display.

inner helm
hoary dew
#

well, its not really difficult to take in the date using elementary methods, this is what I have done so far, but only for the month and day:

#
#include <iomanip>
using namespace std;


int date, difference, first, second, third, fourth, temp, day, month;
int main() {
 cout << "Enter a difference:   ";
 cin >> difference;
 cout << "Type in a date in day / month format: ";
 cin >> date;


 first = date / 1000;
 temp = date / 100;

 second = temp % 10;
 temp = date / 10;
 third = temp % 10;

 fourth = date % 10;

 day = 10*first + second;
 month = 10*third + fourth;

}```
hoary dew
#

the above gives me the month and day, and all I have to do is add the difference

latent pagoda
#

You write code to determine the number of days in a month.

hoary dew
#

but if June has 30 days and July has 31, how does the code tell if it has 30 or 31 days?

hoary dew
latent pagoda
#

It can be literally: "If it's June then it's 30 days, if it's July then it's 31 days".

hoary dew
#

im embarrased that I didnt think of it lmao

#

ok so some sort of checker

#

how though 😭

hoary dew
latent pagoda
#

Add cpp to after the first three backticks.

inner helm
#

if (month == 1 || month == 3 || month == 5 .....) days = 31; else if (month == 4 || ....) days = 30;, and so on

hoary dew
#
#include <iostream>
#include <iomanip>
using namespace std;


int date, difference, first, second, third, fourth, temp, day, month;
bool days31;
int main() {
 cout << "Enter a difference:   ";
 cin >> difference;
 cout << "Type in a date in day / month format: ";
 cin >> date;


 first = date / 1000;
 temp = date / 100;

 second = temp % 10;
 temp = date / 10;
 third = temp % 10;

 fourth = date % 10;

 day = 10*first + second;
 month = 10*third + fourth;


if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    days31 = true;
}
if (month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
    days31 = false;
}
}```
hoary dew
latent pagoda
#

Add right after, without Enter.

#

```cpp

hoary dew
#

I did....

latent pagoda
#

Enter after cpp.

hoary dew
#

oh nice

#

is it ok to use booleans or should I make (yet another varibale) equal to the number of days in the month

latent pagoda
#

February.

hoary dew
#

What of it?

latent pagoda
#

February has neither 30 nor 31 days.

hoary dew
#

oh how many days in feb

#

I assumed we were ignoring that for now

latent pagoda
#

28 or 29.

hoary dew
#

but what of the days in each month, should I just make another variable for it

latent pagoda
#

Also you are going to need to use the number anyhow so it makes the most sense to come up with that number right away.

hoary dew
#

ah ok

#

just a min

#
#include <iostream>
#include <iomanip>
using namespace std;


int date, difference, first, second, third, fourth, temp, day, month, num_days_in_month, final_day, final_month;
bool days31;
int main() {
 cout << "Enter a difference:   ";
 cin >> difference;
 cout << "Type in a date in day / month format: ";
 cin >> date;


 first = date / 1000;
 temp = date / 100;

 second = temp % 10;
 temp = date / 10;
 third = temp % 10;

 fourth = date % 10;

 day = 10*first + second;
 month = 10*third + fourth;


if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    num_days_in_month = 31;
}
if (month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
    num_days_in_month = 30;
}

if (day + difference > num_days_in_month) {
    final_day = num_days_in_month - (day + difference);
    final_month = month + 1;
}
if (day + difference < num_days_in_month) {
    final_day = day + difference;
}
cout << "The final date in day / month format is:  " << final_day << " /" << final_month ;

}```
#

This is what I got so far

#

but it output this

#

how could it output a negative day?

latent pagoda
#

Because your formula is wrong.

hoary dew
#

if (day + difference > num_days_in_month) {
    final_day = (day + difference)- num_days_in_month;
    final_month = month + 1;
}
if (day + difference < num_days_in_month) {
    final_day = day + difference;
}
cout << "The final date in day / month format is:  " << final_day << " /" << final_month ;
#

Ah, this should do it.

#

Now, lastly, Im just wondering before i try myself and ado you all goodnight, how would I work the negative?

latent pagoda
#

It's similar to getting past the end of the month, just in reverse.