-1

I have a simple converting function however the variable is changing.

void convert_print(std::string type,int num)
{
   if(type=="oct"){
       std::cout << num << " oct value is: " << std::oct << num << endl;
   }
   else if(type == "hex"){
      std::cout << num << " hex value is: " << std::hex << num << endl;
   }
}

int main()
{
   int num = 30;
   convert_print("hex",num);
   convert_print("oct",num);
}

Prints

30 hex value is: 1e 
1e value is: 30

Why is num getting changed to 1e when I call the function again. I never reassigned num

user3525290
  • 1,557
  • 2
  • 20
  • 47

1 Answers1

4

You haven't told cout to reset the formatting after your last call. This question describes how to restore it. Restore the state of std::cout after manipulating it

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445