Note that there are some explanatory texts on larger screens.

plurals
  1. POReading a file of ints should be working but is not
    primarykey
    data
    text
    <p>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:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;iomanip&gt; #include &lt;fstream&gt; #include &lt;cmath&gt; #include &lt;cstdlib&gt; using namespace std; struct Hand { int handCards[52]; int totalCards; }; struct Card { char rank; char suit; }; void OpenFile (ifstream&amp;, string&amp;); void ReadFile (ifstream&amp;, Hand&amp;); void ProcessRank (Hand&amp;, int CardRank[]); void ProcessSuit (Hand&amp;, 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 &amp;inf, string &amp;filename) { cout&lt;&lt;"What is the name of the file?" &lt;&lt;endl; cin&gt;&gt;filename; inf.open(filename.c_str()); if (inf.fail()) { cout&lt;&lt;"Sorry, that file doesn't exist" &lt;&lt;endl; exit(1); } else cout&lt;&lt;"Success!" &lt;&lt;endl &lt;&lt;endl; } void ReadFile (ifstream &amp;inf, Hand &amp;theHand) { theHand.totalCards=0; int i=0; inf&gt;&gt;theHand.handCards[i]; while(inf.good()) { i++; inf&gt;&gt;theHand.handCards[i]; theHand.totalCards++; cout&lt;&lt;theHand.handCards[i]; } } void ProcessRank (Hand &amp;theHand, int rank[]) { int placement; for (int i=0; i&lt;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 &amp;theHand, int suit[]) { int placement; for (int i=0; i&lt;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&lt;&lt;aCard.rank&lt;&lt;aCard.suit&lt;&lt;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 &lt;theHand.totalCards; i++) { PrintCard(ConvertRaw(theHand.handCards[i])); } } </code></pre> <p>output is 234560 should be 123456</p> <p>when i put in all the code the file never stops and the core is dumped</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload