-2

Me and a friend have been working on a program over the past couple of days and figured out how to read a file of ints into a program, it worked the first day we made it, but i must've changed something because now it reads from the second integer on and adds a zero at the end. Here's the code:

#include <iostream>

#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;

struct Hand
{
  int handCards[52];
  int totalCards;
};

struct Card
{
  char rank;
  char suit;
};

void OpenFile (ifstream&, string&);
void ReadFile (ifstream&, Hand&);
void ProcessRank (Hand&, int CardRank[]);
void ProcessSuit (Hand&, int CardSuit[]);
char GetRank (int);
char GetSuit (int);
void PrintCard (Card);
Card ConvertRaw (Hand);
void PrintHand (Card, Hand);
int main()
{
  ifstream inf;
  string filename;
  Hand theHand;
  Card aCard;
  int CardRank[13];
  int CardSuit[4];

  OpenFile(inf, filename);
  ReadFile(inf, theHand);


}

void OpenFile (ifstream &inf, string &filename)
{
  cout<<"What is the name of the file?" <<endl;
  cin>>filename;

  inf.open(filename.c_str());

  if (inf.fail())
    {
      cout<<"Sorry, that file doesn't exist" <<endl;
      exit(1);
    }
  else
    cout<<"Success!" <<endl <<endl;
}

void ReadFile (ifstream &inf, Hand &theHand)
{
  theHand.totalCards=0;
  int i=0;
  inf>>theHand.handCards[i];
  while(inf.good())
    {
      i++;
      inf>>theHand.handCards[i];
      theHand.totalCards++;
      cout<<theHand.handCards[i];
    }
}



  void ProcessRank (Hand &theHand, int rank[])
{
  int placement;
  for (int i=0; i<13; i++)
    {
      rank[i]=0;
    }
  for (int i=0; i=theHand.totalCards; i++)
    {
      placement=(theHand.handCards[i]%13);
      switch (placement)
        {
        case 0:rank[0]++; break;
        case 1:rank[1]++; break;
        case 2:rank[2]++; break;
        case 3:rank[3]++; break;
        case 4:rank[4]++; break;
        case 5:rank[5]++; break;
        case 6:rank[6]++; break;
        case 7:rank[7]++; break;
        case 8:rank[8]++; break;
        case 9:rank[9]++; break;
        case 10:rank[10]++; break;
        case 11:rank[11]++; break;
        case 12:rank[12]++; break;
        }
    }


void ProcessSuit (Hand &theHand, int suit[])
{
  int placement;
  for (int i=0; i<5; i++)
    {
      suit[i]=0;
    }
  for (int i=0; i=theHand.totalCards; i++)
    {
      placement=(theHand.handCards[i]/13);
      switch (placement)
        {
        case 0:suit[0]++; break;
        case 1:suit[1]++; break;
        case 2:suit[2]++; break;
        case 3:suit[3]++; break;
        }
    }
}


char GetRank(int CardRank)
{
  int rankV=(CardRank%13);
  switch(rankV)
    {
    case  0: return 'A';
    case  9: return 'T';
    case 10: return 'J';
    case 11: return 'Q';
    case 12: return 'K';
    default: return (char(rankV + '0' + 1));
    }
}

char GetSuit (int CardSuit)
{
  int suitV=(CardSuit/13);
    switch(suitV)
      {
      case  0: return 'D';
      case  9: return 'H';
      case 10: return 'S';


   }
}


void PrintCard (Card aCard)
{
  cout<<aCard.rank<<aCard.suit<<endl;
}

Card ConvertRaw(int rawValue)
{
  Card finalCard;
  finalCard.rank=GetRank(rawValue);
  finalCard.suit=GetSuit(rawValue);
  return finalCard;
}

void PrintHand (Card aCard, Hand theHand)
{
  for (int i=0; i <theHand.totalCards; i++)
    {
    PrintCard(ConvertRaw(theHand.handCards[i]));
    }
}

output is 234560 should be 123456

when i put in all the code the file never stops and the core is dumped

4

1 回答 1

1

ReadFile中,您在 while 循环之外读取了第一张卡片,但在您有机会打印第一张卡片之前,您读取了第二张卡片。

不知道为什么你的最后一张牌是 0,可能与你Hand没有显示的课程有关。

另外,您是否考虑过使用VCS

于 2013-02-08T05:41:44.113 回答