【发布时间】:2014-03-24 20:45:57
【问题描述】:
我在一个文本文件中有一些负数,都包含小数,我已经设法读取它们并将它们转换为整数来显示;
for (int i = 0; i < 1; i++)
{
std::string str = &(cmemblock[0]); //cmemblock is the .txt file
std::istringstream iss(str);
std::vector<int> numbers;
int number;
while (iss >> number) {
numbers.push_back(number); // collect inputs
cout << number << endl; //Displays Stored Value
}
if (!iss.eof()) {
cout << "Blank Space"<<endl; //Error message to detect blank space
}
}
我需要帮助创建一个循环来读取每个字符,例如,如果第一行是“-0.009876”,则应单独读取每个值,“-”、“0”“。”等等。这样做的原因是因为这个数字要用莫尔斯电码来表示,它已经设置在一个数组中,所以我需要单独的数字来将每个数字分配给它指定的莫尔斯电码。
【问题讨论】:
-
为什么不使用字符串的
[]运算符循环/迭代字符串的每个单独字符? -
您是否期望这些数字格式不寻常?比如10E+3?
-
@R Sahu 不,应该是整数
标签: c++ arrays string loops int