Calculation problem

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
  #include <cmath>
int main(){

	int k;
	std::cout << "Enter number 8-365: " << std::endl;
	std::cin >> k;

        double a, b, c, d;
	d = k;
	a = d / 7;
	b = floor(a);
	c = a - b;
	a = int(c * 7);

	if (a == 0) { std::cout << "Sunday" << std::endl; }
	else if (a == 1) { std::cout << "Monday" << std::endl; }
	else if (a == 2) { std::cout << "Tuesday" << std::endl; }
	else if (a == 3) { std::cout << "Wednesday" << std::endl; }
	else if (a == 4) { std::cout << "Thursday" << std::endl; }
	else if (a == 5) { std::cout << "Friday" << std::endl; }
	else if (a == 6) { std::cout << "Saturday" << std::endl; }

        system("pause");
}
Do not compare floating point values directly (==, !=). 0.0000000001 is still not 0. So cast a to int.
I have tried to cast a to int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>

int main(){

	int k;
	std::cout << "Enter number 8-365: " << std::endl;
	std::cin >> k;

        double a, b, c, d;
	d = k;
	a = d / 7;
	b = floor(a);
	c = a - b;
	a = int(c * 7);
        int g = a;

	if (g== 0) { std::cout << "Sunday" << std::endl; }
	else if (g == 1) { std::cout << "Monday" << std::endl; }
	else if (g== 2) { std::cout << "Tuesday" << std::endl; }
	else if (g== 3) { std::cout << "Wednesday" << std::endl; }
	else if (g== 4) { std::cout << "Thursday" << std::endl; }
	else if (g== 5) { std::cout << "Friday" << std::endl; }
	else if (g== 6) { std::cout << "Saturday" << std::endl; }

        system("pause");
}


But now it gives me always Sunday output.
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;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
   int day, Jan1st = 1;

   cout << "Input a day in 2018: ";   cin >> day;
   cout << "It's a " << days[ ( day - 1 + Jan1st ) % 7 ];
}
Input a day in 2018: 16
It's a Tuesday
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.
Last edited on
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;
}
Registered users can post here. Sign in or register to post.