[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

How to convert object data members to lower case.

Hello experts:

I have a Book class and am trying to figure out how to change just two of the data members (author_name and title) to lower case.  Is there any way to adapt the make_lower function (see code below) to work with objects?  Or am I going in the wrong direction alltogether.  I have spent many hours looking for examples online and in texts but can't seem to find anything close.  Any help would be appreciated!


-----------------------------------------------------------------
#include <string>
using namespace std;

# ifndef BOOK_H
# define BOOK_H

class Book
{
      public:
            Book() { };
            Book(string an_author_name, string a_title, string a_publisher,                    string an_isbn, string a_pub_year,
                   string a_page_count, string a_price, string a_book_type)
            {
                  author_name = an_author_name;
                  title = a_title;
                  publisher = a_publisher;
                  isbn = an_isbn;
                  pub_year = a_pub_year;
                  page_count = a_page_count;
                  price = a_price;
                  book_type = a_book_type;
            }

            ~Book() { };
            
            string get_author_name() const;
            string get_title() const;
            string get_publisher() const;
            string get_isbn() const;
            string get_pub_year() const;
            string get_page_count() const;
            string get_price() const;
            string get_book_type() const; //hardcover or paperback
            
            void set_author_name(string new_author_name);
            void set_title(string new_title);
            void set_publisher(string new_publisher);
            void set_isbn(string new_isbn);
            void set_pub_year(string new_pub_year);
            void set_page_count(string new_page_count);
            void set_price(string new_price);
            void set_book_type(string new_book_type);  
            
            string to_string();
            string make_lower(const string& str);

      private:
            string author_name;
            string title;
            string publisher;
            string isbn;
            string pub_year;
            string page_count;
            string price;
            string book_type;
};
#endif;

--------------------------------------------------
// file name: book8.cpp
// implementation file for Book class

#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctype.h>
#include "book8.h"

using namespace std;

string Book::get_author_name() const
{
      return author_name;
}

string Book::get_title() const
{
      return title;
}
string Book::get_publisher() const
{
      return publisher;
}

string Book::get_isbn() const
{
      return isbn;
}

string Book::get_pub_year() const
{
      return pub_year;
}

string Book::get_page_count() const
{
      return page_count;
}

string Book::get_price() const
{
      return price;
}

string Book::get_book_type() const
{
      return book_type;
}

void Book::set_author_name(string new_author_name)
{
      author_name = new_author_name;
}

void Book::set_title(string new_title)
{
      title = new_title;
}

void Book::set_publisher(string new_publisher)
{
      publisher = new_publisher;
}

void Book::set_isbn(string new_isbn)
{
      isbn = new_isbn;
}

void Book::set_pub_year(string new_pub_year)
{
      pub_year = new_pub_year;
}

void Book::set_page_count(string new_page_count)
{
      page_count = new_page_count;
}

void Book::set_price(string new_price)
{
      price = new_price;
}

void Book::set_book_type(string new_book_type)
{
      book_type = new_book_type;
}

string Book::to_string()
{
      
      return ("NAME: " + author_name + " | " + "TITLE: " + title + " | " 
                "PUBLISHER: " + publisher + " | " + "ISBN: " + isbn +
                 " | " + "YEAR: " + pub_year + " | " + "PAGES: " +
                   page_count + " | " + "PRICE: " + price + " | " + book_type);      
               
}

string make_lower(const string& str)
{
      string temp(str);

      for (unsigned int i = 0; i < str.length; i++)
      {
            temp[i] = tolower(str[i]);
            return temp;
      }
}
-----------------------------------------------
//file name: book_user8.cpp

#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include "book8.h"

using namespace std;

int main()
{
      string(str);
      ofstream fout;
      
      Book b1("Bill W. Smith", "Utopia", "Mcgraw Hill", "00-321-4567", "1978", "800",
                    "5.99", "hardcover");
      
      cout << b1.to_string() << endl << endl;
      fout.open("C:\\CSC311\\files\\book8_out.txt");
      fout << b1.to_string() << endl << endl;

      b1.set_author_name("William W. Smith");
      
      cout << b1.to_string() << endl << endl; //for debugging
      fout << b1.to_string() << endl << endl; //for debugging
      
      Book b2;
      
      b2.set_author_name("John J. Jones");
      b2.set_title("The Best You Can Be!");
      b2.set_publisher("Mcmillan");
      b2.set_book_type("paperback");
      b2.set_page_count("1003");
      b2.set_pub_year("1897");
      b2.set_price("47.95");
      b2.set_isbn("23-98-10004");

      cout << b2.to_string() << endl << endl; //for debugging
      fout << b2.to_string() << endl << endl; //for debugging

      b2.make_lower();

      return 0;
}



                             
0
deekakes
Asked:
deekakes
  • 3
  • 3
2 Solutions
 
DexstarCommented:
deekakes:

> I have a Book class and am trying to figure out how to change just two of the
> data members (author_name and title) to lower case.  Is there any way to adapt
> the make_lower function (see code below) to work with objects?  Or am I going
> in the wrong direction alltogether.  I have spent many hours looking for
> examples online and in texts but can't seem to find anything close.  Any help
> would be appreciated!

You can't use on objects like that, but you could add a "make_members_lower" method to your class, and have it call the make_lower function for each string it has.

void Book::make_members_lower()
{
      author_name = make_lower( author_name );
      // Repeat for other members
}

Hope That Helps,
Dex*
0
 
tinchosCommented:
Hi deekakes,

First of all, I'm not sure that make_lower would be a method of the book class, as it does not do anything to the object itself, it is just an auxiliary function.

Here's your make_lower function working with strings, it receives a string as a paremter and returns it in lowercase

for example:

make_lower( "MyStringExample" ) returns mystringexample


string make_lower(const string& str)
{
     string temp(str);
       int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
      return temp;
}

Hope this helps

Tincho
0
 
deekakesAuthor Commented:
Thanx.....I would have never thought of that!  However the compiler complains:

c:\csc311\files\book8.cpp(110) : error C2297: '<' : illegal, right operand has type 'unsigned int (__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::*'

the compiler did the same thing even when I didn't have "unsigned" befor int.  Any suggestions? ( I was wondering if it had to do with VC++.)
---------------------------------------------------------------------
string make_lower(const string& str)
{
     string temp(str);

     for (unsigned int i = 0; i < str.length; i++)
     {
          temp[i] = tolower(str[i]);
          return temp;
     }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
tinchosCommented:
deekakes,

I would definitely recommend you to use the code I provided.
I believe that it is safer and easier to access the string using an iterator than indexing it.

Try with the code above, I've tested it and it should work as you want to.

Tincho
0
 
deekakesAuthor Commented:
I added the following code:

void Book::make_members_lower()
{
      author_name = make_lower(author_name);
        title = make_lower(title);
}


string make_lower(const string& str)
{
     string temp(str);
      int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
     return temp;
}

And added this call in the main program:
b2.make_members_lower();

The compiler states:

ook8.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Book::make_lower(class std::basic_string<char,struct std::char_traits<char>,class std::
allocator<char> > const &)" (?make_lower@Book@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
Debug/Book8.exe : fatal error LNK1120: 1 unresolved externals
 
0
 
tinchosCommented:
The declaration of

string make_lower(const string& str)

must be before the call, so you have 2 possibilites

either

string make_lower(const string& str)
{
     string temp(str);
      int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
     return temp;
}

void Book::make_members_lower()
{
      author_name = make_lower(author_name);
       title = make_lower(title);
}


or

string make_lower(const string& str);

void Book::make_members_lower()
{
      author_name = make_lower(author_name);
       title = make_lower(title);
}


string make_lower(const string& str)
{
     string temp(str);
      int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
     return temp;
}

Hope this helps

Tincho
0
 
deekakesAuthor Commented:
Thanx...works fine!!  Thank you for preventing me from jumping off a cliff :)

I increased the points....75 to Dexstar for the function I would have never thought of and 75 to tincho for the other excellent and clearcut solutions.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now