update:
Thanks to @Evg from the comments, I realized that the code works as fine when I compiled&runned from another IDE.
Not working IDE(CodeBlocks) compile log:
g++.exe -c "Y:\ODTÜ DERS VS\EE - 4\441 DATA STRUCTURES\HW-1\hw1_q1.cpp" -o "Y:\ODTÜ DERS VS\EE - 4\441 DATA STRUCTURES\HW-1\hw1_q1.o"
g++.exe -o "Y:\ODTÜ DERS VS\EE - 4\441 DATA STRUCTURES\HW-1\hw1_q1.exe" "Y:\ODTÜ DERS VS\EE - 4\441 DATA STRUCTURES\HW-1\hw1_q1.o"
Process terminated with status 0 (0 minute(s), 0 second(s))
Working IDE(Dev-Cpp) compile log:
- Compiler Name: TDM-GCC 4.9.2 64-bit Release
Processing C++ source file...
--------
- C++ Compiler: C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\g++.exe
- Command: g++.exe "Y:\ODTÜ DERS VS\EE - 4\441 DATA STRUCTURES\HW-1\hw1_q1.cpp" -o "Y:\ODTÜ DERS VS\EE - 4\441 DATA STRUCTURES\HW-1\hw1_q1.exe" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib" -static-libgcc
Compilation results...
I am trying to get all digits separately of a number, and I am doing this with the below code, though it has some irrelevant parts, I think it is clear.
void IntBoard::writeIntegerToRow(int integer, int row) {
int currentDigit = 0;
if(integer < 0 || integer > (pow(10, DigitCount) - 1)){
std::cerr << "ERROR: This number either does not fit into our Integer Board or is negative!\n" ;
return;
}
for (int i=0; i<DigitCount; i++){
std::cout << "INTEGER FOR THIS CYCLE : " << integer << std::endl;
currentDigit = integer / pow(10, DigitCount-1-i);
std::cout << "CURRENT DIGIT FOR THIS CYCLE : " << currentDigit << std::endl;
setValueOfDigit(currentDigit, row, i);
integer -= currentDigit * pow(10, DigitCount-1-i);
std::cout << "The value that will be omitted from INTEGER : " << currentDigit * pow(10, DigitCount-1-i) << std::endl;
std::cout << "INTEGER FOR NEXT CYCLE : " << integer << "\n" << std::endl;
}
}
DigitCount
is 4, defined as global const int. Now, the above code works well for 2 or 3 digit numbers, however, when I try any 4 digit number, the decomposed number is always 1 less than the actual one. I really could not understand why. The output of the above one for the below call is:
ib.writeIntegerToRow(1453, 2);
THE OUTPUT:
INTEGER FOR THIS CYCLE : 1453
CURRENT DIGIT FOR THIS CYCLE : 1
The value that will be omitted from INTEGER : 1000
INTEGER FOR NEXT CYCLE : 452
INTEGER FOR THIS CYCLE : 452
CURRENT DIGIT FOR THIS CYCLE : 4
The value that will be omitted from INTEGER : 400
INTEGER FOR NEXT CYCLE : 52
INTEGER FOR THIS CYCLE : 52
CURRENT DIGIT FOR THIS CYCLE : 5
The value that will be omitted from INTEGER : 50
INTEGER FOR NEXT CYCLE : 2
INTEGER FOR THIS CYCLE : 2
CURRENT DIGIT FOR THIS CYCLE : 2
The value that will be omitted from INTEGER : 2
INTEGER FOR NEXT CYCLE : 0
As you can see, the problem is at the first cycle. I really did not understand why 1453-1000
evaluates to 452
. And this happens for all 4 digit numbers. The remaining cycles look working well. What do I miss?
EXAMPLE OF WORKING CASE:
for the function call
ib.writeIntegerToRow(453, 2);
THE OUTPUT:
INTEGER FOR THIS CYCLE : 453
CURRENT DIGIT FOR THIS CYCLE : 0
The value that will be omitted from INTEGER : 0
INTEGER FOR NEXT CYCLE : 453
INTEGER FOR THIS CYCLE : 453
CURRENT DIGIT FOR THIS CYCLE : 4
The value that will be omitted from INTEGER : 400
INTEGER FOR NEXT CYCLE : 53
INTEGER FOR THIS CYCLE : 53
CURRENT DIGIT FOR THIS CYCLE : 5
The value that will be omitted from INTEGER : 50
INTEGER FOR NEXT CYCLE : 3
INTEGER FOR THIS CYCLE : 3
CURRENT DIGIT FOR THIS CYCLE : 3
The value that will be omitted from INTEGER : 3
INTEGER FOR NEXT CYCLE : 0