0

Hie, I have a problem separating characters from integers within an input file in C++.An example of the file contents is given below:

D  5  C  7  C  9  H  4  S  8  D  11  H  13

I want to read and store characters in an array and integers their own array as well, I know how to use fstream but I am stuck on separating the items. I will greatly appreciate any help offered, thank you in advance.

I solved a similar problem last week but there were no characters in that assignment, code is given below for that:

#include <iostream>
#include <fstream>

using namespace std;
int frequencyCal(unsigned short int[][5], unsigned short int, ofstream&);
unsigned short int ScoreForRow(unsigned short int[][5], unsigned short int,      ofstream&);

int main() {
unsigned short int iArray[50][5];
unsigned short int rows = 0;
ifstream fin("YahtzeIn.txt");
ofstream fout("YahtzeOut.txt");

for(unsigned short int i = 0; i < 50; i++) {
    rows += 1;
    for(unsigned short int j = 0; j < 5; j++) {
        fin >> iArray[i][j];
        cout << iArray[i][j] << " ";
        fout << iArray[i][j] << " ";
    }
    cout << "   ==>  " << ScoreForRow(iArray,i,fout) << endl;
    fout << "   ==>  " << ScoreForRow(iArray,i,fout) << endl;
    if(fin.eof())
       break;
}
cout << endl;
fout << endl;
frequencyCal(iArray, rows, fout);
return 0;
}

int frequencyCal(unsigned short int in_array[][5], unsigned short int filerows, ofstream& outfile) {
unsigned short int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;
// Count the numbers in each row;
for(unsigned short int i = 0; i < filerows; i++)
{
    for(unsigned short int j = 0; j < 5; j++ )
    {  
        // Identify the number and add one to a counter variable
        if(in_array[i][j] == 1) {
           num1+=1;
        }

        else if(in_array[i][j] == 2) {
                num2+=1;
             }

        else if(in_array[i][j] == 3) {
                num3+=1;
             }

        else if(in_array[i][j] == 4) {
                num4+=1;
             }

        else if(in_array[i][j] == 5) {
                num5+=1;
             }

        else if(in_array[i][j] == 6) {
                num6+=1;
             }
    }
}

cout << "Dice frequency" << endl;
cout << "==============" << endl;
cout << "1's    2's    3's    4's    5's    6's" << endl;
outfile << "Dice frequency" << endl;
outfile << "==============" << endl;
outfile << "1's    2's    3's    4's    5's    6's" << endl;
// Print and display the values of the variables;
cout << num1 << "     " << num2 << "     " << num3 << "     " << num4 << "     " << num5 << "     " << num6;
outfile << num1 << "     " << num2 << "     " << num3 << "     " << num4 << "     " << num5 << "     " << num6;

return 0;
}


unsigned short int ScoreForRow(unsigned short int in_array[][5], unsigned short int i, ofstream& outfile) {
unsigned short int numb1 = 0, numb2 = 0, numb3 = 0, numb4 = 0, numb5 = 0, numb6 = 0, score = 0;
bool four = false, five = false;
for(unsigned short int j = 0; j < 5; j++ )
    {  
        // Sum up all numbers in a row
        score += in_array[i][j];
        // check if number occurs twice, three times or five times
        if(in_array[i][j] == 1) {
           numb1+=1;
           if(numb1 == 4) {
              four = true;
           }
           else if(numb1 == 5) {
                    four = false;
                    five = true;
           }
        }

        else if(in_array[i][j] == 2) {
                numb2+=1;
                if(numb2 == 4) {
                    four = true;
                }
                else if(numb2 == 5) {
                        four = false;
                        five = true;
                }
            }

        else if(in_array[i][j] == 3) {
                numb3+=1;
                if(numb3 == 4) {
                    four = true;
                }
                else if(numb3 == 5) {
                        four = false;
                        five = true;
                }
            }

        else if(in_array[i][j] == 4) {
                numb4+=1;
                if(numb4 == 4) {
                    four = true;
                }
                else if(numb4 == 5) {
                        four = false;
                        five = true;
                }
            }

        else if(in_array[i][j] == 5) {
                numb5+=1;
                if(numb5 == 4) {
                    four = true;
                }
                else if(numb5 == 5) {
                        four = false;
                        five = true;
                }
            }

        else if(in_array[i][j] == 6) {
                numb6+=1;
                if(numb6 == 4) {
                    four = true;
                }
                else if(numb6 == 5) {
                        four = false;
                        five = true;
                }
            }
   }

// Check if a number appears twice and another occurs 3 times, if so, update score to 25;    
if(((numb1 == 2) || (numb2 == 2) || (numb3 == 2) || (numb4 == 2) || (numb5 == 2) || (numb6 == 2)) 
&& ((numb1 == 3) || (numb2 == 3) || (numb3 == 3) || (numb4 == 3) || (numb5 == 3) || (numb6 == 3))) {
    score = 25;
}
// Check if a number appears four times
else if(four == true) {
    score = 30;
} 

// Check if a number appears five times
else if(five == true) {
    score = 50;
}
// the value returned  is either sum or number generated by the conditions 
return score;
}
  • Please show us what you have tried so far. – Striezel Nov 24 '16 at 15:10
  • You have to [split the input on whitespace](http://stackoverflow.com/questions/236129/split-a-string-in-c) and then check each token if it is a number. In C++11 this can be done with [std::stoi](http://en.cppreference.com/w/cpp/string/basic_string/stol) – freakish Nov 24 '16 at 15:12
  • The idea is that attempting to read a decimal int when a letter is the next non-white character makes the stream set its "failbit": You can then call clear() on the stream and try to read the letter into a string. Do that repeatedly until eof is encountered. (For robustness you should, if fail() is true, check bad() as well, in order to distinguish real read errors.) – Peter - Reinstate Monica Nov 24 '16 at 15:13
  • Use `isdigit()` from cctype – stark Nov 24 '16 at 15:24
  • 1
    Is the format always alternating characters and numbers? – molbdnilo Nov 24 '16 at 15:27
  • As @molbdnilo asked, is the input always alternating? It makes it a lot easier if it's just alternating. – Charles Nov 24 '16 at 15:27
  • Which part of scoring a Yahtzee game do you believe is relevant to the question? – molbdnilo Nov 24 '16 at 15:53
  • @molbdnilo In the Yahtzee game assignment the input file had numbers only, in this assignment it as numbers and characters occurring alternatively –  Nov 24 '16 at 15:58
  • @c650 yes it is alternating –  Nov 24 '16 at 15:58
  • @CodeDrogba Yes, but your scoring and frequency calculation functions are completely irrelevant. – molbdnilo Nov 24 '16 at 16:02
  • @CodeDrogba if it's alternating, and you know which type the input starts with, you don't need any sort of type-determination. – Charles Nov 24 '16 at 16:49

1 Answers1

0

From the code above you could change iArray in a character array, after this you can use casting to read the integers. Hope this helps