2013-02-08 21 views
-2

我和一個朋友在過去幾天一直在研究一個程序,並找出如何讀入一個程序中的整數文件,它工作的第一天,我們做了它,但我必須改變了一些東西,因爲現在它從第二個整數開始讀取並在最後加上一個零。 下面的代碼:閱讀一個文件的整數應該是工作,但不是

#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])); 
    } 
} 

輸出爲234560 應該是123456

當我把所有的代碼的文件從來沒有停止和核心轉儲

+0

學習使用'g ++ -Wall -g'進行編譯並使用'gdb'調試器 –

回答

1

ReadFile,你讀的第一卡在while循環之外,但在您有機會打印第一張卡片之前,請先閱讀第二張卡片。

不知道爲什麼你的上一張牌是0,可能與你的Hand類沒有關係。您是否考慮使用VCS

+0

擺脫第一個inf >>使其按時間順序讀取正確,但零仍然存在,並且沒有我沒有成爲cs足夠長的時間/足夠的研究來了解它們的存在 – user1804751

相關問題