arrays

reading txt file into array

Page 1 of 1

10 Replies - 1226 Views - Last Post: 05 May 2009 - 03:39 AM Rate Topic: -----

#1 atabe  Icon User is offline

  • New D.I.C Head

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

arrays

Posted 02 May 2009 - 06:06 PM

Please help
I'm supposed to write a program that keeps track of the hits, walks and outs of a baseball team using parallel arrays. The player number is the index of the array. Program is supposed to read text file that contains the player number, hits, walks and outs. The data file contains multiple games. Not every player has stats to each game.
Here is the text file:
2
1 2 2 2
20 0 5 1
2 0 0 6
18 4 2 0
3 2 1 3
4 1 2 3
7 0 0 3
8 1 4 1
9 3 2 1
10 2 2 2
11 6 0 0
12 2 2 2
2 0 5 1
20 0 0 6
17 4 2 0
4 2 1 3
1 2 2 2
3 1 2 3
7 0 0 3

And here is my code:
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;


int main ()
{

  const int noOfPlayers = 21;
  int playerNo [noOfPlayers] = {0};
int hitsNo[noOfPlayers]={0};
int walksNo[noOfPlayers]={0};
int outsNo[noOfPlayers]={0};
 int player = 0;
 int hits = 0;
 int walks =0;
 int outs = 0;


 ifstream infile;

 infile.open ("PlayBall.txt");

 
  while (infile)
	{
	  for (int game =1; game < noOfPlayers; game++)
	
  infile >> playerNo [player] >> hitsNo [0+hits]++
	 >> walksNo [0+walks]++ >> outsNo [0+outs]++;
	}
   

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

 for (int playerNo = 1; playerNo < noOfPlayers; playerNo++)
   {
	 cout << setw(8) << playerNo [noOfPlayers] << setw(8)
	  << hitsNo [noOfPlayers] << setw(8) <<  walksNo [noOfPlayers]
	  << setw(8) << outsNo [noOfPlayers]<< endl;
   }

 infile.close();

 return 0;
}




I don't know what's wrong but compiler is giving me an error message: "ambiguous overload for std::basic_istream<char,
std::char_traits<char> >& >> int operator

Please help

Is This A Good Question/Topic? 0
  • +

Replies To: arrays

#2 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: arrays

Posted 02 May 2009 - 06:12 PM

Please advise how you went with the responses given to your original posting of this question here:
http://www.dreaminco...h...=102401&hl=

What has changed in your code since then?
It seems nothing important has changed and that is why you are getting the same error message.

But correct me if you have addressed the issue and I am just missing it.
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: arrays

Posted 02 May 2009 - 06:23 PM

I thought I improved it

this part:
while (infile)
	{
	  for (int game =1; game < noOfPlayers; game++)
	
  infile >> playerNo [player] >> hitsNo [0+hits]++
	 >> walksNo [0+walks]++ >> outsNo [0+outs]++;
	}



