简体   繁体   English

如何在C ++中读取输入文件时检查是否出现整数或字符

[英]How to check whether an integer has occurred or a character has occurred while reading input file in C++

Hie, I have a problem separating characters from integers within an input file in C++.An example of the file contents is given below: 嗨,我在使用C ++的输入文件中将字符与整数分开时遇到问题,文件内容的示例如下所示:

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. 我想读取和存储字符并将其存储在数组中,也希望对它们自己的数组进行整数处理,我知道如何使用fstream,但我仍然坚持将各项分开。 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;
}

From the code above you could change iArray in a character array, after this you can use casting to read the integers. 从上面的代码中,您可以更改字符数组中的iArray,此后,您可以使用强制转换来读取整数。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM