#How to take input of dates without any libraries?
64 messages · Page 1 of 1 (latest)
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.
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
You take it in as a string and then you parse the string.
ok is it complicated though?
because so far we have covered these things, and I assume we are only allowed to use these: basics, if statements, real numbers.
It's not complicated absolutely speaking.
assume I am only allowed to use the basic things, how would I do it?
I am thinking of this:
- Dissect the input into day month and year
- Add the difference to the day
- If the day goes over 30 then add one to the month
- 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
"without any libraries" - what about the standard library?
Because there is date manipulation in it, going back all the way from C
But doing it manually isn't that hard. Yes, you need to track the number of days in a month, but it's not crazy hard
That's roughly the algorithm, the biggest thing in all of this is really just getting the date input.
I'm mainly wondering how exactly Id do that
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
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.
You could start with assuming 31 days in each month. And when that works, replace 31 with the proper amont
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;
}```
yes, but how do I make it so for each individual month?
the above gives me the month and day, and all I have to do is add the difference
You write code to determine the number of days in a month.
but if June has 30 days and July has 31, how does the code tell if it has 30 or 31 days?
this is brilliant
It can be literally: "If it's June then it's 30 days, if it's July then it's 31 days".
also is there a way to make this have colours
Add cpp to after the first three backticks.
if (month == 1 || month == 3 || month == 5 .....) days = 31; else if (month == 4 || ....) days = 30;, and so on
#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;
}
}```
it seems to have shown in the output
Enter after cpp.
oh nice
is it ok to use booleans or should I make (yet another varibale) equal to the number of days in the month
February.
What of it?
February has neither 30 nor 31 days.
28 or 29.
but what of the days in each month, should I just make another variable for it
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.
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?
Because your formula is wrong.
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?
It's similar to getting past the end of the month, just in reverse.