I've got a program, which gets numbers n(quantity of blocks) and r(volume), and after it gets n sizes in format firstsize secondsize thirdsize (e.g. 1 1 1) so the overall simplest input is:
1 1 1 1 1 which in theory should return 1. But it doesn't.
I have the following code:
#include <iostream>
#include <cstdint>
#include <vector>
using namespace std;
struct Size{
long long w=0,h=0,d=0;
};
istream& operator>>(istream& istr, Size& rval){
istr >> rval.w >> rval.h >> rval.d;
return istr;
}
long long calcMass(Size s, int r){
long long res = s.d*s.h*s.w*r;
cout << "Gained:" << res << '\n';
cout << "Got: sizes:{" << s.d << ' ' << s.h << ' ' << s.w << "}, p = " << r << '\n';
return res;
}
int main(){
int n;
long long sum = 0;
Size s;
uint8_t r; // Condition is that r<100
cin >> n >> r;
for(int i=0; i<n; i++){
cin >> s;
sum += calcMass(s,r);
}
cout << sum;
}
Actually, I got this problem solved by changing type of the r variable in main() from uint8_t to int, but the problem is, I don't understand why it didn't work whilst being uint8_t. When I try the following code:
cout <<'\n' << static_cast<long long>(static_cast<int>((static_cast<uint8_t>(1))));
It gives me 1.
So, I decided to inspect the situation with uint8_t r in main(). uint8_t r = 1 in main() somehow becomes the int r = 49 in calcMass() and I do not undertand why.
I know about the rules of implicit conversion, that before the operation variables smaller in size than int are converted to int, and then the "smaller in size" variable is converted to bigger one (e.g. int r to long long in calcMass(Size,int) function?), but I do not understand the thing: why do uint8_t r = 1 becomes int r = 49?
Also, I tried changing the type of r in calcMass() from int to uint8_t. As supposed, uint8_t r = 1 in main() becomes uint8_t r = 1 in calcMass(), but the result of this function is still 49!
These I used to run the program:
Operating System: Windows 10 Enterprise LTSC
System Type: 64bit
C++ compiler: MinGW
I would be pleased if somebody could explain why explicit conversion of uint8_t to long long isn't equal to the imlpicit one when passing it to calcMass() and doing operations with long long using it.