无法将指针恢复为零
Can't get pointer back to zero
我无法将指针归零,因此我可以在echo之后再次遍历数据。
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ifstream infile;
infile.open ("scores.txt" , ifstream::in);
int ch = infile.get();
string lastname;
char gender= ' ';
string collegetype;
float score=0;
float scoreMALE=0;
float scoreFEMALE=0;
int countMALE=0;
int countFEMALE=0;
while ( !infile.eof())
{
cout << (char) ch;
ch = infile.get();
}
infile.seekg(0,ios::beg);
while ( !infile.eof())
{
infile>>lastname>>gender>>collegetype>>score;
if(gender == 'm')
{
scoreMALE = scoreMALE + score;
countMALE++;
}
else if (gender == 'f')
{
scoreFEMALE = score;
countFEMALE++;
}
}
cout<<"nnn The total Female Scores is"<<countFEMALE<<"nnn The total of the male scores is"<<countMALE; // Checking to see if file works
infile.close();
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
这是输入文件:
Bailey M CC 68Harrison F CC 71赠款M UN 75Peterson F UN 69徐79鲍尔斯M CC 75Anderson F UN 64Nguyen F CC 68夏普F CC 75Jones M UN 75McMillan F UN 80Gabriel F UN 62
在中缀.seekg(0,ios:beg)之上添加中缀.clear(),会导致代码在这里迭代第二个循环(正如Kerreck SB建议的那样)。
这样做后,你的分数仍然为0的原因是所使用的字符比较是不正确的。例如,输入文件中的"f"answers"m",而不是"f"answers"m"。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 通过指针恢复对数组的引用.UB与否?
- C++指针恢复
- 无法从映射中恢复指向成员函数的指针
- 无法将指针恢复为零
- C ++:给定指针类型,如何恢复指针类型
- 如何恢复我的鼠标指针时,它不可见的QWSSERVER::SetCursorVisible(false)
- 从boost::any中恢复函数指针
- 从指针到成员恢复父节点
- C++:在回调中恢复对象指针(API 不支持user_data指针)