按回车键后,程序没有输出任何结果.为什么会这样
After I have pressed ENTER, the program did not output any results. Why is this happening?
我是C++新手。我在网上看到了这个代码。按回车键后,程序没有输出任何结果。 为什么会这样?有人可以帮我吗?提前感谢您的任何帮助!
int main(){
const string hexdigits = "0123456789ABCDEF";
cout << "enter a series of numbers between 0 and 15 separated by spaces. Hit ENTER when finished: "
<< endl;
string result;
string::size_type n;
while(cin >> n){
if(n < hexdigits.size()){
result += hexdigits[n];
}
}
cout << "your hex number is: " << result << endl;
}
我输入了:
12 0 5 15 8 15
输入循环将继续读取整数,直到输入流关闭,或者遇到无法解析为整数的内容。 每个值由任何空格(包括换行符)分隔。
如果你想为每一行输入输出新的东西,你可以使用 std::getline
首先读取字符行,然后从std::istringstream
读取:
string line;
while( getline( cin, line ) )
{
istringstream iss( line );
string result;
string::size_type n;
while(iss >> n){
if(n < hexdigits.size()){
result += hexdigits[n];
}
}
cout << "your hex number is: " << result << endl;
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何在不产生任何垃圾的情况下获得C中的像素
- 如何通过键和值以及在C 中的结果映射中比较两个地图?我们有任何STL API吗?
- 该程序应该创建一个带有结果的输出文件,但文件中没有任何内容
- 通过Qt(mac - OSX)进行的AWS查询没有得到任何结果
- 提升精神,提升任何意想不到的结果
- 缓存错过压力测试:令人惊叹的结果.任何解释
- C void*任何类型实现都会返回奇怪的结果
- PyRun_String在出现任何错误后停止将结果发送到stdout
- 按回车键后,程序没有输出任何结果.为什么会这样
- 在c++中,任何更好的方法都可以做到这一点,计算谜题的解决方案(49!结果,49个循环)
- "Truncation from int to char"不产生任何结果
- 查询后出现MongoDB套接字异常,未返回任何结果
- 标准中是否有任何引用支持以下结果?
- 查找可能不返回任何结果的最佳匹配的算法