2

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;
6
  • so what's wrong with tolower(), exactly? Commented Apr 26, 2014 at 16:17
  • 1
    Try removing the () after tolower - you want to pass the function, not call it. Commented Apr 26, 2014 at 16:20
  • Whether i use 'transform' or 'tolower()' i get 'error: cannot convert 'std::string' to 'int' for arguement '1' to 'int tolower( int )'. I dont understand since the 'entry.state' variable is a string Commented Apr 26, 2014 at 16:25
  • removing the '()' after 'tolower' give 'no matching function for call to transform' and a bunch of extra lines Commented Apr 26, 2014 at 16:29
  • Daniel has pointed you to an example Commented Apr 26, 2014 at 16:37

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.