无法将指针恢复为零

Can't get pointer back to zero

本文关键字:恢复 指针      更新时间:2023-10-16

我无法将指针归零,因此我可以在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"。