Hello, can someone please explain what is wrong with my code/calculation? With my calculations on my calculator the values should be right, but it doesn't output right.
What is the point of all that math? What are you trying to do (in English and not in code)?
I have a feeling you might want to use the modulus operator %.
Please use more meaningful variable names.
1 2 3 4 5 6 7
#include <iostream>
int main()
{
int input_day = 43;
int week_day = input_day % 7;
std::cout << week_day << std::end;
}
Sorry for the lack of variable names. But the modulus operator % worked, thanks.
I was trying to calculate the day by inputting day of the year (1-365), I excluded 1-7. The 1. of January is Monday.
So the theory was, you take a number divide by 7 and subtract the first number, so leaving only number after decimal point. Then multiply by 7 and you get number from 0 to 6 and it reflects the day name.
Yep, modulus is definitely the way to go here. lastchance's code is very nice. But just for your knowledge, you can avoid working with decimal (floating point) numbers by doing a - b * floor(a / b), which is equivalent to a - b * (a / b) if a and b are integers (integer division is floor division for n >= 0).
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int main()
{
// you should use %
// this is to just show what it's mathematically equivalent to, for positive numbers
int a = 43;
int b = 7;
std::cout << a % b << std::endl;
std::cout << a - b * (a / b) << std::endl;
}