Question

For sake of simplicity i chose to use a type 'string' instead of 'char'. But i am supposed to lowercase the string that i read in from the input file. I did not know at the time i would not be able to use 'tolower()'. However i did find a way using 'transform'. But i cannot get it to work and cannot find example of using it with an array of stucts. Please help. And if at all possible i also have to capitalize the first letter(s) of each state so if you could point in in the right direction would be extremely grateful.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>

struct ATdata  
{
   string state;
   double miles;
   int shelters;
};

int readData( ifstream& input, struct ATdata data[] );
size_t readData( ifstream& input, struct ATdata data[], size_t MAX_ENTRIES );

int main()
{
    ifstream input;
    char filename[256];
    ATdata data[14];
    int i;

    cout << "Enter input file name: ";
    cin >> filename;

    input.open( filename );

    if ( input.fail() )
    {
        cout << "Input file does not exist." << endl;
        exit(1);
    }


    size_t linesRead = readData( input, data, sizeof ATdata data[], size_t MAX_ENTRIES );
    input.close();
    return(0);

}

size_t readData( ifstream& input, struct ATdata data[], size_t MAX_ENTRIES )
{
    size_t i;
    int j;
    while ( i < MAX_ENTRIES && !input.eof() )
    {
        ATdata entry;
        getline( input, entry.state );
        transform( entry.state.begin(), entry.state.end(), entry.state.begin(), tolower() );
        string nextLine;
        if ( !getline( input, nextLine ) )
        {
            break;
        }
        istringstream iss( nextLine );
        if ( !(iss >> entry.miles >> entry.shelters ) )
        {
            continue;
        }
        data[i++] = entry;
    }    
    for ( j = 0; j < 14; j++ )
    {
        cout << data[j].state << data[j].miles << data[j].shelters << endl;
    }
}
return i;
Was it helpful?

Solution

The problem is that in the line

transform( ...., tolower() );

You are calling the function tolower rather than passing it by reference to the transform algo.. -- drop the parenthesis () in the tolower...

The following is the text book examples....

#include <iostream>     // std::cout
#include <algorithm>    // std::transform
#include <vector>       // std::vector
#include <functional>   // std::plus

int main () {
    std::string myname("my name IS soren");

    // To upper
    std::transform (myname.begin(), myname.end(), myname.begin(), ::toupper);
    std::cout << myname << std::endl; 

    // To lower
    std::transform (myname.begin(), myname.end(), myname.begin(), ::tolower);
    std::cout << myname << std::endl; 

    // Capitalize first
    std::transform (myname.begin(), myname.begin()+1, myname.begin(),  ::toupper);
    std::transform (myname.begin()+1, myname.end(),   myname.begin()+1,::tolower);
    std::cout << myname << std::endl; 


    return 0;
}

However as other have pointed out, this does not handle multi-byte (e.g. UTF-8) characters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top