正确从 ifstream 获取值时出现问题
Problems getting values from ifstream correctly
**EDIT:我通过将"inStream>> next"
更改为"inStream>> skipws>> next"来让它工作。 在我之前的一个函数(拉取姓氏和名字)中,我切换了noskipws。 显然,这种切换在功能之间持续?
我有一个程序,它是作业的一部分,应该读取一个格式设置为的文本文件:"姓氏名字 1 2 3 4 5 6 7 8 9 10"(其中 10 个数字中的每一个都是整数分数)。
我可以很好地读取姓氏和名字,但是当我开始阅读数字时,我只能读取第一个,然后其余的都设置为 0。
下面是应该读取分数的函数。 inStream已经取消了姓氏和名字。 我正在使用的文本文件有一行:
托西斯·哈雷 85 23 10 95 43 12 59 43 20 77
当运行程序并打印出 student.score 值从 0 到 9 时,第一个正确显示为"85",但所有重置都显示为"0"。 思潮?
void GetScores (ifstream& inStream, record& student)
{
int score[10] = {-1, -1, -1, -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1};
int next;
int counter = 0;
string test;
for (int i = 0; i < 10; i++)
{
inStream >> next;
student.score[i] = next;
}
}
假设输入确实是所有数字,该函数实际上应该可以工作。但是,应始终验证输入是否确实正确读取:
for (int i = 0; i != 10 && inStream >> next; ++i) {
student.score[i] = next;
}
if (!inStream) {
std::cout << "an input error occuredn";
}
相关文章:
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 为什么在分配给成员变量之前获取unique_ptr的返回是一个问题?
- 在 2D 矢量中获取多个输入时出现问题
- 在我的以下代码中获取 MLE(内存限制错误).尝试解决 ROUND C 2019(问题 A-摆动行走)启动问题
- 在Hackerearth上获取运行时错误(SIGSEGV),无法找出代码中的问题
- 通过命令行参数获取llvm ir文件时面临问题
- 函数,从问题中获取输入,并在主函数中使用该输入
- 从命令行获取参数时出现问题
- 获取处理器相关性(linux)时出现问题
- time_t的时钟周期和获取时间问题
- 按名称问题获取卸载应用程序路径
- 在 JS 中通过 Emscripten 获取 SDL 代码C++时遇到问题
- 获取 CLSID 密钥问题
- 获取“boost::文件系统::p ath”字符指针时出现问题
- 获取应用程序图标时出现问题
- 我的代码有什么问题以获取正确的梯度
- 调用基构造函数时遇到问题 - 获取读取访问冲突
- 在程序中获取 Linux 'dd' 命令的输出时遇到问题C++
- 关于获取行和字符串查找函数的问题
- C++问题 - 获取线跳过第一个输入