it supposed to read (that's why >>) the numbers in the text file:
First number - is the player number
second - number of hits of the player
third - number of walks of the player
fourth - number of outs of the player
for each game (each line in text file)
and update the components of the arrays.
That's what I thought I did
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: arrays

Posted 03 May 2009 - 04:54 AM

View Postatabe, on 2 May, 2009 - 05:23 PM, said:

I thought I improved it


Same question as before.
Is this a pattern of code you have seen somewhere else?
Are you following an example from another source?

If you are then share that example with us so we can see what you are trying to do.

I've not seen the idea you seem to be using implemented successfully so it may be an invalid approach (or it may be exposing a hole in my knowledge - there are a lot of those). Show us an example of doing what you are trying to do done successfully, if you have one.

Here's a hint.
If I comment out your multiple use of ">>"
  while (infile)
    {
      for (int game =1; game < noOfPlayers; game++)
    
		infile >> playerNo [player]; 
		//infile >> hitsNo [0+hits]++ >> walksNo [0+walks]++ >> outsNo [0+outs]++;
    }


Look at the compiler messages I get:
DIC.cpp: In function ‘int main()’:
DIC.cpp:42: error: invalid types ‘int[const int]’ for array subscript
DIC.cpp:17: warning: unused variable ‘hits’
DIC.cpp:18: warning: unused variable ‘walks’
DIC.cpp:19: warning: unused variable ‘outs’

Interesting isn't it?
Suddenly the ambiguity goes away (and exposes another problem in your "cout" statement that you will need to look at later).

How about you try a more standard approach to getting your incoming data.
get a line of input from the file as a string and then allocate substrings to the relevant array locations.

Why are you making you arrays "int" arrays?
Just because something has digits doesn't mean it has to be an int (or float or double).
Your phone number is made up of digits but it is a string of characters not a number to do maths with.
Are you doing maths with these digits?
If not, make your life easier and make your arrays 'string' or 'char'.
Was This Post Helpful? 0
  • +
  • -

#5 atabe  Icon User is offline

  • New D.I.C Head

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

Re: arrays

Posted 03 May 2009 - 06:57 AM

View Postjanotte, on 3 May, 2009 - 03:54 AM, said:

View Postatabe, on 2 May, 2009 - 05:23 PM, said:

I thought I improved it


Same question as before.
Is this a pattern of code you have seen somewhere else?
Are you following an example from another source?

If you are then share that example with us so we can see what you are trying to do.

I've not seen the idea you seem to be using implemented successfully so it may be an invalid approach (or it may be exposing a hole in my knowledge - there are a lot of those). Show us an example of doing what you are trying to do done successfully, if you have one.

Here's a hint.
If I comment out your multiple use of ">>"
  while (infile)
    {
      for (int game =1; game < noOfPlayers; game++)
    
		infile >> playerNo [player]; 
		//infile >> hitsNo [0+hits]++ >> walksNo [0+walks]++ >> outsNo [0+outs]++;
    }


Look at the compiler messages I get:
DIC.cpp: In function ‘int main()’:
DIC.cpp:42: error: invalid types ‘int[const int]’ for array subscript
DIC.cpp:17: warning: unused variable ‘hits’
DIC.cpp:18: warning: unused variable ‘walks’
DIC.cpp:19: warning: unused variable ‘outs’

Interesting isn't it?
Suddenly the ambiguity goes away (and exposes another problem in your "cout" statement that you will need to look at later).

How about you try a more standard approach to getting your incoming data.
get a line of input from the file as a string and then allocate substrings to the relevant array locations.

Why are you making you arrays "int" arrays?
Just because something has digits doesn't mean it has to be an int (or float or double).
Your phone number is made up of digits but it is a string of characters not a number to do maths with.
Are you doing maths with these digits?
If not, make your life easier and make your arrays 'string' or 'char'.



I'm following the example from my book (the only thing on parallel arrays)
Here it is:
"Suppose you need to keep track of student's course grades, together with their ID numbers. (...) there may be 50 students (...) You can declare two arrays: studentID of type int and course Grade of type char. Each array has 50 components. Furthermore, studentID[0] and courseGrade[0] will store the ID and course grade of the first student, studentID[1] and courseGrade[1] will store the ID and course grade of the second student, and so on.

The statements:
int studentID[50];
char courseGrade[50];


declare these two arrays.

Suppose you need to input data into thses arrays, and the data is provided in a file in the following form:
studentID courseGrade

For example, a sample data set is:
23456 A
83744 B
23456 C
33976 B
.
.
.
Suppose that the input file is opened using the ifstream variable infile. Because the size of each array is 50, a maximum of 50 elements can be stored into each array. Moreover, it is possible that there may be fewer than 50 students in the class. Therefore, while reading the data, we also count the number of students and ensure that the array indices do not go out of bounds. The following loop reads the data into the parallel arrays studentID and courseGrade:
int noOfStudents =0;

infile >> studentID[noOfStudents] >> courseGrade[noOfStudents];

while (infile && noOfStudents <50)
{
noOfStudents++;
infile >> studentID[noOfStudents] >> courseGrade [noOfStudents];
}



This code is working, I checked, but I need to modify it.
This code just reads the data file in order. I need to read my data file and have code "recognize" the player number (first digid) to be able to update three other arrays (columns/digits). For example player number 1 is twice in the data file and twice he scored 2 2 2 so I need a code that will update components of three arrays for that player and add his scores. Player no 1, hits (2+2=4), walks (2+2=4), and outs (2+2=4) and so on for every player. Some players would have 0.
I have no clue how to do that.
I cannot use char, maybe only for the first number that is player number.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: arrays

Posted 03 May 2009 - 07:07 AM

Well your code doesn't work like your example does it.

So how about you follow the example you have shared with us.

Read everything into your parallel arrays just like in the example.

Once you have all the data in the parallel arrays then start doing the sorting, comparing and combining operations.
Was This Post Helpful? 0
  • +
  • -

#7 atabe  Icon User is offline

  • New D.I.C Head

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

Re: arrays

Posted 03 May 2009 - 08:51 AM

View Postjanotte, on 3 May, 2009 - 06:07 AM, said:

Well your code doesn't work like your example does it.

So how about you follow the example you have shared with us.

Read everything into your parallel arrays just like in the example.

Once you have all the data in the parallel arrays then start doing the sorting, comparing and combining operations.


This code reads the data file:
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;


int main ()
{

  int playerNo [20] = {0};
int hitsNo[20]={0};
int walksNo[20]={0};
 int outsNo[20]={0};
 int noOfPlayers = 0;

 ifstream infile;

  infile.open ("PlayBall.txt");

 infile >> playerNo[noOfPlayers] >> hitsNo[noOfPlayers]
	>> walksNo [noOfPlayers] >> outsNo[noOfPlayers];

 while (infile && noOfPlayers < 20)
   {
	 noOfPlayers++;
	 infile >> playerNo[noOfPlayers] >> hitsNo[noOfPlayers]
	>> walksNo [noOfPlayers] >> 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) << playerNo [noOfPlayers] << setw(8)
	  << hitsNo [noOfPlayers] << setw(8) <<  walksNo [noOfPlayers]
	  << setw(8) << outsNo [noOfPlayers]<< endl;
   }

 infile.close();

 return 0;
}




