0

I am trying to perform some operations on a text file containing a repetition of a C based string and some numbers. My code successfully carried out the operation on the first set but it would not get to the remaining sets.

Please see the content of the text file below:

Max Scherzer             2017

6.2 4 2 2 2 7

6.0 4 3 1 2 10

mod Cameron              2018

6.4 4 1 2 1 3

6.0 4 3 5 2 8

John Brandonso           2019

6.1 1 3 5 2 7

6.5 4 7 3 4 10

I have used .eof() and it completely messed up what i am doing.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
char playername [25];
int season;

ifstream gamefilein;

gamefilein.open("C:\\Users\\troy\\Desktop\\GAME_SCORE\\gameinfo.txt");
if(!gamefilein)
{
    cout<<"unable to open file";
}

double IP;
int H,R,ER,BB,K;
int counter=0;
double totalscore=0;
while(!gamefilein.fail())
{
gamefilein.get(playername,25);
gamefilein>>season;
cout<<playername<<season<<endl;
cout<<"Game Scores:"<<endl;

while(gamefilein>>IP>>H>>R>>ER>>BB>>K)
{
int IPa=IP;
int IPb=(IP-IPa)*10;
int IPc=0;
if(IPa>4)
{
    IPc=IPa-4;
}
int score=50+(IPa*3)+(IPb*1)+(IPc*2)+(K*1)-(H*2)-(ER*4)-((R-ER)*2)-(BB*1);
cout<<score<<endl;
counter++;
totalscore+=score;
}

cout<<"Number of Games Started: "<<counter<<endl;
cout<<fixed<<setprecision(2)<<"Average Game Score:  
<<(totalscore/counter)<<endl<<endl;
}
gamefilein.close();
return 0;
}    

I get the below result, but I want the same result for the rest of the information in the text file, for example, I am expecting two more results like the one I have below.

Max Scherzer            2017


Game Scores:

63

64

Number of Games Started: 2

Average Game Score: 63.50
Greenonline
  • 1,330
  • 8
  • 23
  • 31
  • 1
    You have to know the format of what you read. How else would you ever know how to make sense of it? – Jesper Juhl Apr 25 '19 at 18:57
  • 1
    You may want to read a line at a time then parse the string. – drescherjm Apr 25 '19 at 18:57
  • 3
    `I have used .eof()` be careful of this pitfall: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Apr 25 '19 at 18:58
  • 3
    IMHO, you should have structures or classes to model the records (input lines). Each structure should overload `operator>>` to read in its data. You can then create a `Player` class which *contains* these record classes. Usually a lot cleaner approach and let's you do other things with these objects. – Thomas Matthews Apr 25 '19 at 19:42

2 Answers2

0

Aren't you reading the file as a char array?

If I read this correctly you try to shift an int and double over a char array with numbers in a STRING right?

e.g. "6.2" string is different than a 6.2 double number in your memory, hence why it cant work.

You also seem to have a lot of spaces which should not forget as well.

Where do you get that string to begin with? I would recommend you change the creation of that file to a more convenient format e.g. cv or json

0

I just solved my problem myself. The problem occurred when the loop operating on the integers and double completes its run and sees the character-based string that is in the next dataset. So i inserted a clear member function just at the point where i check for end of file (gamefilein.clear()) and that solved my problem.

Thanks for attempting to help