lundi 27 juin 2016

How to read and count the words in an input file, count and print the amount of letters per word using C++?

I am a rookie in programming and have an assignment requires me to create a program that would read a text file with a list of words, count the total amount of words and the amount of letters for each word and print out a output file with the amount of words with x amount of letters by categories from 1 letter words to 13 letter words.

As I create my function and try to have it read the words inside the text file it doesn't allow me to use inFile >> word; to read their length.

I get the error:

"Invalid Operands To Binary Expressions".

Other classmates have used this command without trouble. I'm using Eclipse Mars.1 on OS X El Capitan.

Another error that I am getting is on my switch feature where it evaluates for the first case but not for the following ones. In this case I get the following error message:

" 'case' Statement not on Switch statement".

Thanks in advance!

void Words_Statistics(std::ifstream & fin, std::ofstream & fout, std::string inFile, std::string outFile)
    {

    // Variable Declaration
    inFile="words.txt";
    outFile="Words_Satistics.txt";
    string word;
    int totalWords=0;
    int lettersQuantity;
    int un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix, onze, douze, treize, otre;
    un = deux = trois = quatre = cinq = six = sept = huit = neuf = dix = onze = douze = treize = otre=0;

    // Open input file to read-in
    fin.open(inFile);
        if(fin.fail())
        {
            cout << inFile << " Failed to open file."<< endl;
            exit (1);
        }
        do {
            (inFile >> word);
            lettersQuantity = int (sizeof(word));
            totalWords++;
            lettersQuantity+=lettersQuantity;

                switch (lettersQuantity)
                    case 1:
                        un++;
                        break;
                    case 2:
                        deux++;
                        break;
                    case 3:
                        trois++;
                        break;
                    case 4:
                        quatre++;
                        break;
                    case 5:
                        cinq++;
                        break;
                    case 6:
                        six++;
                        break;
                    case 7:
                        sept++;
                        break;
                    case 8:
                        huit++;
                        break;
                    case 9:
                        neuf++;
                        break;
                    case 10:
                        dix++;
                        break;
                    case 11:
                        onze++;
                        break;
                    case 12:
                        douze++;
                        break;
                    case 13:
                        treize++;
                        break;
                    default:
                        otre++;
                        break;
        }
        while (!fin.eof());

    int avg = lettersQuantity / totalWords;
}

Aucun commentaire:

Enregistrer un commentaire