C ++ prints integer octal decimal hexadecimal and prints logical Boolean type

    Printing an integer in octal, decimal, and hexadecimal in C ++ is very convenient. You do not need to define other functions or methods. You can directly use the keywords oct, dec, and hex. In addition, you can print the boolean type by using the keyword boolalpha. Code:

#include <iostream>
using namespace std;
int main(){
	int x = 0;
	cout<<"请输入一个整数:"<<endl;
	cin>>x;
	//oct 表示输出八进制
	cout<<oct<<x<<endl;
	//dec 表示输出十进制
	cout<<dec<<x<<endl;
	//hex 表示输出十六进制
	cout<<hex<<x<<endl;
	bool y = 0;
	cout<<"请输入一个布尔类型值(1或者0)"<<endl;
	cin>>y;
	//boolalpha 表示输出逻辑类型
	cout<<boolalpha<<y<<endl;
	return 0;
}

    Run the above code, enter the integer 10 according to the prompt, and then print, enter the Boolean type 1 again according to the prompt, and then view the print result:

     

    Among them, oct means printing octal of integer, dec means decimal of integer, hex means hexadecimal of integer, hexadecimal of 10 is just a, and logical boolean value can be printed through boolalpha. 

    The above content refers to the C ++ introductory video tutorial on the MOOC online, mainly to deepen the understanding of the oct, dec, hex, boolalpha keywords.

Published 529 original articles · praised 287 · 1.47 million views

Guess you like

Origin blog.csdn.net/feinifi/article/details/104729046