online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <iostream> #include <string> int count(long n) //cout the number of integer digits { if (n == 0) return 0; return 1 + count(n / 10); } std::string trunc(float num, int digits) //remove the excess decimal places { std::string str = std::to_string(num).substr(0, digits + 1); if (str.find('.') == std::string::npos || str.back() == '.') { str.pop_back(); } return str; } int main() { float num = 12348.567890; long num2 = num; std::cout << trunc(num, count(num2) + 2) << '\n'; //2 is number of decimal places }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue