Thomas Ahrens
Thomas Ahrens

Reputation: 23

Reading each character from a separate file?

I'm currently working on a school assignment that involves first taking in the contents of x number of files that the user inputs the names of, then reading how many of certain letters there, then outputting that to the user. This is what I have so far:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;

int main()
{
    const int fileNameSize = 20;
    int fileCount = 0;
    ifstream inFile;
    ofstream outFile;
    int numRows, numPRow, num, rowSum;
    char inputFileName [fileNameSize+1];
    cout << "How many files are being processed?\n";
    cin >> fileCount;
    for (int i = 0; i <= fileCount; i++)
    {
        cin >> inputFileName;
        inFile.open(inputFileName);
        if (!inFile)
        {
            cout << "Could not open input file.\n";
            return 0;
        }
        else
        {
            int acount, tcount, ecount, ocount, icount, ncount, scount, hcount, rcount;
            char c;
            while (!inFile.eof())
            {
                //inFile >> num;
                inFile.get(tolower(c));
                switch (c)
                {
                    case 'a': acount++;
                    case 't': tcount++;
                    case 'e': ecount++;
                    case 'o': ocount++;
                    case 'i': icount++;
                    case 'n': ncount++;
                    case 's': scount++;
                    case 'h': hcount++;
                    case 'r': rcount++;
                }               
            }
            int chartotal = acount + tcount + ecount + ocount + icount + ncount + scount + hcount + rcount;
            cout << "File name: " << inputFileName << endl;
            cout << "Number of E's: " << ecount << endl;
            cout << "Number of T's: " << tcount << endl;
            cout << "Number of A's: " << acount << endl;
            cout << "Number of O's: " << ocount << endl;
            cout << "Number of I's: " << icount << endl;
            cout << "Number of N's: " << ncount << endl;
            cout << "Number of S's: " << scount << endl;
            cout << "Number of H's: " << hcount << endl;
            cout << "Number of R's: " << rcount << endl;
            cout << "Total Number of Characters: " << chartotal << endl;
        }
    }
    //cout << "exit colm flag";
    //cout << "exit row flag\n";

}

After compiling, I receive an error with line 33:

33:26: note: candidates are:
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :283:7: note: std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_                                                                                                             CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>, st                                                                                                             d::basic_istream<_CharT, _Traits>::int_type = int]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :283:7: note:   candidate expects 0 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :297:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _T                                                                                                             raits>::get(std::basic_istream<_CharT, _Traits>::char_type&) [with _CharT = char                                                                                                             , _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::char_ty                                                                                                             pe = char]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :297:7: note:   no known conversion for argument 1 from 'int' to 'std::basic_ist                                                                                                             ream<char>::char_type& {aka char&}'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :324:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _T                                                                                                             raits>::get(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, st                                                                                                             d::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char, _Traits = std                                                                                                             ::char_traits<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std:                                                                                                             :streamsize = int]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :324:7: note:   candidate expects 3 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :335:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_is                                                                                                             tream<_CharT, _Traits>::get(std::basic_istream<_CharT, _Traits>::char_type*, std                                                                                                             ::streamsize) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_                                                                                                             istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_                                                                                                             istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :335:7: note:   candidate expects 2 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :358:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _T                                                                                                             raits>::get(std::basic_istream<_CharT, _Traits>::__streambuf_type&, std::basic_i                                                                                                             stream<_CharT, _Traits>::char_type) [with _CharT = char, _Traits = std::char_tra                                                                                                             its<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_st                                                                                                             reambuf<char>, std::basic_istream<_CharT, _Traits>::char_type = char]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :358:7: note:   candidate expects 2 arguments, 1 provided
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :368:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_is                                                                                                             tream<_CharT, _Traits>::get(std::basic_istream<_CharT, _Traits>::__streambuf_typ                                                                                                             e&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_C                                                                                                             harT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_C                                                                                                             harT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream                                                                                                             :368:7: note:   no known conversion for argument 1 from 'int' to 'std::basic_ist                                                                                                             ream<char>::__streambuf_type& {aka std::basic_streambuf<char>&}'

I understand very little of how these ifstream, ofstream, etc functions work and have simply been transcribing them from an example file and manipulating them to the desired program. Is there a specific function for reading each character from .txt file, that I can the work with?

Upvotes: 0

Views: 79

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

The problem you're referring to is here:

inFile.get(tolower(c));

If the compiler tells you that there's no applicable overload for a specific function call, the 1st action you should take is to read the documentation and check what's required to call that function.

The problem is that std::tolower()'s result type is int as rvalue, while std::istream::get() expects a char& type.

You can fix that code like

inFile.get(c);
c = tolower(c);

See the fixed code here please.


Note that there are more problems with your code as mentioned in the comments

Upvotes: 2

Related Questions