C++ While Loop: Hex & Oct, & Dec
C++ While Loop: Hex & Oct, & Dec
我目前正在编写一个代码:
取2个十进制数,然后在紧凑的查看表中以八进制、十六进制以及原始格式显示。
目前,我被限制为while
循环。代码看起来像:
cout << "Enter in two hexadecimal numbers that will be the beginning and endn";
while (num1 <= num2){
// takes 2 hexadecimal inputs
cin >> hex >> num1; cin >> hex >> num2;
cout << "DecimaltOctaltHexadecimaln";
cout << "***********************************n";
虽然不多,但这已经折磨了我一段时间了。我目前不知道如何处理这个问题。
注意:我不知道该增加什么,也不知道是否需要另一个变量。如果你能给我建议,或者给我指明正确的方向,那就太好了。
首先,您需要获取值,然后使用以下内容从num1
循环到num2
:
cout << "Enter in two hexadecimal numbers that will be the beginning and endn";
cin >> hex >> num1; cin >> hex >> num2; //takes 2 hexadecimal inputs
while (num1 <= num2) {
// ... do stuff
++num1;
}
然后,您可能只需要io操纵器作为输入,以在循环的"做事"部分的不同基础上输出num1
。
有一件事-您可能想在读取值后开始循环
cout << "Enter in two hexadecimal numbers that will be the beginning and end" << endl;
cin >> hex >> num1; cin >> hex >> num2; //takes 2 hexadecimal inputs
while (num1<=num2)
{
cout << num1 << " " << oct << num1 << " " << hex << num1 <<endl;
num1++;
}
我可能会做这样的事情。。。
#include <iomanip>
.........
int num1 = 0;
int num2 = 0;
cout << "Enter number 1: ";
cin >> num1;
cout << "Enter number 2: ";
cin >> num2;
if(num1 > num2){
cout << "number 1 needs to be smaller than number 2; exiting...";
return 0;
}
cout << "DecimaltOctaltHexadecimaln";
cout << "***********************************n";
while(num1 <= num2){
cout << dec << num1 << " " << oct << num1 << " " << hex << num1 << endl;
num1++;
}
查看c++的ios标志
相关文章:
- 将十六进制转换为 DEC
- 在编写自己的流运算符时,如何检查当前的 ostream dec/hex 模式?
- 运行简单的.oct文件时Octave崩溃,我的代码出了什么问题?
- 如何将字符串形式的日期 ( "Dec 25, 2012" ) 转换为一组整数 (12/25/12)?
- C++时间库和八度.oct文件
- 努力理解一个以十六进制、十进制、bin 和 dec 计算数字总和的程序
- 十六进制、二进制和 oct 到 dec 转换器无法正常工作C++
- 我如何在 C++ 中将变量从十六进制转换为 dec
- DEC to BIN using for and
- 使用 mkoctfile 与 Octave 和 Visual Studio 2010 编译器编译 .mex 或 .oct
- 如何在Linux中编译DEC UNIXv4.0应用程序源代码
- oct文件中NDArray的乘法运算
- 字符串流:dec int到hex到car的转换问题
- DTW算法OCT文件
- std::dec仍然将内存地址输出为十六进制
- 如何迭代Quad/Oct树
- ThreadSanitizer说我的Atomic Inc / Dec有数据竞争,误报
- 将 ASCII dec 转换为字符时出现问题
- 如何使用 boost.log 以 dec 格式打印进程 ID 和线程 ID
- C++ While Loop: Hex & Oct, & Dec