and now I have it exactly like in data file, but that's not what I want. I want it in order 1-20, every player appearing just once.
I don't know how to do that.
Was This Post Helpful? 0
  • +
  • -

#8 atabe  Icon User is offline

  • New D.I.C Head

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

Re: arrays

Posted 03 May 2009 - 05:54 PM

I rewrote the program and it's still not working. It's showing mostly 0 and from time to time some number. Arrays are longer, more that 20 numbers.
I have no clue what I did wrong this time. Can anyone help?

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

using namespace std;

const int NO_OF_PLAYERS = 20;

void printHeading ();

void initialize (int hitsNo[], int walksNo[], int outsNo[], int noOfRows);

int binSearch (char playerNo[], int noOfRows, char player);

void processGames (ifstream& inp, char playerNo[], int hitsNo[], int walksNo[],
		  int outsNo[], int noOfRows);

void printResults (char playerNo[], int hitsNo[], int walksNo[], int outsNo[],
		   int noOfRows);


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

 ifstream infile;

  infile.open("playBall.txt");

  if (!infile)
	{
	  cout << "Input file (playBall.txt) does not exist." << endl;
	  return 1;
	}


  initialize (hitsNo, walksNo, outsNo, NO_OF_PLAYERS);

  processGames (infile, playerNo, hitsNo, walksNo, outsNo, NO_OF_PLAYERS);

  printHeading ();

  printResults (playerNo, hitsNo, walksNo, outsNo, NO_OF_PLAYERS);

  infile.close();

  return 0;
}


void initialize (int hitsNo[], int walksNo[], int outsNo[], int noOfRows)
{
  int i;

  for (i = 0; i< noOfRows; i++)
	hitsNo [i] = 0;

  for (i = 0; i < noOfRows; i++)
	walksNo[i] = 0;

  for (i = 0; i < noOfRows; i++)
	outsNo[i] = 0;
}


int binSearch (char playerNo[], int noOfRows, char player)
{
  int first, last, mid;
  bool found;
  first = 0;
  last = noOfRows -1;
  found = false;

  while (!found && first <= last)
	{
	  mid = (first + last) / 2;
	  if (playerNo[mid] == player)
	found = true;
	  else if (playerNo[mid] > player)
	last = mid - 1;
	  else
	first = mid + 1;
	}
  if(found)
	return mid;
  else 
	return -1;
}


void processGames (ifstream& inp, char playerNo[], int hitsNo[], int walksNo[],
		   int outsNo[], int noOfRows)
{
  char personNumber;
  int noOfHits;
  int noOfWalks;
  int noOfOuts;
  int loc;

  inp >> personNumber >> noOfHits >> noOfWalks >> noOfOuts;

  while (inp)
	{
	  loc = binSearch (playerNo, noOfRows, personNumber);

	  if (loc != -1)
	hitsNo[loc] = hitsNo[loc] + noOfHits;
	  walksNo[loc]= walksNo[loc] + noOfWalks;
	  outsNo[loc] = outsNo[loc] + noOfOuts;

	  inp >> personNumber >> noOfHits >> noOfWalks >> noOfOuts;
	}
}

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

  cout << setw(8) << "--------" << setw(8) << "--------" << setw(8)
	   << "--------" << setw(8) << "--------" << endl;
}


