I have the following bit of code to return the scaled down value and its respective unit of measurement:
enum class convert_mode {up_metric, low_metric, metric, bit_metric, imperial}
std::pair <double, std::string> auto_range (uint64_t x, int factor1 = 1000, int factor2 = 10){
double val = (double) x;
int i = 0;
while (val >= factor1 ){
val = val / factor1;
i++;
}
switch (i) {
case 0 : return std::make_pair( digit_rm( val, factor2 ) ,"_" );
case 1 : return std::make_pair( digit_rm( val, factor2 ) ,"K" );
case 2 : return std::make_pair( digit_rm( val, factor2 ) ,"M" );
case 3 : return std::make_pair( digit_rm( val, factor2 ) ,"G" );
case 4 : return std::make_pair( digit_rm( val, factor2 ) ,"T" );
case 5 : return std::make_pair( digit_rm( val, factor2 ) ,"P" );
}
return std::make_pair(0,"_");
}
that returns me the following error at compilation:
get_info.cpp:41:33: error: invalid declarator before ‘auto_range’
41 | std::pair <double, std::string> auto_range (uint64_t x, int factor1 = 1000, int factor2 = 10){
| ^~~~~~~~~~
get_info.cpp: In function ‘int main(int, char**)’:
get_info.cpp:270:14: error: ‘auto_range’ was not declared in this scope
270 | auto x1 = auto_range(PC.get_memory_info(), factor1, factor2);
| ^~~~~~~~~~
As a side-note the code did compile correctly previously and I inspired this method from the following answer : Returning multiple values from a C++ function
Edit:
- The function should work like this:
- auto_range ( number_to_reduce, reduction_per_unit, reduction_of_digits )
- returns pair< reduced_number, unit_of_measurement >
- digit_rm is a function to remove the extardigits remained after the comma (12345m => 12.345Km => digit_rm(12.345,10) => 12.3Km).
double digit_rm (double val, int factor = 100){
return (double)((int)(val*factor))/factor;
}
- may
#include
's in this case are the following :
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip> // std::seprecision, std::fixed
#include <math.h>
#include <sys/statvfs.h>
#include <cstring>
#include <utility>
// See: https://man7.org/linux/man-pages/man2/sysinfo.2.html
#include <sys/sysinfo.h>
I am using a Linux machine (Ubuntu 20.04.2 LTS x86_64), if that matters.
The
enum class
is a part of a future modification, if that is worth mentioning.Here is an example :
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip> // std::seprecision, std::fixed
#include <math.h>
#include <sys/statvfs.h>
#include <cstring>
#include <utility>
// See: https://man7.org/linux/man-pages/man2/sysinfo.2.html
#include <sys/sysinfo.h>
double digit_rm (double val, int factor = 100){
return (double)((int)(val*factor))/factor;
}
enum class convert_mode {up_metric, low_metric, metric, bit_metric, imperial}
std::pair < double, std::string > auto_range (uint64_t x, int factor1 = 1000, int factor2 = 10){
double val = (double) x;
int i = 0;
while (val >= factor1 ){
val = val / factor1;
i++;
}
switch (i) {
case 0 : return std::make_pair( digit_rm( val, factor2 ) ,"_" );
case 1 : return std::make_pair( digit_rm( val, factor2 ) ,"K" );
case 2 : return std::make_pair( digit_rm( val, factor2 ) ,"M" );
case 3 : return std::make_pair( digit_rm( val, factor2 ) ,"G" );
case 4 : return std::make_pair( digit_rm( val, factor2 ) ,"T" );
case 5 : return std::make_pair( digit_rm( val, factor2 ) ,"P" );
}
return std::make_pair(0,"_");
}
int main (int argc, char **argv)
{
double x = 1234567; //say this is Hz
auto y = auto_range(x);
std::cout << y.first << " " << y.second << "Hz" << std::endl; //Should return 1.2Mhz
return 0;
}