3 Replies - 5628 Views - Last Post: 30 April 2009 - 04:27 AM Rate Topic: -----

#1 atabe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-April 09

Parallel Arrays - reading a file into Parallel Arrays

Post icon  Posted 29 April 2009 - 12:50 PM

Please help. I'm working on writing a program that uses parallel arrays. It supposed to keep track of hits, walks and outs of a baseball team. The file contains multiple games. Not every player has stats in each game.
The player number is the index of the array. I cannot make the program to read data into a proper component.

With this code I have compiler error which I don't understand:
"PlayBall.cpp: In function void read File (std::basic_ifstream<char,
std::char_traits<char> >, const int*, int*, int*, int*, int&):
PlayBall.cpp:58: ambiguous overload for 'std::ifstream& >> const int&' operator
/usr/include/c++/3.2.3/bits/istream.tcc:83: candidates are:
std::basic_istream<_CharT, _Traits>& std::basic_istream<-CharT,
_Traits>::operator>>(std::basix_istream<_CharT,
_Traits>& (*) (std::basic_istream<_CharT, _Traits>&)) [with_CharT =char,
_Traits = std::char_traits<char<] <near match>
/usr/include/c++/3.2.3/bitx/istream.tcc:92:
std::basix_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::operator>>(std::basic_ios<_CharT,
_Traits>&(*) (std::basic_ios,_CharT, _Traits.&)) [with _CharT =char, _Traits
= std::char_traits<char>] <near match.
/usr/include/c++/3.2.3/bits/istream.tcc:101:
It continues like this for different lines

Here is my code:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void readFile (ifstream& infile, const int PLAYER_NO[], int hitsNo [], 
int walksNo[], int outsNo[], int& noOfPlayers);

int main ()
{
const int PLAYER_NO [20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int hitsNo [20] = {0};
int walksNo [20] = {0};
int outsNo [20] = {0};
int noOfPlayers = 0;

ifstream infile;
infile.open ("PlayBall.txt");

readFile (infile, PLAYER_NO, hitsNo, walksNo, outsNo, noOfPlayers);

cout << setw (8) << "Player" << setw (8) << "Hits" << setw (8) << "Walks" << setw (8) << "Outs" << endl;
cout << endl;

for (noOfPlayers = 0; noOfPlayers < 20; noOfPlayers++)
{
cout << setw(8) << PLAYER_NO [noOfPlayers] << setw(8)
<< hitsNo [noOfPlayers] << setw(8) << walksNo [noOfPlayers] << setw(8) << outsNo [noOfPlayers] << endl;
}

infile.close();
return 0;
}

void readFile (ifstream& infile, const int PLAYER_NO [], 
int hitsNo[], int walksNo[], int outsNo [], int& noOfPlayers)
{
while (infile && noOfPlayers < 20)
{
for (noOfPlayers =0; noOfPlayers < 20; noOfPlayers++)

infile >> PLAYER_NO [noOfPlayers] >> hitsNo [noOfPlayers]
>> walksNo [noOfPlayers] >> outsNo [noOfPlayers];
}
}


Thanks for suggestion
I don't even know if it's possible to read data and put it in different components or it has to be in order. I cannot find any information in my book

This post has been edited by atabe: 29 April 2009 - 02:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Parallel Arrays - reading a file into Parallel Arrays

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Parallel Arrays - reading a file into Parallel Arrays

Posted 29 April 2009 - 01:44 PM

Please edit your posting (see the "EDIT" button in lower right).
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.

----

Look at this line
infile.open ("PlayBall.txt);
and this error from the compiler
DIC.cpp:18: error: missing terminating " character
Was This Post Helpful? 0
  • +
  • -

#3 atabe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-April 09

Re: Parallel Arrays - reading a file into Parallel Arrays

Posted 29 April 2009 - 02:22 PM

I edited it as you told me and added what compiler is telling about my code
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Parallel Arrays - reading a file into Parallel Arrays

Posted 30 April 2009 - 04:27 AM

Gee whiz!
Is that how your code is formatted in your editor?
No indentation and no sensible brace placement?
Prepare yourself for a bad mark if you hand in code formatted like that.
It's just unacceptable!

Explain what you think this will do:
infile >> PLAYER_NO [noOfPlayers] >> hitsNo [noOfPlayers]
>> walksNo [noOfPlayers] >> outsNo [noOfPlayers];
}


Have you seen an example like that in your text or somewhere else?

How about getting your textbook and class notes out and look into how you might do file I/O based on the provided examples.

Or read here:
http://www.cplusplus...tutorial/files/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1