void printResults (char playerNo[], int hitsNo[], int walksNo[], int outsNo[],
		   int noOfRows)
{
  int i;

  for (i = 0; i < noOfRows; i++)

	cout << setw (8) << playerNo[i] << setw (8) << hitsNo[i] << setw (8)
	 << walksNo[i] << setw (8) << outsNo[i] << endl; 
}


Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: arrays

Posted 04 May 2009 - 06:00 AM

View Postatabe, on 3 May, 2009 - 07:51 AM, said:

now I have it exactly like in data file, but that's not what I want. I want it in order 1-20, every player appearing just once.
I don't know how to do that.


But your teacher thinks you can work out a plan or s/he wouldn't have set you the homework.
Have as much faith in yourself as they have in you.

My advice about char arrays was bad because I didn't know what you were trying to do because your original example didn't have any of the detail you have told us since. Declare all your arrays as holding ints as you, correctly, originally had them.

Remember you know more about the problem than anyone in the forum so don't be afraid to ignore advice you know is bad.

-----

OK here's a plan.
Feel free to come up with a better one.

Sort the array playerNo[] as you would a single array but whenever you make a change to playerNo[] replicate the same change in the parallel arrays hitsNo[], walksNo[] and outsNo[] so they stay aligned with playerNo[].

Now get rid of duplicates in playerNo[] adding and storing the corresponding values in the parallel arrays before removing the relevant elements from all arrays.

Now you have 4 arrays sorted in playerNo order with all the other numbers for a given player added together.

Make sense?

----

Now go slowly.

Do they 'sort all arrays into player order' step and get that working before you try the 'remove duplicate step'.

Do the 'sort' and 'remove duplicates' steps in new separate functions so you can focus on just the relevant bit until you get it working.

I haven't looked at your latest code because you have already, correctly, identified that your plan can never work if you et the array lengths to 20 when you know very well you are going to read in more than 20 elements into the arrays.

This is not true
"I have no clue what I did wrong this time."
You do have a very good "clue" and you have the smarts to do this.
What you are lacking is a plan.
Get a plan first and then write the code.
Was This Post Helpful? 0
  • +
  • -

#10 atabe  Icon User is offline

  • New D.I.C Head

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

Re: arrays

Posted 04 May 2009 - 09:14 PM

View Postjanotte, on 4 May, 2009 - 05:00 AM, said:

View Postatabe, on 3 May, 2009 - 07:51 AM, said:

now I have it exactly like in data file, but that's not what I want. I want it in order 1-20, every player appearing just once.
I don't know how to do that.


But your teacher thinks you can work out a plan or s/he wouldn't have set you the homework.
Have as much faith in yourself as they have in you.

My advice about char arrays was bad because I didn't know what you were trying to do because your original example didn't have any of the detail you have told us since. Declare all your arrays as holding ints as you, correctly, originally had them.

Remember you know more about the problem than anyone in the forum so don't be afraid to ignore advice you know is bad.

-----

OK here's a plan.
Feel free to come up with a better one.

Sort the array playerNo[] as you would a single array but whenever you make a change to playerNo[] replicate the same change in the parallel arrays hitsNo[], walksNo[] and outsNo[] so they stay aligned with playerNo[].

Now get rid of duplicates in playerNo[] adding and storing the corresponding values in the parallel arrays before removing the relevant elements from all arrays.

Now you have 4 arrays sorted in playerNo order with all the other numbers for a given player added together.

Make sense?

----

Now go slowly.

Do they 'sort all arrays into player order' step and get that working before you try the 'remove duplicate step'.

Do the 'sort' and 'remove duplicates' steps in new separate functions so you can focus on just the relevant bit until you get it working.

I haven't looked at your latest code because you have already, correctly, identified that your plan can never work if you et the array lengths to 20 when you know very well you are going to read in more than 20 elements into the arrays.

This is not true
"I have no clue what I did wrong this time."
You do have a very good "clue" and you have the smarts to do this.
What you are lacking is a plan.
Get a plan first and then write the code.


Thanks for the encouragement. I did it :-) !!!!
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: arrays

Posted 05 May 2009 - 03:39 AM

View Postatabe, on 4 May, 2009 - 08:14 PM, said:

Thanks for the encouragement. I did it :-) !!!!


That's excellent news!
Well done!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1