reading from a file to an array, then printing it out

i need help with a problem im having, im trying to read in some things from a .txt file and put them in an array, then print out the information, i just cannot figure it out. it is a game of bridge, its suppose to read in the info and figure out how much each hand is worth and who would win in the hand, this is what i have so far:

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

using namespace std;

const int rows = 3;
const int col = 7;

void readfile(int hand[][col], int rows, int col) // reads in the hands
{
	ifstream infile;
	infile.open("program1.txt");
	for(int i=0; i<rows; i++)
		for(int j=0; j<col; j++)
			infile >> hand[i][j];
}

void getbid(int hand[][col], int rows, int col, int Bid[]) // Gets the bids
{
	int bid;
	for(int i=0; i<rows; i++)
	{
		bid = 0;
		for(int j=0; j<col; j++)
			bid += hand[i][j];
		Bid[i]=bid;
	}
}

void printhand(int hand[rows][col])
{
	int i,j;
	for(i=0; i<rows; i++)
		for(j=0; j<col; j++)
			cout << setw(5) << hand[i][j] << endl;
}

int main ()
{
	int hand[rows][col];
	ifstream infile;
	infile.open("program1.txt");
	printhand(hand);
	readfile(hand, rows, col);

	infile.close();
	system("pause");
	return 0;
}
What's the problem exactly?
One silly thing is that you pass rows and col to readfile even though they are global constants. This shouldn't cause a compile error.

Just noticed. You first call printhand and only then readfile. How does that make sense?

Also, you open file in main(), then open it again in readfile() and then close it. This might be causing errors too. I'm not sure about this one though.
right after i posted this i noticed that printhand was before readfile, but that didnt make a difference, now all i want to do is read in from a file, and give values to the information given,

I.E.

A stands for ACE, K for king, Q for queen, J for jack, and T for ten
H stands for hearts and S stands for spades.

ace are worth 11 points
kings are worth 8 points
queens are worth 6 points
jacks are worth 3 points
tens are worth 1 point

so...

AH, AS should equal 11, KH, KS should equal 8, QS, QH should equal 6, JH, JS, should equal 3, TS, TH, sould equal 1.

the first two lines our .txt document looks like this

AS KS QS JS TS AH
KH QH JH TH AS KS

there are 6 lines in the .txt doc

how do i make it so that the program will add values to the letters given, and then add them up for the total for each line.
Last edited on
1
2
3
4
5
6
7
8
9
char word[3];
while(file >> word){
   //now word[0] is A, K, Q, J or T and word[1] is S or H
   //I'm not sure what you want to do with them,
  //but whatever it is you'll need some logic:
  int points = 0;
  if(word[0] == 'A') points = 11;
  else ...
}


By the way, row and col only need to be 2 and 6.
Topic archived. No new replies allowed.